2
votes

i was reading http://mwop.net/blog/2012-07-02-zf2-beta5-forms.html and working with that annotation builder in zf2 and doctrine without any problem

i wonder if i had a zend form class ... for example class bookForm ... how can i use this annotation builder inside the class

for example load basic fields from doctrine entity annotation then add some extra things (like submit button) inside the bookForm class ...

in mwop.net example it use within controller ... if I add my extra form fields in that controller would be too ugly..

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.
}

please help

3

3 Answers

4
votes

I found this question while trying to find a way of adding fieldsets using the AnnotationBuilder. The correct way to add fieldsets would be to set the Annotation\Type in your entity like so

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

Then within your form you are able to add the Annotated form as a fieldset like so

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'
            )
        ));
    }
}

Hope this helps someone else.

2
votes

You can set the base form class in your entity class like

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

Full working example: https://gist.github.com/nepda/d572f9ad787c48c8555d

1
votes

actually for my issue you have to create your own base form using zend_form 2.0 then add the secondary form that u build with AnnotationBuilder as field-set to first form

code example :

$newform = new BaseForm();


        $user = new Entity\Material;
        $builder = new AnnotationBuilder();
        $form = $builder->createForm($user);
        $fld = $form->getElements();
        foreach ($fld as $fldone) {
            $newform->add($fldone);
        }