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

Clearing form field

I'am having an issue with clearing a form field before I render the field. In this case I have created a new form element using the $user as entity. When the form is renderd I like the password field to be an empty string instead of the password has that is stored in the $user entity. The clear below is not working.

    public function editAction( $id )
    {
      $user =Users::findFirst( $id );
      $form =new UserForm( $user );

      $form->clear( 'password' );  // <-- I like for the password field to be empty

      if( $this->request->isPost())
      {
        $form->bind( $this->request->getPost(), $user );

        if( $form->isValid( ))
        {
          $result =$user->update( );
          if( $result )
          {
            $this->response->redirect( '/backend/users/' );
            return false;
          }
          else
          {
            die( 'error1' );
          }
        }
        else
        {
          die( 'error2' );
        }
      }

      $this->view->setVar( 'form', $form );
    }


85.5k

i did not know clear method exists, what i do is


$field->setDefault(null);

hope that works, haven't being using it much to be honest



43.9k

https://devdocs.io/phalcon~3/api/phalcon_forms_element#clear

Clears every element in the form to its default value



4.5k
edited May '18

Sadly I tried to set the password field in the form class, but that doesn't work.

And indeed clear resets the field to it's default value which is in my case an password hash as that is set as default value via the constructor by adding the user entity that should be used to fill the form.

I can change "$form =new UserForm( $user );" into "$form =new UserForm( );" and set all the fields manually in the controller. That would work, but also adds a lot of extra code to the controller I like to prevent.

I need a way to clear the password field to an empty string when I render the edit view for a user. As clear will not work and setting a default value for the field in the form class isn't working. Anyone know of another way ?



43.9k

a dirty client side javascript hack ?



4.5k

That might be an solution, but it is not something you want to use if you ask me. I could also extend the password field and add the missing functionality. But is would rather see this as part of the password field itself. I mean I would expect that a password tag can be emptied regarding setting a entity for a form. I mean it is a password field.



43.9k
Accepted
answer

$form->clear( 'password' ); // <-- I like for the password field to be empty

well, the rendered password field will display dots anyway ...

if you want your given $user to re enter his password while he's filling the form, do not use the "real" password in the form but something like a "passwordConfirmationn" field (set it to null by default and you should have an empty password field in the rendered view ;-))



4.5k

That might be an accepted solution. My password hases are nearly 128 chars long so my current password field has a lot of dots in it. And when a user is updated without changing the password will store a new password which is equal to the hased password currently set as value for the password field.

Thanks for the idea