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.



7.2k
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 ...)



1.5k

http://docs.phalconphp.com/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);
}