Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 );
    }


75.3k

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



36.3k

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

Clears every element in the form to its default value



1.1k
edited 11d ago

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 ?



36.3k

a dirty client side javascript hack ?



1.1k

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.



36.3k
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 ;-))



1.1k

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