Symfony2 - 动态表单选择 - 验证删除 [英] Symfony2 - Dynamic form choices - validation remove
问题描述
我有一个下拉式表单元素。最初它开始是空的,但是在用户做了一些交互之后,它通过javascript填充了值。这一切工作正常。但是,当我提交它时总会返回一个验证错误此值无效。
。
如果我将项目添加到表单代码中的选项列表中,它将验证OK,但是我试图动态填充它,并且预先将项目添加到选项列表中不去上班。
我认为这个问题是因为表单正在对一个空的项目列表进行验证。我不希望它根据列表进行验证。我已将验证设置为false。
这将仅针对添加到选择列表中的空行或项目进行验证
p> $ builder-> add('verified_city','choice',array($ b $''required'=> false
));
类似的问题在这里没有回答。
<如果你不知道所有可用的选择是什么。它可以从外部Web源加载?
在尝试查找它之后花了很多时间。你基本上需要添加一个 PRE_BIND
侦听器。在绑定可供验证的值之前添加一些额外选项。
使用Symfony \Component\Form\AbstractType ;
使用Symfony \Component\Form\FormEvents;
使用Symfony \Component\Form\FormEvent;
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
// ..在顶部创建表单代码
$ ff = $ builder-> getFormFactory();
//函数动态添加'template'选择字段
$ func = function(FormEvent $ e)use($ ff){
$ data = $ e->的getData();
$ form = $ e-> getForm();
if($ form-> has('verified_city')){
$ form-> remove('verified_city');
}
//这有助于确定可用城市的列表,我们可以使用
if($ data instanceof \Portal\PriceWatchBundle\ Entity\PriceWatch){
$ country =($ data-> getVerifiedCountry())? $ data-> getVerifiedCountry():null;
}
else {
$ country = $ data ['verified_country'];
}
//在这里你可以以某种方式填充选项u在loadChoices中使用你的服务
$ choices = array('','','Manchester '=>'曼彻斯特','利兹'=>'利兹');
#if(/ *某些条件等* /)
#{
#$ options = array('3'=>'3','4'=> ;'4');
#}
$ form-> add($ ff-> createNamed('verified_city','choice',null,compact('choices')));
};
//在PreSet和PreBind上注册上面的EventListener函数
//在第一次初始化时调用这个函数 - 在这个例子中不需要
#$ builder - > addEventListener(FormEvents :: PRE_SET_DATA,$ func);
//在验证之前调用
$ builder-> addEventListener(FormEvents :: PRE_BIND,$ func);
}
I have a drop down form element. Initially it starts out empty but it is populated with values via javascript after the user has made some interactions. Thats all working ok. However when I submit it always returns a validation error This value is not valid.
.
If I add the items to the choices list in the form code it will validate OK however I am trying to populate it dynamically and pre adding the items to the choices list is not going to work.
The problem I think is because the form is validating against an empty list of items. I don't want it to validate against a list at all. I have set validation required to false. I switched the chocie type to text and that always passes validation.
This will only validate against empty rows or items added to choice list
$builder->add('verified_city', 'choice', array(
'required' => false
));
Similar question here that was not answered.
Validating dynamically loaded choices in Symfony 2
Say you don't know what all the available choices are. It could be loaded in from a external web source?
after much time messing around trying to find it. You basically need to add a PRE_BIND
listener. You add some extra choices just before you bind the values ready for validation.
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
public function buildForm(FormBuilderInterface $builder, array $options)
{
// .. create form code at the top
$ff = $builder->getFormFactory();
// function to add 'template' choice field dynamically
$func = function (FormEvent $e) use ($ff) {
$data = $e->getData();
$form = $e->getForm();
if ($form->has('verified_city')) {
$form->remove('verified_city');
}
// this helps determine what the list of available cities are that we can use
if ($data instanceof \Portal\PriceWatchBundle\Entity\PriceWatch) {
$country = ($data->getVerifiedCountry()) ? $data->getVerifiedCountry() : null;
}
else{
$country = $data['verified_country'];
}
// here u can populate choices in a manner u do it in loadChoices use your service in here
$choices = array('', '','Manchester' => 'Manchester', 'Leeds' => 'Leeds');
#if (/* some conditions etc */)
#{
# $choices = array('3' => '3', '4' => '4');
#}
$form->add($ff->createNamed('verified_city', 'choice', null, compact('choices')));
};
// Register the function above as EventListener on PreSet and PreBind
// This is called when form first init - not needed in this example
#$builder->addEventListener(FormEvents::PRE_SET_DATA, $func);
// called just before validation
$builder->addEventListener(FormEvents::PRE_BIND, $func);
}
这篇关于Symfony2 - 动态表单选择 - 验证删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!