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

Phalcon\Forms\Element doesn't have removeValidator()

Case:

I have form with one field (Company NIP number) which is uniqe in DB. I woudlike to use one form to add and edit actions but in edit action I need to remove my own Uniqueness validator from "NIP" field. How can I do this? Class Phalcon\Forms\Element has only addValidator and getValidators methods.



12.8k
edited Jan '15

Ofcourse I can extends \Phalcon\Forms\Element and implement method "removeValidator" ($_validators is protected variable). but also I need to implement render method in each form field class (check, date, email ...)



2.1k

https://docs.phalcon.io/en/latest/reference/forms.html#initializing-forms

In the sample below there's one where u can initialize the form with options. you can use to to do some logic checking

edited Jan '15
public function initialize(Users $user, $options = []) {

    $name = new Text('name', array(
            'placeholder' => 'Your name',
            'using' => array('id', 'name'),
     ));
    $name->setLabel('Name');

    if (empty($options['edit'])) { // !in_array('edit') could work depending ...
        // don't add validator when you create the form and set a certain option
        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'Your name is required'
            )),
        ));
    }
    $this->add($name);
}