Disable notInArray Validator Zend Framework 2

若如初见. 提交于 2019-12-04 03:13:37

Since version 2.2, Zend Framework provide the ability to disable inArray validator calling:

$element->setDisableInArrayValidator(false);

or passing option to an element:

'disable_inarray_validator' => false

I had the same problem and what i did is populate the element before validate it, for example:

$clientForm->get('city')->setValueOptions($options);
$clientForm->setData($post);

if ($clientForm->isValid()) {
  //
} else {
  //
}

This don't disable notInArray valitador but you can trick it.

I came with the same case : while i was populating my html select element via ajax after alot of searching found no way to do it , ending with creating my own select form element , I will provide you with my changes :

    /**
     * Provide default input rules for this element
     *
     * Attaches the captcha as a validator.
     *
     * @return array
     */
    public function getInputSpecification()
    {
        $spec = array(
            'name' => $this->getName(),
            'required' => true,
            //// make sure to delete the validators array in the next line  
            'validators' => array( 
                $this->getValidator()
            )
        );

        return $spec;
    }

I found this at this link and thought I would post it as I found it a great solution .... http://samsonasik.wordpress.com/2012/10/01/zend-framework-2-extending-zendform-to-add-select-multicheckbox-emaildate-textarea-and-radio-element/

don’t try to deactive the default validator. re-values the select options values before setData.

$form->get('state_id')
     ->setOptions(
           array('value_options'=> $Newstatecollection)) ;

Saves the mess of deactivating the function if you need it elsewhere

If you don't need validation at all, add required => false in InputFilter

    $this->add(array(
        'name' => 'your-elements-name',
        'required' => false,
    ));

worked for me..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!