我遇到了Symfony选项解析器的问题,我需要指定一个应该规范化的已定义变量列表。
问题是:我不想在
中再次定义所有这些变量$resolver->setDefined();
因为我在$ builder中有一个已定义字段的列表,并且在实体SlotRequest中定义了相同的字段。
是否有不同的方法将所有字段/变量从实体分配到解析器?
$ resolver-> setDefined([ ' date_form''等。' ]);
但是,它毫无意义,因为在现实世界中我必须规范10个以上的变量+20个字段)
$ resolver-> setDefined($ anArrayOfParsedFieldsFromEntity);
有更好的方法吗?
使用的一个例子:
在控制器中:
$form = $this->createForm(new SlotRequestType(), new SlotRequest());
SlotRequestType:
class SlotRequestType extends AbstractType
{
/**
* @var CCriteria
*/
protected $resolved = null;
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date_from',null,['property_path'=>'dateFrom']);
//more fields
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$this->resolved = new \CCriteria();
$resolver->setDefaults(array(
'data_class' => SlotRequest::class,
'allow_extra_fields' => true,
'method' => 'GET'
));
$resolver->setDefined([....]);// the list of fields
$resolver->setNormalizer('date_from', function (Options $options, $value) {
$dateFrom = new \DateTime($value);
$this->resolved->setStartDate($dateFrom->getTimestamp());
return $value;
});
//more normalizers
}
/**
* @return null
*/
public function getName()
{
return null;
}
/**
* @return CCriteria
*/
public function getResolved()
{
return $this->resolved;
}
实体SlotRequest
<?php
namespace Test/Entity;
class SlotRequest
{
/**
* @var string
* @Assert\NotBlank(message="Parameter [date_from] is missing.")
* @Assert\Type(
* type="string",
* message="The value {{ value }} is not a valid {{ type }}."
* )
* @Assert\Date()
*/
public $dateFrom;
//more fields
}