symfony 2 choice ajax validation fix

放肆的年华 提交于 2019-12-11 19:12:14

问题


I try something like that - Validating dynamically loaded choices in Symfony 2

but this when we get submited form check possible values - in my sitation any is correct.

i add modification to allow any value - like here

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
        $data = $event->getData();
        $event->getForm()->add('tags', 'tag', [
            'label'   => 'Sub Choice',
            'choices' => $data['tags'],
            'mapped'=>false,
            'required'=>false,
            'multiple'=>true,
        ]);
    });

but it not work - how to make it usable ?

tag is my input extens of choice (for js ajax chosen)


回答1:


ok i found problem. This example is ok but we need array_flip

here is working version

 $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
        $data = $event->getData();
        if(is_array($data['tags']))$data=array_flip($data['tags']);
        else $data = array();
        $event->getForm()->add('tags', 'tag', [
            'label'   => 'Sub Choice',
            'choices' => $data,
            'mapped'=>false,
            'required'=>false,
            'multiple'=>true,
        ]);
    });


来源:https://stackoverflow.com/questions/28245027/symfony-2-choice-ajax-validation-fix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!