【问题标题】:Validate ajax dynamic checkbox fields (this value is not valid)验证 ajax 动态复选框字段(此值无效)
【发布时间】:2014-11-13 14:01:08
【问题描述】:

我有一个带有与类别字段相关的标签的表单

Category 1
    tag a
    tag b
    tag c

Category 2
    tag d
    tag e
    tag f

...

加载页面时,我有“Category 1”和“他的标签列表” 然后,当我将类别更改为“Category 2”时,我会通过 ajax 替换标签列表。

当我提交表单时,我收到“This value is not valid”。我猜这是因为表单期望初始列表中的值。

所以,我不知道如何继续验证我的标签。

这是生成表单的代码

->add('category', null, array(
    'choices' => $this->cat_tree, 
    'label' => 'Category',
    'required' => true, 
    'empty_value' => '', 
))
->add('tags', 'entity', array(
    'class' => 'MyappServicesBundle:Category',
    'query_builder' => function(EntityRepository $er) use ($parent_id)  {
        return $er->createQueryBuilder('c')
            ->where('c.parent = :parent_id')
            ->setParameter('parent_id', $parent_id)
            ->orderBy('c.title', 'ASC');
    },
    'required' => false, 
    'multiple' => true,
    'expanded' => true, 
    'label' => 'Tags',
))

这里是替换标签列表的ajax代码

$('#myapp_servicesbundle_category').change(function() {
    $.post( 
        "/tag/ajax/search", 
        { parent_id: $(this).val() }, 
        function( data ) {
            var newtags = '';   
            jQuery.each( data, function( i, val ) {         
                newtags += '    <input type="checkbox" value="'+val.id+'" name="myapp_servicesbundle[tags][]" id="myapp_servicesbundle_tags_'+val.id+'">';
                newtags += '    <label for="myapp_servicesbundle_tags_'+val.id+'">'+val.label+'</label>';
            });
            $('#myapp_servicesbundle_tags').html(newtags);              
        }, "json"
    );
}); 

提前感谢您的帮助

【问题讨论】:

    标签: php ajax symfony symfony-2.5


    【解决方案1】:

    根据我的经验,您必须在 Symfony 中使用 eventListener。文档中有一个非常清楚的例子(英文比法文更详细,小心)。

    http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

    【讨论】:

    • 谢谢!我会仔细看看,我会告诉你的
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2014-05-10
    • 2011-01-15
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    相关资源
    最近更新 更多