私のフォームの多くには同じフィールドがありますが、それらのフィールドは別個のエンティティではありません。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 クラスを追加) する必要がある場合があり、すべてのフォームのすべての要素を通過することを避けたい場合があります。
フォームの例。フォームの 1 つは次のようになります。
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 は、同じ種類のフィールド (インストラクターの成績) です。applJusticeFIGrade に FieldSet を使用しようとしました ( fieldset 内の select 要素の引数として名前を提供する方法を見つけることを念頭に置いて、1 つのグレードで試しました)。
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;
}
}