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 make optional field in form.

Hello. I'm trying to create a simple form that stores the user. However, some fields have to be optional. Their values can be empty. Nevertheless phalcon form use PresenceOf validator with 'allowEmpty' => false, by default. I dont know how to remove this validator. I can only add new one.

So, can you help me? How to create optional element in phalcon form? What I am doing wrong?

class UserForm extends Form {

public function initialize($entity = null, $options = array())
{

    $this->setValidation(array(
        'notNullValidations' => false ));
    if (!isset($options['edit']) && !isset($options['add'])){
        $element = new Text("id");
        $this->add($element->setLabel("Id"));
    } else
        $this->add(new Hidden("id"));

    $email = new Text("email", array('id'   => 'userEmail'));
    $email->setLabel("Email");
    $email->setFilters(array('striptags', 'string'));
    $email->addValidators(
        array(
            new PresenceOf(
                array(
                    'message' => 'Email обязательно'
                )
            )
        )
    );
    $this->add($email);
    $this->add(new TextArea('admin_comment', array('id'=>'userComment')));
    $lastname = new Text('lastname', array('id'   => 'fUser'));
    $this->add(new Text('firstname', array('id'   => 'iUser')));
    $this->add($lastname);
    $this->add(new Text('patronymic', array('id'   => 'oUser')));
    $this->add(new Numeric('phone', array('id'   => 'telFaxUser')));
    if (isset($options['add'])) {
        $password = new Password('password');
        $password->setLabel("Пароль");
        $password->setFilters(array('striptags', 'string'));
        $password->addValidators(
            array(
                new PresenceOf(
                    array(
                        'message' => 'пароль обязательно'
                    )
                )
            )
        );
        $this->add($password);
    }
    $this->add(
        new Select(
            "status",
            array(
                '1' => 'Активный',
                '0' => 'Заблокированый'
            )
        )
    );
}

}

Hey, if I understood you correctly, you can just remove the validation part. I mean just dont add validation for that field:

   $email = new Text("email", array('id'   => 'userEmail'));
   $email->setLabel("Email");
   $email->setFilters(array('striptags', 'string'));
   // Remove below and field validation will not be added
 /*  $email->addValidators(
       array(
           new PresenceOf(
               array(
                   'message' => 'Email обязательно'
               )
           )
       )
   );*/

The deal, than there is still check for emptyness for fields "lastname", "firstname", "patronymic". I do not use any validators but $form->isValid() returns lastname is reqired if the field value is "".



11.6k

maybe your lastname field is set to NOT NULL at db level?

Ok thanks. It seems that I need to set ->allowEmptyStringValues(['admin_comment', 'firstname', 'lastname', 'patronymic', 'phone']); In the model. Than I can save the empty strings.



12.2k

@Vadim

or just add

['allowEmpty'     => true],

to the validator and don't use PresenceOf validator. For example, validate email against email or stringlength validator:

use Phalcon\Validation\Validator\Email as EmailValidator;

$email->addValidator(
    new EmailValidator(
            [
                'message' => 'Blabla',
                'allowEmpty => true,
            ]
    )
);