We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to clear Phalcon form element values

I have following controller action to sign user in.

public function signinAction()
{
    $form = new SigninForm;
    if ($this->security->checkToken())
    {
        try
        {
            $this->auth->signin($form);
        } catch (Exception $e)
        {
            $this->flash->error($e->getMessage());
        }
    }

    $form->clear();
    $form->get('password')->clear();
    \Phalcon\Tag::resetInput();
    $this->view->form = $form;
}

The above clear methods do not work and I get password field repopulated with user input. I am using Phalcon 2.0.10. What should i do to clear the password field?



1.6k

Are you sure it is not the autocomplete function from the browser? Have you added autocomplete="off" and the chrome hack for this?

Chrome autocomplete="off"



2.4k

Thanks for the link. I hadnt tried those. However even after trying those options, it still fills password field with value. Here tis the generated HTML form.

<form action="/signin" id="frmSignin" name="frmSignin" autocomplete="off" method="post"> 
     <input type="hidden" name="wW7InRAGI7NzGjs" value="O3JGQAHxenJ4ByE">

     <input style="display:none">
     <input type="password" style="display:none">

     <div>
         <div class="mdl-textfield mdl-js-textfield b-textfield b-ico-email is-dirty is-upgraded" data-upgraded=",MaterialTextfield">
                <label for="email" class="mdl-textfield__label">E-Mail</label>
                <input type="email" id="email" name="email" value="[email protected]" class="mdl-textfield__input">                
        </div>
     </div>

     <div>
         <div class="mdl-textfield mdl-js-textfield b-textfield b-ico-https is-dirty is-upgraded" data-upgraded=",MaterialTextfield">
                <label for="password" class="mdl-textfield__label">Password</label>
                <input type="password" id="password" name="password" value="tetetet" class="mdl-textfield__input" autocomplete="off">                
        </div>
        </div>
        <div class="form-group mdl-grid">
            <div class="mdl-cell mdl-cell--4-col mdl-cell--12-col-tablet mdl-cell--middle mdl-typography--text-center">
                <button class="mdl-button mdl-js-button mdl-button--colored mdl-button--raised mdl-js-button--ripple-effect" data-upgraded=",MaterialButton">                    <i class="fa fa-sign-in" aria-hidden="true"></i>&nbsp;&nbsp;Sign In
                </button>                
            </div>
            <div class="mdl-cell mdl-cell--7-col mdl-cell--middle mdl-typography--text-right">
                <a href="/forgot-password" class="mdl-button mdl-js-button mdl-color-text--deep-purple" data-upgraded=",MaterialButton">Reset password</a>
            </div>
            <div class="mdl-cell mdl-cell--12-col mdl-cell--middle mdl-typography--text-left alert alert-notice">
                <span class="noaccount">Don't have an account?</span><span class="signuplink"><a href="/signup">SIGN UP HERE</a></span>
            </div>
        </div>

    </form>


1.6k

Hm can you show me what code you use to generate this? Do you set value in the Form class?

// Remove the value here ->
$email = new \Phalcon\Forms\Element\Text('email', ['class' => 'xyz', 'value' => '[email protected]']);
// to this
$email = new \Phalcon\Forms\Element\Text('email', ['class' ==> 'xyz']);


2.4k

Here is the signup form code. I dont set the value in my form class. The values were autofilled by chrome.

class SigninForm extends FormBase
{
  public function initialize($entity = null, $options = null)
  {
      parent::initialize();
      $this->setAction('signin');
      $this->setUserOption('id', 'frmSignin');
      $this->setUserOption('name', 'frmSignin');
      $this->setUserOption('autocomplete', 'off');

      // Email
      $email = new Email('email');
      $email->setLabel('E-Mail');
      $email->setFilters('email');
      $email->addValidators(array(
          new PresenceOf(array(
              'message' => 'E-mail is required'
          )),
          new VEmail(array(
              'message' => 'E-mail is not valid'
          ))
      ));
      $this->add($email);

      // Password
      $password = new Password('password');
      $password->setLabel('Password');
      $password->setDefault('');
      $password->setAttribute('autocomplete', 'off');
      $password->addValidators(array(
          new PresenceOf(array(
              'message' => 'Password is required'
          ))
      ));
      $this->add($password);
    }
  }


1.6k

Try it with this:

<input type="text" style="display:none">
<input type="password" style="display:none">

and maybe put id directly before the other password field