表单未正确验证Zend Framework 2 [英] Form not validating correctly zend framework 2
问题描述
我正在尝试针对新系统范围的自定义配置.
I am trying a custom configuration for a new system wide.
我被困在使用过滤器进行表单验证上.
I'm stuck on Form validations using Filter.
主要问题:
$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);
}
}
和过滤器:
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,
),
),
)
));
}
}
令我惊讶的是,我执行$ form-> isValid()之后的错误正像这样返回:
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)
这就是问题所在,我什至没有为这些字段声明验证,仅针对idModule和名称输入,我就浪费了时间,甚至认为我什至没有声明!
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!
那些验证能从哪里来?
顺便说一句,我什至没有在视图上回显createdDate和updatedDate,仅用于数据库控制,我不需要在Form范围内验证那些字段.
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:通过从Form中删除createDate和updateDate,我得到了零条消息错误,但甚至没有验证该表单!我需要表格有效.
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.
推荐答案
首先-确保数据库表结构中的字段(createdDate和updatedDate)不为NOT NULL.
First - Do make sure that the database table structure does not have those fields (createdDate and updatedDate) as NOT NULL.
第二个-InputFilter更像-严格的行为. 如果您需要或不需要任何元素,则必须明确指定.
Second - InputFilter has more like - strict behavior. If you want any element to be required or not, then you have to specify that explicitly.
尝试将"createdDate"和"updatedDate"的过滤器设置为'required' => false,
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,
....
));
至少这样,这些消息将不会添加到errors()数组中.
At least this way, those messages wont be added in the errors() array.
这篇关于表单未正确验证Zend Framework 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!