1

コレクションに問題があります。私はチュートリアルの例 ( 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 )。編集アクションで問題ありません。Blogpost2つのタグを同時に追加すると、データベースに1つしか追加されませんTag(countオプションは1に設定されています。この数を増やすと、オプションに従ってタグを追加できます)。それを編集してBlogpost2 つのタグ (合計 3 つ) を追加すると、2 つのタグがデータベースに追加されます。

addTags() 関数をデバッグする場合、パラメーターは add-action で 1 つの Tag オブジェクトのみを保持します。edit-action では、追加されたすべての新しいタグが保持されます (私の場合、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);
});

テンプレートを正しくレンダリングします。edit-action では問題なく動作しますが、add-action では 1 つのタグしか追加されません。問題は、入力フィルターまたはバリデーターを追加することにより、コントローラーに存在します。新しいタグを追加する場合、特定のフィルター/バリデーターが必要です。

次のコードは、フィルター/バリデーターをフォーム/フィールドセットに追加するために使用されています。

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設定されている特定のオプションよりも多くを追加できます。counttagFieldset

addAction()で設定した入力フィルターは、コレクション フィールドセットにはありません。(新しい入力フィルターを使用して) 私の basefieldset を追加することにより、コレクション フィールドセットは、指定されたcountオプションよりも多くのタグを追加できなくなります。この場合、タグフィールドセットは追加されないため、Javascript 関数を使用してタグフィールドセットを追加しても意味がありません。

4

0 に答える 0