【发布时间】:2016-11-22 12:40:43
【问题描述】:
我的一个实体有一个Zend\Form\Form,它使用DoctrineModule\Form\Element\ObjectSelect 元素使用户能够选择引用的实体。
class MyEntityForm extends Zend\Form\Form
{
public function __construct()
{
// ...
$this->add([
'name' => 'referenced_entity',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => [
'object_manager' => $object_manager,
'target_class' => 'MyOtherEntity',
'property' => 'id',
'display_empty_item' => true
],
]);
// ...
}
}
引用的实体可能为空(= 数据库中的外键字段可以是NULL)。如果没有选择引用的实体,我只是无法获取表单来验证。即使给定的referenced_entity 为空(null 或"")或根本不存在(数据数组中缺少键referenced_entity),我也希望我的表单能够验证。
我尝试了各种不同的输入过滤器规格,最后的设置如下
class MyEntityForm
extends Zend\Form\Form
implements Zend\InputFilter\InputProviderInterface
{
// ...
public function getInputSpecification()
{
return [
// ...
'referenced_entity' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false
],
// ...
}
// ...
}
但无济于事,验证错误保持不变($form->getMessages() 的 var_dump 在$form->isValid() 之后的摘录)
'referenced_entity' =>
array (size=1)
'isEmpty' => string 'Value is required and can't be empty' (length=36)
我是否必须扩展 ObjectSelect 表单元素以更改其输入过滤器规范并删除 isEmpty 验证器,还是有更简单的解决方案?
【问题讨论】:
-
您是否尝试从 inputSpecification 中删除“allow_empty”和“continue_if_empty”选项,因为它们可能不是必需的。
-
你用的是什么版本的zend-form?
-
@Kwido 是的,我尝试了多种组合,但都没有奏效
-
@ViníciusFagundes composer.lock 说 zendframework 的 2.4.10 版本(是的,我们还没有从使用整个包更改为只使用我们需要的组件,将在不久的将来完成......)
-
你试过了吗:
required => false,allow_empty => true,continue_if_empty => true。最后一个与您的问题不同。您是否还确保您保存它的实体也具有referenced_entity属性注释:* @ORM\Column(name="referenced_entity", nullable=true)(可为空的部分)
标签: php forms validation doctrine-orm zend-framework2