Form not validating correctly zend framework 2

删除回忆录丶 提交于 2019-12-13 04:39:48

问题


I am trying a custom configuration for a new system wide.

I'm stuck on Form validations using Filter.

Primary Question:

    $form      = new CrudForm($this->getEntityManager());

    $request = $this->getRequest();

    if ($request->isPost()) {

        $filter = new CrudFilter();
        $form->setInputFilter($filter);
        $post = $request->getPost();
        $form->setData($post);
        //$crudLogic->edit($crudLogic->populateEntity($post)->update());

        if ($form->isValid()) {
        }
     }

This Form Never Get Validated. But what makes me crazy is that this is the Form is simple:

namespace Manager\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Doctrine\ORM\EntityManager;
class CrudForm extends Form
{
    public function __construct(EntityManager $em)
    {
        parent::__construct('Crud');
        $this->setAttribute('method', 'post');

        $idcrud = new Element('idCrud');
        $idcrud->setAttributes(array(
            'name' => 'idCrud',
            'id' => 'idCrud',
            'type' => 'text',
        ));
        $idcrud->setLabel('Id Crud');

        $this->add($idcrud);

        $idmodule = array(
            'name' => 'idModule',
            'type' => 'DoctrineModule\Form\Element\ObjectSelect',
            'options' => array(
                'label' =>'Id Module',
                'object_manager' => $em,
                'target_class' => 'Manager\Entity\Module',
                'property' => 'name',
                'empty_option' => 'Choose',
            ),
        );
        $this->add($idmodule);

        $name = new Element('name');
        $name->setAttributes(array(
            'name' => 'name',
            'id' => 'name',
            'type' => 'text',
        ));
        $name->setLabel('Name');

        $this->add($name);

        $createddate = new Element\Date('createdDate');
        $createddate->setAttributes(array(
            'name' => 'createdDate',
            'id' => 'createdDate',
            'type' => 'date',
            'size' => '30',
            'class' => 'datepicker',
        ));
        $createddate->setLabel('Created Date');

        $this->add($createddate);

        $updateddate = new Element\Date('updatedDate');
        $updateddate->setAttributes(array(
            'name' => 'updatedDate',
            'id' => 'updatedDate',
            'type' => 'date',
            'size' => '30',
            'class' => 'datepicker',
        ));
        $updateddate->setLabel('Updated Date');

        $this->add($updateddate);

        $send = new Element('submit');
        $send->setValue('Submit');
        $send->setAttributes(array(
            'type'  => 'submit'
        ));
        $this->add($send);
    }
}

And The Filter:

namespace Manager\Filter;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class CrudFilter extends InputFilter
{
    public function __construct()
    {

        $this->add(array(
            'name' => 'IdCrud'
            ,'filters'  => array(
                array('name' => 'Int'),
            )
        ));
        $this->add(array(
            'name' => 'IdModule'
            ,'required' => true
            ,'filters'  => array(
                array('name' => 'Int'),
            )
        ));
        $this->add(array(
            'name' => 'Name'
            ,'required' => true
            ,'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 1,
                    'max'      =>150,
                ),
            ),
        )
    ));

}

}

To me surprise at all, the erros after i do $form->isValid() is returning like it:

array (size=2)
  'createdDate' => 
    array (size=1)
      'isEmpty' => string 'Value is required and can't be empty' (length=36)
  'updatedDate' => 
    array (size=1)
      'isEmpty' => string 'Value is required and can't be empty' (length=36)

So it is the problem, i not even declare validation on those fields, just for idModule and name inputs, i'm losting time on one think that i not even declare!

Where can is coming those validation?

Edit: By the way i not even echoing the createdDate and updatedDate on view, is just for database control, i not need those fields to be validated on Form scope.

Edit2: By removing the createDate and updateDate from Form, i get zero message erros, but even not validating the form! I need the form to be valid.


回答1:


First - Do make sure that the database table structure does not have those fields (createdDate and updatedDate) as NOT NULL.

Second - InputFilter has more like - strict behavior. If you want any element to be required or not, then you have to specify that explicitly.

Try setting the Filter for 'createdDate' and 'updatedDate' as 'required' => false,

$this->add(array(
    'name' => 'createdDate',
    'required' => false,
    ....
));

$this->add(array(
    'name' => 'updatedDate',
    'required' => false,
    ....
));

At least this way, those messages wont be added in the errors() array.




回答2:


I have the same issue it seems you cant make it optional.

I've tried setting 'required' => false, etc but to no avail.

This seems like a bug to me.

The table DDL has nulls allowed, I try to allow Null values by setting required off, but these options are ignored.

Looks like you have to extend the DoctrineModule\Form\Element\ObjectSelect and force null valuses to be accepted.

Makes ObjectSelect less attractive for use in real applications though.



来源:https://stackoverflow.com/questions/23170719/form-not-validating-correctly-zend-framework-2

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