更新basefieldset inputfilters与collection fieldset对象冲突

时间:2013-05-30 12:25:11

标签: php doctrine-orm zend-framework2 zend-form

我的收藏品有问题。我一直在关注示例教程(https://github.com/doctrine/DoctrineModule/blob/master/docs/hydrator.md#a-complete-example-using-zendform)并成功建立了BlogpostTag实体。在我的控制器中,我有一个添加动作和编辑动作。我的添加和编辑视图包含以下代码:

<?php
echo $this->formElement($blogpost->get('addTag'));
$tag = $blogpost->get('tags');

?>
<fieldset id="tagFieldset">
    <?php
        echo $this->formCollection($tag);
    ?>
</fieldset>

该视图有一个动态添加标签的按钮(如本例所示:http://zf2.readthedocs.org/en/latest/modules/zend.form.collections.html#adding-new-elements-dynamically),在编辑操作中可以正常工作。当同时添加带有2个标签的新Blogpost时,它只在数据库中添加1 Tag(计数选项设置为1.当我增加此数字时,它可以添加标签根据选项)。当我编辑Blogpost并添加2个标签(总共三个)时,会将2个标签添加到数据库中。

调试addTags()函数时,该参数在add-action中只保存1个Tag对象。在编辑操作中,它包含已添加的所有新标记(在我的示例中,包含2个新标记)。

BlogpostFieldset看起来像:

$this->add(array(
    'name' => 'addTag',
    'type' => 'button',
    'options' => array(
        'label' => 'Add more tags',
    ),
    'attributes' => array(
        'id' => 'addTag'
    )
));

$tagFieldset = new TagFieldset($serviceManager);
$this->add(array(
    'type'    => 'Zend\Form\Element\Collection',
    'name'    => 'tags',
    'options' => array(
        'count'           => 1,
        'target_element' => $tagFieldset,
        'should_create_template' => true,
        'allow_add' => true,
    )
));

用于添加字段集的Javascript:

$('#addTag').click(function(e) {
    var currentCount = $('form > #tagFieldset > label').length;
    alert(currentCount);
    //Set datatemplate (generated by the arraycollection)
    var template = $('form > #tagFieldset > span').data('template');

    //Replace the template to other addressfieldset
    template = template.replace(/__index__/g, currentCount);

    //Set new fieldset
    $('form > #tagFieldset').append(template);
});

它正确呈现我的模板。在我的编辑操作中工作正常,但我的add-action只添加了1个标记。通过添加inputfilters或验证器,我的控制器中存在问题。在添加新标签的情况下,我想要一些特定的过滤器/验证器。

以下代码用于将过滤器/验证器添加到必须放置的表单/字段集中:

public function addAction()
{
    $form = new Form($this->serviceLocator);
    $entity= new Entity();

    $form->bind($entity);

    if ($this->request->isPost()) {

        $data = $this->request->getPost();

        $form->getInputFilter()->get('fieldset')->get('input')->setRequired(true);
        $form->getInputFilter()->get('fieldset')->get('input')->allowEmpty(false);

        $form->setData($data);

        if ($form->isValid()) {
        // Handle the rest off this action.
        }

    }

    return array(
        'form' => $form
    )
}

从控制器中删除以下行 - addAction()

$form->getInputFilter()->get('fieldset')->get('input')->setRequired(true);
$form->getInputFilter()->get('fieldset')->get('input')->allowEmpty(false);

与我创建的Tags的{​​{1}}设置的给定选项相比,可以添加更多count

我在tagFieldset中设置的那些输入过滤器不在集合字段集上。通过addepting(使用新的inputfilters)我的basefieldset,集合fieldset无法添加比给定addAction()选项更多的标签。在这种情况下,使用Javascript函数添加tagfieldsets是没用的,因为它们不会被添加。

0 个答案:

没有答案