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

disable password required

i have form with password field

controller

$object = (new Object)->getOne($id);
$form = new EditForm();
$form->setEntity($object);

form

$this->add((new Password('password', [
                'required' => false,
                'class' => 'form-control input-md'
            ]))
            ->setLabel('Password')
        );

after render

<input type="password" id="password" name="password" value="password hash" class="form-control input-md" required="" />
  1. how disable password required?
  2. how set empty password value, when i edit object?


5.7k
Accepted
answer
edited May '15

If you remove "required" => false completely from the form setting, it should remove it from the output.

$this->add(new Password('password',
    [ 'class' => 'form-control input-md' ]
))->setLabel('Password');

As for not having the password field auto-populated, you can do a few things. I've done both in the past:

Idea #1:

Do not name your password field password. Name it something like password_update.

$this->add(new Password('password_update',
    [ 'class' => 'form-control input-md' ]
))->setLabel('Password');

Then in your controller check for $this->request->getPost("password_update").


 class SomeController extends \Phalcon\Mvc\Controller {

    // Some action in your controller that the form was posted to
    function someotherAction(){
        $object = (new Object)->getOne($id);

        $form = new EditForm($object);
        // $form->setEntity($object);

        // Check to see if the form was posted
        if($this->request->isPost()){

            // Check to see if the form is valid
            if($form->isValid($this->request->getPost()){

                // Set the standard fields
                $object->setField1($this->request->getPost('field_1'));
                $object->setField2($this->request->getPost('field_2'));

                // Store the new password value into $password_update variable for reuse
                $password_update = $this->request->getPost('password_update');

                // Check to see if a password was submitted/updated
                if($password_update){
                    // If a new password was posted, set the new password in the object
                    $object->setPassword($password_update);
                }

                // Update/save the object
                if(!$object->update()){
                    // Object Update Failed - Flash the error messages here
                    $errors = [];
                    foreach($object->getMessages() as $message){
                        $errors[] = $message->getMessage();
                    }
                    $this->flash->error(implode('<br />', $errors));
                } else {
                    // Save succeeded - Flash success
                    $this->flash->success('Record was updated successfully');
                }               
            } else {
                // Form validation failed - flash the error messages here
                    $errors = [];
                    foreach($form->getMessages() as $message){
                        $errors[] = $message->getMessage();
                    }
                    $this->flash->error(implode('<br />', $errors));
            }

        }

        $this->view->form = $form;
        $this->view->object = $object;
    }
}

Idea #2:

Physically type out the input password tag, do not have it go through the phalcon form with no default values