Zend framework2 ZF2+Doctrine2注释表单必需且允许为空
我在条令实体类中使用注释。字段的注释包括:Zend framework2 ZF2+Doctrine2注释表单必需且允许为空,zend-framework2,annotations,doctrine,Zend Framework2,Annotations,Doctrine,我在条令实体类中使用注释。字段的注释包括: /** * @var integer * * @ORM\Column(name="duree", type="integer", nullable=true) * * @Form\Type("Zend\Form\Element\Number") * @Form\Attributes({"required":false, "placeholder":"Durée", "min":"1", "max":"20"}) * @Form\Re
/**
* @var integer
*
* @ORM\Column(name="duree", type="integer", nullable=true)
*
* @Form\Type("Zend\Form\Element\Number")
* @Form\Attributes({"required":false, "placeholder":"Durée", "min":"1", "max":"20"})
* @Form\Required(false)
* @Form\AllowEmpty()
* @Form\Options({"label":"Durée :"})
* @Form\Filter({"name": "Int"})
* @Form\Validator({"name":"IsInt"})
*/
private $duree;
因此DB列可以是空的,也可以是空的,以i want的形式,同一个ie用户可以将输入留空。我已经将注释Requiredfalse和allowEmpty都添加到了注释中,但是对于这个字段,表单从不有效总是得到isEmpty
若我将@Form\Type设置为Text,那个么若输入为空,那个么表单是有效的事件。但就班级人数而言,就不一样了
我有相同的pb和对应于关系的Select元素。注释包括:
/**
* @var \Application\Entity\CcCategorie
*
* @ORM\ManyToOne(targetEntity="Application\Entity\CcCategorie")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="categorie", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* })
*
* @Form\Type("DoctrineModule\Form\Element\ObjectSelect")
* @Form\Attributes({"type":"select", "required":false})
* @Form\Options({"label":"Catégorie :"})
* @Form\Required(false)
* @Form\AllowEmpty()
* @Form\Options({
* "label":"Catégorie :",
* "empty_option": "---",
* "target_class": "Application\Entity\CcCategorie",
* "property": "label"
* })
*/
private $categorie;
但是,如果在验证表单时将Select设置为empty选项,则此字段有错误isEmpty
我找到的唯一解决方法是设置注释
* @Form\Type("\Application\Form\Entity\CcRepriseFieldset")
位于实体类的顶部。类CCRepreseFieldSet扩展Fieldset,并实现InputFilterProviderInterface。然后我在这个类中指定函数:
public function getInputFilterSpecification()
{
return array(
array(
"name" => "duree",
'required' => false,
'allow_empty' => true,
),
array(
"name" => "categorie",
'required' => false,
'allow_empty' => true,
),
);
}
有了这个它就工作了。。。但这不是注释。
我不明白为什么注释不起作用
谢谢好的,我找到了问题所在。 Annotation Builder制作了一个包含所有字段的数组,如下所示:
array (size=7)
'name' => string 'reprise' (length=7)
'attributes' => array (size=0)
'elements' =>
array (size=8)
1 =>array (size=2)
...
'fieldsets' => array (size=0) empty
'type' => string '\Application\Form\Entity\CcRepriseFieldset'
(length=42)
'input_filter' =>
array (size=8)
'name' =>
array (size=4)
'name' => string 'name' (length=4)
'required' => boolean true
'filters' =>
array (size=3)
...
'validators' =>
array (size=2)
...
但此数组是附加到实体的字段集的数组。
所以Zend\Form\Factory永远不会解析它,因为“input\u filter”只是对ZendForm元素的解析,而不是对fieldset的解析,当然,因为fieldset没有SetInputFilter方法 好的,我发现了问题所在。 Annotation Builder制作了一个包含所有字段的数组,如下所示:
array (size=7)
'name' => string 'reprise' (length=7)
'attributes' => array (size=0)
'elements' =>
array (size=8)
1 =>array (size=2)
...
'fieldsets' => array (size=0) empty
'type' => string '\Application\Form\Entity\CcRepriseFieldset'
(length=42)
'input_filter' =>
array (size=8)
'name' =>
array (size=4)
'name' => string 'name' (length=4)
'required' => boolean true
'filters' =>
array (size=3)
...
'validators' =>
array (size=2)
...
但此数组是附加到实体的字段集的数组。
所以Zend\Form\Factory永远不会解析它,因为“input\u filter”只是对ZendForm元素的解析,而不是对fieldset的解析,当然,因为fieldset没有SetInputFilter方法 好吧,我有一个解决办法,但我对它不是很满意。 首先,我创建了一个新的FormFactory:
namespace Application\Form;
use Zend\Form\FieldsetInterface;
Class EntityFormFactory extends \Zend\Form\Factory {
public function configureFieldset(FieldsetInterface $fieldset, $spec)
{
$fieldset=parent::configureFieldset($fieldset, $spec);
$fieldset->input_filter_factory= $spec['input_filter'];
return $fieldset;
}
}
因此,该工厂将输入过滤器添加到字段集的输入过滤器工厂变量
那么在fieldset类中是:
class CcRepriseFieldset extends Fieldset implements \Zend\InputFilter\InputFilterProviderInterface
{
public function getInputFilterSpecification()
{
if (isset($this->input_filter_factory)){
return $this->input_filter_factory;
}
}
}
最后,当我使用annotationbuilder时,我更改了formfactory:
$builder = new AnnotationBuilder($this->_em);
$builder->setFormFactory(new EntityFormFactory());
这一切都很好。。。我不认为这是最好的方法。好的,我有一个解决办法,但我不是很满意。 首先,我创建了一个新的FormFactory:
namespace Application\Form;
use Zend\Form\FieldsetInterface;
Class EntityFormFactory extends \Zend\Form\Factory {
public function configureFieldset(FieldsetInterface $fieldset, $spec)
{
$fieldset=parent::configureFieldset($fieldset, $spec);
$fieldset->input_filter_factory= $spec['input_filter'];
return $fieldset;
}
}
因此,该工厂将输入过滤器添加到字段集的输入过滤器工厂变量
那么在fieldset类中是:
class CcRepriseFieldset extends Fieldset implements \Zend\InputFilter\InputFilterProviderInterface
{
public function getInputFilterSpecification()
{
if (isset($this->input_filter_factory)){
return $this->input_filter_factory;
}
}
}
最后,当我使用annotationbuilder时,我更改了formfactory:
$builder = new AnnotationBuilder($this->_em);
$builder->setFormFactory(new EntityFormFactory());
这一切都很好。。。我不认为这是最好的方法。伙计,一些建议-远离表单注释。构建适当的、可缓存的代码,并将输入过滤器与表单构造分离;这篇小博文可能会帮助您明确指出allow empty应该为true吗?ie@Form\AllowEmptytrue我使用的表单注释与@Sa偶的意见不同,但我完全希望将参数保留为空,因为必须将其解释为错误value@Saeven谢谢你的帮助。我发现使用条令和表单注释有一个优势:所有信息都在同一个位置。我发现有一种感觉,如果ORM定义为type=integer,nullable=true,那么在同一个位置定义包含此数据的表单字段是一个允许空值的数字,并且具有Int Validator和filter,当然还有完全相同的名称。当您需要更改数据库中的某些内容时,只需编辑点块,然后运行doctrine工具来更改数据库。所有其他设置都在相关的类扩展字段集中。@根据ZF 2 doc AllowEmpty,Crisp:将输入标记为允许空值。此注释不需要值。但我已经尝试了你的建议,没有成功的结果。谢谢你的想法,一些建议-远离表单注释。构建适当的、可缓存的代码,并将输入过滤器与表单构造分离;这篇小博文可能会帮助您明确指出allow empty应该为true吗?ie@Form\AllowEmptytrue我使用的表单注释与@Sa偶的意见不同,但我完全希望将参数保留为空,因为必须将其解释为错误value@Saeven谢谢你的帮助。我发现使用条令和表单注释有一个优势:所有信息都在同一个位置。我发现有一种感觉,如果ORM定义为type=integer,nullable=true,那么在同一个位置定义包含此数据的表单字段是一个允许空值的数字,并且具有Int Validator和filter,当然还有完全相同的名称。当您需要更改数据库中的某些内容时,只需编辑点块,然后运行doctrine工具来更改数据库。所有其他设置都在相关的类扩展字段集中。@根据ZF 2 doc AllowEmpty,Crisp:将输入标记为允许空值。此注释不需要值。但我已经尝试了你的建议,没有成功的结果。谢谢你的主意