コード例 #1
0
 protected function getForm($name = 'name', $propertyPath = null, $dataClass = null, $errorMapping = array(), $virtual = false, $synchronized = true)
 {
     $config = new FormConfig($name, $dataClass, $this->dispatcher, array('error_mapping' => $errorMapping));
     $config->setMapped(true);
     $config->setVirtual($virtual);
     $config->setPropertyPath($propertyPath);
     if (!$synchronized) {
         $config->addViewTransformer(new CallbackTransformer(function ($normData) {
             return $normData;
         }, function () {
             throw new TransformationFailedException();
         }));
     }
     return new Form($config);
 }
コード例 #2
0
ファイル: ChoiceList.php プロジェクト: navassouza/symfony
 /**
  * Adds a new choice.
  *
  * @param array $bucketForPreferred The bucket where to store the preferred
  *                                  view objects.
  * @param array $bucketForRemaining The bucket where to store the
  *                                  non-preferred view objects.
  * @param mixed  $choice           The choice to add.
  * @param string $label            The label for the choice.
  * @param array  $preferredChoices The preferred choices.
  */
 protected function addChoice(&$bucketForPreferred, &$bucketForRemaining, $choice, $label, array $preferredChoices)
 {
     $index = $this->createIndex($choice);
     if ('' === $index || null === $index || !FormConfig::isValidName((string) $index)) {
         throw new InvalidConfigurationException('The index "' . $index . '" created by the choice list is invalid. It should be a valid, non-empty Form name.');
     }
     $value = $this->createValue($choice);
     if (!is_string($value)) {
         throw new InvalidConfigurationException('The value created by the choice list is of type "' . gettype($value) . '", but should be a string.');
     }
     $view = new ChoiceView($value, $label);
     $this->choices[$index] = $this->fixChoice($choice);
     $this->values[$index] = $value;
     if ($this->isPreferred($choice, $preferredChoices)) {
         $bucketForPreferred[$index] = $view;
     } else {
         $bucketForRemaining[$index] = $view;
     }
 }
コード例 #3
0
 public function testMapFormsToDataSkipsVirtualForms()
 {
     $car = new \stdClass();
     $engine = new \stdClass();
     $parentPath = $this->getPropertyPath('name');
     $childPath = $this->getPropertyPath('engine');
     $parentPath->expects($this->never())->method('getValue');
     $parentPath->expects($this->never())->method('setValue');
     $childPath->expects($this->once())->method('setValue')->with($car, $engine);
     $config = new FormConfig('name', '\\stdClass', $this->dispatcher);
     $config->setPropertyPath($parentPath);
     $config->setVirtual(true);
     $config->setCompound(true);
     $config->setDataMapper($this->getDataMapper());
     $form = $this->getForm($config);
     $config = new FormConfig('engine', '\\stdClass', $this->dispatcher);
     $config->setByReference(true);
     $config->setPropertyPath($childPath);
     $config->setData($engine);
     $child = $this->getForm($config);
     $form->add($child);
     $this->mapper->mapFormsToData(array($form), $car);
 }
コード例 #4
0
ファイル: FormBuilder.php プロジェクト: nathanlon/symfony
 /**
  * Creates a new form builder.
  *
  * @param string                   $name
  * @param string                   $dataClass
  * @param EventDispatcherInterface $dispatcher
  * @param FormFactoryInterface     $factory
  */
 public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, FormFactoryInterface $factory, array $options = array())
 {
     parent::__construct($name, $dataClass, $dispatcher, $options);
     $this->factory = $factory;
 }
コード例 #5
0
ファイル: FormTest.php プロジェクト: laubosslink/lab
 /**
  * @expectedException Symfony\Component\Form\Exception\FormException
  */
 public function testClientDataMustBeObjectIfDataClassIsSet()
 {
     $config = new FormConfig('name', 'stdClass', $this->dispatcher);
     $config->addViewTransformer(new FixedDataTransformer(array('' => '', 'foo' => array('bar' => 'baz'))));
     $form = new Form($config);
     $form->setData('foo');
 }
コード例 #6
0
 public function testBindEmptyGetRequestToSimpleForm()
 {
     if (!class_exists('Symfony\\Component\\HttpFoundation\\Request')) {
         $this->markTestSkipped('The "HttpFoundation" component is not available');
     }
     $request = new Request(array(), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET'));
     $dispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $config = new FormConfig('author', null, $dispatcher);
     $config->setCompound(false);
     $form = new Form($config);
     $event = new FormEvent($form, $request);
     $listener = new BindRequestListener();
     $listener->preBind($event);
     $this->assertNull($event->getData());
 }