0

我的许多表单都有相同的字段,但这些字段不是单独的实体。我想要一些Base我可以以各种形式重复使用的东西。我尝试使用字段集,但所有示例都建议使用字段集的实体。如果我使用如下字段集,则填充字段集,但表单的其余部分不会绑定到实体字段。

BaseSF(它将有更多的共享字段)

class BaseFS extends Fieldset
{

    public function __construct()
    {
        parent::__construct('BaseFS');

        $this->setHydrator(new ClassMethodsHydrator(true));

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

    }

}

身份信息表

class IdentityInformationForm extends Form
{

    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('Identity Information');
        $this->setHydrator(new ClassMethods(true));
        $this->setObject(new IdentityInformation());

        $baseFieldset = new BaseFS();
        $baseFieldset->setUseAsBaseFieldset(true);
        $this->add($baseFieldset);

        $this->setAttribute('method', 'post');


        $this->add(array(
            'name' => 'id',
            'options' => array(
                'label' => 'Id',
            ),

        ));

        $this->add(array(
            'type' => 'Hidden',
            'name' => 'buildName',
            'attributes' => array(
                'value' => 'unknown'
            )
        ));



        $this->add(array(
            'name' => 'stud_id',
            //'type' => 'Hidden',
            'options' => array(
                'label' => 'Student Id',
            ),
        ));

        $this->add(array(
            'name' => 'stud_fname',
            'type' => 'Text',
            'options' => array(
                'label' => 'First Name',
            ),
        ));

        $this->add(array(
            'name' => 'stud_lname',
            'type' => 'Text',
            'options' => array(
                'label' => 'Last Name',
            ),
        ));


        $this->add(array(
            'name' => 'acad_year',
            'options' => array(
                'label' => 'Acad Year',
            ),
        ));
        $this->add(array(
            'name' => 'stud_email',
            'options' => array(
                'label' => 'Stud Email',
            ),
        ));
        $this->add(array(
            'name' => 'stud_phone',
            'options' => array(
                'label' => 'Stud Phone',
            ),
        ));
        $this->add(array(
            'name' => 'agency_name',
            'options' => array(
                'label' => 'Agency Name',
            ),
        ));
        $this->add(array(
            'name' => 'agency_address',
            'options' => array(
                'label' => 'Agency Address',
            ),
        ));
        $this->add(array(
            'name' => 'agency_city',
            'options' => array(
                'label' => 'Agency City',
            ),
        ));
        $this->add(array(
            'name' => 'agency_zip',
            'options' => array(
                'label' => 'Agency Zip',
            ),
        ));
        $this->add(array(
            'name' => 'agency_phone',
            'options' => array(
                'label' => 'Agency Phone',
            ),
        ));
        $this->add(array(
            'name' => 'agency_fax',
            'options' => array(
                'label' => 'Agency Fax',
            ),
        ));
        $this->add(array(
            'name' => 'stud_focus',
            'options' => array(
                'label' => 'Stud Focus',
            ),
        ));
        $this->add(array(
            'name' => 'liaison_fname',
            'options' => array(
                'label' => 'Liaison Fname',
            ),
        ));
        $this->add(array(
            'name' => 'liaison_lname',
            'options' => array(
                'label' => 'Liaison Lname',
            ),
        ));
        $this->add(array(
            'name' => 'liaison_email',
            'options' => array(
                'label' => 'Liaison Email',
            ),
        ));
        $this->add(array(
            'name' => 'liaison_phone',
            'options' => array(
                'label' => 'Liaison Phone',
            ),
        ));
        $this->add(array(
            'name' => 'finstructor_fname',
            'options' => array(
                'label' => 'Finstructor Fname',
            ),
        ));
        $this->add(array(
            'name' => 'finstructor_lname',
            'options' => array(
                'label' => 'Finstructor Lname',
            ),
        ));
        $this->add(array(
            'name' => 'finstructor_email',
            'options' => array(
                'label' => 'Finstructor Email',
            ),
        ));
        $this->add(array(
            'name' => 'finstructor_phone',
            'options' => array(
                'label' => 'Finstructor Phone',
            ),
        ));
        $this->add(array(
            'name' => 'stud_eval_id',
            'type' => 'Text',
            'options' => array(
                'label' => 'Student Evaluation Id',
            ),
        ));


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


    }

} 

编辑 1

我有学生在实习期间练习不同的技能。他们获得讲师的评分,并为自己的每项技能分配一个评分。每个技能也有示例(从 db 加载的工具提示文本)。所有这些表单元素对于每个技能都是相同的。有时我需要更改(例如添加 css 类)一个元素(例如讲师等级),并且我想避免遍历每种形式的每个元素。

表单示例,其中一种表单如下所示:

namespace OnlineFieldEvaluation\Form;

use Zend\Captcha;
use Zend\Form\Element;
use Zend\Form\Form;

class CompetencyAdvanceHumanRightsAndJusticeForm extends Form
{   
    public function __construct($name = null, $options = null)
    {
        $semester = null;

        parent::__construct('Engage Diversity and Difference in Practice', $options);

        $semester = $options['semester'];

        $this->setAttribute('method', 'post');

        $this->add(array(
            'name' => 'id',
            'type' => 'Hidden',
            'options' => array(
                'label' => 'Id',
            ),

        ));

        $this->add(array(
            'type' => 'Hidden',
            'name' => 'buildName',
            'attributes' => array(
                'value' => 'unknown'
            )
        ));

        $this->add(array(
            'type' => 'Hidden',
            'name' => 'semester',
            'attributes' => array(
                'value' => 'unknown'
            )
        ));


        $this->add(array(
            'name' => 'applJustice',
            'type' => 'Zend\Form\Element\Textarea',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'test label'
            ),
        ));

        $this->add(array(
            'name' => 'applJusticeFIGrade',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
                'value' => '0',
            ),
            'options' => array(
                'label' => 'Field Instructor Grade',
                'value_options' => $this->getOptionsForSelect($semester),
            ),
        ));

        $this->add(array(
            'name' => 'applJusticeSSGrade',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Student Self Grade',
                'value_options' => $this->getOptionsForSelect($semester),
            ),
        ));

        $this->add(array(
            'name' => 'applJusticeFBExample',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
        ));

        $this->add(array(
            'name' => 'applJusticeVFPExample',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
        ));


        $this->add(array(
            'name' => 'engageJustice',
            'type' => 'Zend\Form\Element\Textarea',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'test label'
            ),
        ));

        $this->add(array(
            'name' => 'engageJusticeFIGrade',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
                'value' => '0',
            ),
            'options' => array(
                'label' => 'Field Instructor Grade',
                'value_options' => $this->getOptionsForSelect($semester),
            ),
        ));

        $this->add(array(
            'name' => 'engageJusticeSSGrade',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Student Self Grade',
                'value_options' => $this->getOptionsForSelect($semester),
            ),
        ));

        $this->add(array(
            'name' => 'engageJusticeFBExample',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
        ));

        $this->add(array(
            'name' => 'engageJusticeVFPExample',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'disabled' => 'disabled',
                'required' => 'required',
            ),
        ));


        $this->add(array(
            'name' => 'studEvalId',
            'type' => 'Text',
            'options' => array(
                'label' => 'Student Evaluation Id',
            ),
        ));

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

        $this->add(array(
            'name' => 'submitbutton',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Save',
                'id' => 'submitbutton',
            ),
            'options' => array(
                'label' => 'Save It',
            ),
        ));
    }

    public function getOptionsForSelect($semester)
    {

        $valueOptions = null;

        if ($semester == 1) {
            $valueOptions = array(
                'na' => 'N/A',
                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10',
            );

        } else {
            $valueOptions = array(

                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10'
            );
        }


        return $valueOptions;
    }
 }

applJusticeFIGrade 和engageJusticeFIGrade 是相同类型的字段 - 讲师等级。我尝试将 FieldSet 用于 applJusticeFIGrade (我尝试了一个等级,记住我会找到一种方法来提供名称作为 fieldset 内的 select 元素的参数):

class CompetencyAdvanceHumanRightsAndJusticeForm extends Form implements ObjectManagerAwareInterface
{

 ... the rest of the form ...

 $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'applJusticeFIGradeFS',
            'options' => array(
                'count' => 1,
                'allow_add' => true,
                'allow_remove' => true,
                'object_manager' => $this->getObjectManager(),
                'should_create_template' => true,
//                'target_element' => $fieldset,
                'target_element' => array(
                    'object_manager' => $this->getObjectManager(),
                    'object' => '\OnlineFieldEvaluation\Entity\CompetencyAdvanceHumanRightsAndJustice',
                    'type' => 'OnlineFieldEvaluation\Form\FIGradeFS',
                ),
            )
        ));

.....the rest of the form
}

Instructor Grade 字段显示在视图中,但 Doctrine 无法从数据库中分配值(我留下注释掉的代码只是为了显示我已经尝试过的变体):

<?php

namespace OnlineFieldEvaluation\Form;


use OnlineFieldEvaluation\Entity\CompetencyAdvanceHumanRightsAndJustice;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;


class FIGradeFS extends Fieldset implements ObjectManagerAwareInterface, InputFilterProviderInterface
{

    protected $objectManager;

    /**
     * @param ObjectManager $objectManager
     */
    public function __construct($name = null, $options = null)
    {
        parent::__construct($name, $options = null);
    }


    /**
     *
     */
    public function init()
    {

//        $om = $this->getObjectManager();
//
//        $hydrator = new DoctrineHydrator($om, 'OnlineFieldEvaluation\Entity\CompetencyAdvanceHumanRightsAndJustice');
//
//        $this->setHydrator($hydrator)->setObject(new CompetencyAdvanceHumanRightsAndJustice());



//        $this->setHydrator(new ClassMethodsHydrator(false))
        $this->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($this->getObjectManager(), false));
        // bind entity to form
           $this->setObject(new CompetencyAdvanceHumanRightsAndJustice());


        $this->add(array(
            'type' => 'DoctrineModule\Form\Element\ObjectSelect',
//            'type' => 'Zend\Form\Element\Select',
//            'type' => 'DoctrineORMModule\Form\Element\EntitySelect',
            'name' => 'applJusticeFIGradeES',
            'attributes' => array(
                'required' => 'required',
                'id' => 'fiGrade',
            ),
            'options' => array(
                'label' => 'verifier',
                'empty_option' => '',
                'object_manager' => $this->getObjectManager(),
                'target_class' => 'OnlineFieldEvaluation\Entity\CompetencyAdvanceHumanRightsAndJustice',
                'property' => 'applJusticeFIGrade',
                'is_method' => true,
                'find_method' => array(
                    'name' => 'getApplJusticeFIGrade',
                ),
                //'value_options' => $this->getOptionsForSelect(1),
            ),
        ));

//        $value = $this->getObject()->getApplJusticeFIGrade();
//        $this->get('applJusticeFIGradeES')->setValue($value);


//        $this->add(array(
//            'name' => 'applJusticeFIGrade',
//            'type' => 'Zend\Form\Element\Select',
//            'attributes' => array(
//                'required' => 'required',
//            ),
//            'options' => array(
//                'label' => 'Field Instructor Grade',
//                'disable_inarray_validator' => true,
//                'value_options' => $this->getOptionsForSelect(1),
//            ),
//        ));
    }

    /**
     * @return array
     */
    public function getInputFilterSpecification()
    {
        return array(
            'name' => array(
                'required' => true,
            ),
        );
    }

    /**
     * @param $semester
     * @return array
     */
    public
    function getOptionsForSelect($semester)
    {

        $valueOptions = null;

        if ($semester === 1) {
            $valueOptions = array(
                'na' => 'N/A',
                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10',
            );

        } else {
            $valueOptions = array(

                '0' => '0',
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
                '7' => '7',
                '8' => '8',
                '9' => '9',
                '10' => '10'
            );
        }
        return $valueOptions;
    }

    /**
     * @return mixed
     */
    public
    function getObjectManager()
    {
        return $this->objectManager;
    }

    /**
     * @param ObjectManager $objectManager
     * @return $this
     */
    public
    function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

}
4

0 回答 0