2

私はhttp://mwop.net/blog/2012-07-02-zf2-beta5-forms.htmlを読んでいて、zf2と教義でその注釈ビルダーを問題なく使用していました

Zendフォームクラスがあったのだろうか...たとえばbookFormクラス...クラス内でこの注釈ビルダーを使用するにはどうすればよいですか

たとえば、doctrine エンティティ アノテーションから基本的なフィールドをロードし、bookForm クラス内にいくつかの追加機能 (送信ボタンなど) を追加します ...

mwop.netの例では、コントローラー内で使用しています...そのコントローラーに追加のフォームフィールドを追加すると、見苦しくなりすぎます..

use MyVendor\Model\User;
use Zend\Form\Annotation\AnnotationBuilder;

$user    = new User();
$builder = new AnnotationBuilder();
$form    = $builder->createForm($user);

$form->bind($user);
$form->setData($dataFromSomewhere);
if ($form->isValid()) {
    // $user is now populated!
    echo $form->username;
    return;
} else {
    // probably need to render the form now.
}

助けてください

4

3 に答える 3

4

AnnotationBuilder を使用してフィールドセットを追加する方法を見つけようとしているときに、この質問を見つけました。フィールドセットを追加する正しい方法は、エンティティに Annotation\Type を次のように設定することです

namespace Application\Entity;
/** 
 * 
 * My Entity.
 * 
 * @ORM\Entity
 * @ORM\Table(name="my_table")
 * 
 * @Annotation\Name("my_name")
 * @Annotation\Type("fieldset")
 * 
 */
class SomeEntity ...

次に、フォーム内で、注釈付きフォームをフィールドセットとして追加できます

namespace Application\Form;

use Zend\Form\Form,
    Doctrine\Common\Persistence\ObjectManager,
    DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator,
    Zend\Form\Annotation\AnnotationBuilder;

class SomeForm extends Form
{
    public function __construct(ObjectManager $objectManager)
    {
        // we want to ignore the name passed
        parent::__construct('entity-create-form');
        $this->setAttribute('method', 'post')
             ->setHydrator(new DoctrineHydrator($objectManager));

        $builder    = new AnnotationBuilder();

        $entity = new Application\Entity\SomeEntity;
        //Add the fieldset, and set it as the base fieldset
        $fieldset = $builder->createForm( $entity ) ;
        $fieldset->setUseAsBaseFieldset(true);
        //var_dump($fieldset);
        $this->add( $fieldset );


        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf'
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Save'
            )
        ));
    }
}

これが他の誰かに役立つことを願っています。

于 2013-08-25T09:43:33.683 に答える
2

次のようにエンティティクラスに基本フォームクラスを設定できます

/**
 * @Annotation\Type("App\Form\BookForm")
 */
class Model
{
}

完全な作業例: https://gist.github.com/nepda/d572f9ad787c48c8555d

于 2015-04-04T12:39:23.217 に答える
1

実際、私の問題では、zend_form 2.0 を使用して独自のベース フォームを作成し、AnnotationBuilder で作成したセカンダリ フォームをフィールド セットとして最初のフォームに追加する必要があります。

コード例:

$newform = new BaseForm();


        $user = new Entity\Material;
        $builder = new AnnotationBuilder();
        $form = $builder->createForm($user);
        $fld = $form->getElements();
        foreach ($fld as $fldone) {
            $newform->add($fldone);
        }
于 2012-08-22T07:41:45.277 に答える