【发布时间】:2017-06-18 20:46:44
【问题描述】:
我正在使用带有 ajax 的 select2 插件在我的表单上有一个动态字段,但是当我提交它时它返回一个错误 “此值无效”,这是正常原因我在创建时的choices 选项中使用ChoiceType 和一个空的array()。根据 symfony 文档的this 部分,表单事件是我的救星,所以尝试使用它,但我的代码看起来有问题,看不出是什么。
所以我的问题是:
如何将选择的可能性传递给字段,以使表单有效。
我的表单类型
class ArticleType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//My other field
//My functions to add the field with the possible choices
$formModifier = function (FormInterface $form, $imageValue) use ($options) {
if ($imageValue !== null) {
$listImages = $this->getChoiceValue($imageValue, $options);
if (!$listImages) {
$form->get('image')->addError(new FormError(
'Nous n\'avons pas pu trouver l\'image, veuiller choisir une autre'
));
}
} else {
$listImages = array();
}
//die(var_dump($listImages)); //Array of Image
$form->add('image', ChoiceType::class, array(
'attr' => array(
'id' => 'image'),
'expanded' => false,
'multiple' => false,
'choices' => $listImages));
};
$formModifierSubmit = function (FormInterface $form, $imageValue) use ($options) {
if ($imageValue !== null) {
$listImages = $this->getChoiceValue($imageValue, $options);
if (!$listImages) {
$form->get('image')->addError(new FormError(
'Nous n\'avons pas pu trouver l\'image, veuiller choisir une autre'
));
}
} else {
$form->get('image')->addError(new FormError(
'Veuillez choisir une image s.v.p.'
));
}
//die(var_dump($listImages)); //Array of Image object
$config = $form->get('image')->getConfig();
$opts = $config->getOptions();
$chcs = array('choices' => $listImages);
//die(var_dump($chcs)); //output an array with a 'choices' keys with array value
array_replace($opts, $chcs); //not work
//array_merge($opts, $chcs); //not work
//die(var_dump($opts)); //replacements/merge are not made
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be the entity Article
$data = $event->getData();
$formModifier($event->getForm(), $data->getImage());
}
);
//$builder->get('image')->addEventListener( //give error cause the field image don't exist
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($formModifierSubmit) {
$imageVal = $event->getData();
//die(var_dump($imageVal)); //return all the submitted data field in an array
//But when change this event to Submit it return the Article model populated by the submitted data, EXCEPT the image field which have null as value
$formModifierSubmit($event->getForm(), $imageVal['image']);
}
);
}
public function getChoiceValue($imageValue, $options)
{
$listImages = $options['em']->getRepository('AlmotivAppBundle:Image')->findBy(array(
'id' => $imageValue
));
return $listImages; //array of Image object
}
[...]
}
供参考
我的image 字段不依赖于文档示例等任何其他字段,因此我需要在PRE_SUBMIT 事件上填充choices 选项以提供可能的选择。
而且image 在我的Article 实体中有一个多对一关系
class Article implements HighlightableModelInterface
{
//some properties
/**
* @ORM\ManyToOne(targetEntity="Image\Entity\Path", cascade={"persist"})
* @Assert\Valid()
*/
private $image;
}
如果我的情况不好,请告诉我,因为我现在不知道,我会尝试很多事情,比如
- array_replace 用配置字段中的选项但是没有错。
- 向表单操作
url : $form.attr('action')的url 发出一个ajax 请求,我认为它会加载choices选项,可能是<option>,但我的选择仍然返回,没有<option>。
还有更多(记不得了)。
而且我正在使用框架的 v3.1 和 select2 插件的 v4.0.3,如果需要更多信息,请询问并感谢阅读和尝试帮助。
编辑
添加一些信息更清楚
【问题讨论】:
标签: php forms symfony symfony-forms