コード例 #1
0
 /**
  * Build form field
  *
  * @param DocumentType  $document      Document type
  * @param FormInterface $form          Form
  * @param string        $name          Field name
  * @param string        $type          Field type
  * @param array         $options       Options
  * @param mixed         $submittedData Submitted data
  * @return void
  */
 public function buildField(DocumentType $document, FormInterface $form, $name, $type, array $options = [], $submittedData = null)
 {
     $options['type'] = $document->getChildForm($options['options']['data_class']);
     $options['allow_add'] = true;
     $options['allow_delete'] = true;
     $form->add($name, $type, $options);
 }
コード例 #2
0
 /**
  * @dataProvider testData
  *
  * @param string $class  class name
  * @param string $name   form name
  * @param array  $fields fields for builder
  *
  * @return void
  */
 public function testBuildForm($class, $name, $fields)
 {
     $builderDouble = $this->getMock('Symfony\\Component\\Form\\FormBuilderInterface');
     $i = 0;
     foreach ($fields as $field) {
         $builderDouble->expects($this->at($i++))->method('add')->with($field['name'], $field['type'], $field['options']);
     }
     $sut = new DocumentType($this->classMap, $this->fieldMap);
     $sut->initialize($class);
     $sut->buildForm($builderDouble, []);
     $this->assertEquals($name, $sut->getName());
 }
コード例 #3
0
ファイル: FormFieldBuilder.php プロジェクト: alebon/graviton
 /**
  * Build form field
  *
  * @param DocumentType  $document      Document type
  * @param FormInterface $form          Form
  * @param string        $name          Field name
  * @param string        $type          Field type
  * @param array         $options       Options
  * @param mixed         $submittedData Submitted data
  * @return void
  */
 public function buildField(DocumentType $document, FormInterface $form, $name, $type, array $options = [], $submittedData = null)
 {
     if (!isset($options['data_class'])) {
         $options['data_class'] = 'stdclass';
     }
     // we set "required" flag to "true" if submitted data is not null
     // because required field cannot be a child of the optional field
     if (!isset($options['required']) || !$options['required']) {
         $options['required'] = $submittedData !== null;
     }
     $form->add($name, $document->getChildForm($options['data_class']), $options);
 }
コード例 #4
0
 /**
  * @param Request $request request
  *
  * @return \Symfony\Component\Form\Form
  */
 private function getForm(Request $request)
 {
     list($service) = explode(':', $request->attributes->get('_controller'));
     $this->formType->initialize($service);
     return $this->formFactory->create($this->formType, null, array('method' => $request->getMethod()));
 }
コード例 #5
0
ファイル: Form.php プロジェクト: alebon/graviton
 /**
  * @param Request       $request request
  * @param DocumentModel $model   model
  *
  * @return \Symfony\Component\Form\Form
  */
 public function getForm(Request $request, DocumentModel $model)
 {
     $this->formType->initialize($model->getEntityClass());
     return $this->formFactory->create($this->formType, null, ['method' => $request->getMethod()]);
 }
コード例 #6
0
ファイル: DocumentTypeTest.php プロジェクト: alebon/graviton
 /**
  * Test DocumentType::handlePreSubmitEvent()
  *
  * @return void
  */
 public function testHandlePreSubmitEvent()
 {
     $class = __CLASS__;
     $fields = [['name1', 'type1', ['options1']], ['name2', 'type2', ['options2']]];
     $submittedData = ['name1' => 'data1', 'name2' => 'data2'];
     $formDouble = $this->getMockBuilder('Symfony\\Component\\Form\\FormInterface')->disableOriginalConstructor()->getMock();
     $formDouble->expects($this->once())->method('isRequired')->willReturn(true);
     $sut = new DocumentType($this->fieldBuilderDouble, [$class => $fields]);
     $sut->initialize($class);
     $this->fieldBuilderDouble->expects($this->exactly(2))->method('supportsField')->withConsecutive(['type1', ['options1']], ['type2', ['options2']])->willReturn(true);
     $this->fieldBuilderDouble->expects($this->exactly(2))->method('buildField')->withConsecutive([$sut, $formDouble, 'name1', 'type1', ['options1'], 'data1'], [$sut, $formDouble, 'name2', 'type2', ['options2'], 'data2']);
     $sut->handlePreSubmitEvent(new FormEvent($formDouble, $submittedData));
 }