【问题标题】:using annotation builder in extended zend form class在扩展的zend表单类中使用注释构建器
【发布时间】:2012-08-17 08:55:03
【问题描述】:

我正在阅读 http://mwop.net/blog/2012-07-02-zf2-beta5-forms.html 并在 zf2 和教义中使用该注释构建器没有任何问题

我想知道我是否有一个 zend 表单类...例如 bookForm 类...我如何在该类中使用此注释生成器

例如,从学说实体注释中加载基本字段,然后在 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.
}

请帮忙

【问题讨论】:

    标签: zend-framework zend-form zend-framework2


    【解决方案1】:

    我在尝试寻找使用 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'
                )
            ));
        }
    }
    

    希望这对其他人有所帮助。

    【讨论】:

    • 在我发布解决方案时没有@annotation/type ...它最近添加到 zf2 包中
    • 是的,我已经看到这是对框架的后期添加,您的回答实际上帮助我解决了一个单独的问题,即将理论水合器注入基本表单,这对于 Annotation 构建器来说仍然是一个挑战。这个答案只是为了帮助那些仍在试图找出如何处理这些任务的人
    【解决方案2】:

    您可以在实体类中设置基本表单类,例如

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

    完整的工作示例:https://gist.github.com/nepda/d572f9ad787c48c8555d

    【讨论】:

      【解决方案3】:

      实际上,对于我的问题,您必须使用 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);
              }
      

      【讨论】:

        猜你喜欢
        • 2018-11-24
        • 2014-03-12
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多