public function testbuildViewWithWithSonataAdmin()
 {
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin->expects($this->once())->method('getCode')->will($this->returnValue('my.admin.reference'));
     $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $formView = new FormView();
     $options = array();
     $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
     $config->setAttribute('sonata_admin', array('admin' => $admin, 'name' => 'name'));
     $config->setAttribute('sonata_admin_enabled', true);
     $form = new Form($config);
     $formView->vars['block_prefixes'] = array('form', 'field', 'text', '_s50b26aa76cb96_username');
     $extension = new FormTypeFieldExtension();
     $extension->buildView($formView, $form, array());
     $this->assertArrayHasKey('block_prefixes', $formView->vars);
     $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
     $this->assertArrayHasKey('sonata_admin', $formView->vars);
     $expected = array('form', 'field', 'text', 'my_admin_reference_text', 'my_admin_reference_name_text', 'my_admin_reference_name_text_username');
     $this->assertEquals($expected, $formView->vars['block_prefixes']);
     $this->assertTrue($formView->vars['sonata_admin_enabled']);
 }
 protected function getForm($name = 'name', $propertyPath = null, $dataClass = null, $errorMapping = array(), $virtual = false, $synchronized = true)
 {
     $config = new FormConfigBuilder($name, $dataClass, $this->dispatcher, array('error_mapping' => $errorMapping));
     $config->setMapped(true);
     $config->setVirtual($virtual);
     $config->setPropertyPath($propertyPath);
     $config->setCompound(true);
     $config->setDataMapper($this->getDataMapper());
     if (!$synchronized) {
         $config->addViewTransformer(new CallbackTransformer(function ($normData) {
             return $normData;
         }, function () {
             throw new TransformationFailedException();
         }));
     }
     return new Form($config);
 }
 /**
  * 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.
  *
  * @throws InvalidConfigurationException If no valid value or index could be created.
  */
 protected function addChoice(array &$bucketForPreferred, array &$bucketForRemaining, $choice, $label, array $preferredChoices, $level = 0)
 {
     $index = $this->createIndex($choice);
     if ('' === $index || null === $index || !FormConfigBuilder::isValidName((string) $index)) {
         throw new InvalidConfigurationException(sprintf('The index "%s" created by the choice list is invalid. It should be a valid, non-empty Form name.', $index));
     }
     $value = $this->createValue($choice);
     if (!is_string($value)) {
         throw new InvalidConfigurationException(sprintf('The value created by the choice list is of type "%s", but should be a string.', gettype($value)));
     }
     $view = new TreeChoiceView($choice, $value, $label, $level);
     $this->choices[$index] = $this->fixChoice($choice);
     $this->values[$index] = $value;
     if ($this->isPreferred($choice, $preferredChoices)) {
         $bucketForPreferred[$index] = $view;
     } else {
         $bucketForRemaining[$index] = $view;
     }
 }
Example #4
0
 /**
  * Get form name
  *
  * @return string
  */
 public function getFormName()
 {
     if (FormConfigBuilder::isValidName($this->exposedName)) {
         return $this->exposedName;
     }
     $name = $this->exposedName;
     $name = preg_replace('/[^a-zA-Z0-9_]/', '', $name);
     $name = preg_replace('/[^a-zA-Z0-9_\\-:]/', '', $name);
     return $name === '' ? $this->fieldName : $name;
 }
 public function testSubmitEmptyGetRequestToSimpleForm()
 {
     $request = new Request(array(), array(), array(), array(), array(), array('REQUEST_METHOD' => 'GET'));
     $dispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $config = new FormConfigBuilder('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());
 }
Example #6
0
 /**
  * @expectedException \Symfony\Component\Form\Exception\RuntimeException
  */
 public function testSetDataCannotInvokeItself()
 {
     // Cycle detection to prevent endless loops
     $config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
     $config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
         $event->getForm()->setData('bar');
     });
     $form = new Form($config);
     $form->setData('foo');
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function getFormConfig()
 {
     /** @var $config self */
     $config = parent::getFormConfig();
     $config->children = array();
     $config->unresolvedChildren = array();
     return $config;
 }
Example #8
0
 /**
  * Creates a new form builder.
  *
  * @param string                   $name
  * @param string                   $dataClass
  * @param EventDispatcherInterface $dispatcher
  * @param FormFactoryInterface     $factory
  * @param array                    $options
  */
 public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, FormFactoryInterface $factory, array $options = array())
 {
     parent::__construct($name, $dataClass, $dispatcher, $options);
     $this->setFormFactory($factory);
 }
 public function testMapFormsToDataIgnoresDisabled()
 {
     $car = new \stdClass();
     $engine = new \stdClass();
     $propertyPath = $this->getPropertyPath('engine');
     $this->propertyAccessor->expects($this->never())->method('setValue');
     $config = new FormConfigBuilder('name', '\\stdClass', $this->dispatcher);
     $config->setByReference(true);
     $config->setPropertyPath($propertyPath);
     $config->setData($engine);
     $config->setDisabled(true);
     $form = $this->getForm($config);
     $this->mapper->mapFormsToData(array($form), $car);
 }
 public function testbuildViewWithNestedForm()
 {
     $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $formView = new FormView();
     $formView->vars['name'] = 'format';
     $options = array();
     $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
     $config->setAttribute('sonata_admin', array('admin' => false));
     $form = new Form($config);
     $formView->parent = new FormView();
     $formView->parent->vars['sonata_admin_enabled'] = true;
     $formView->parent->vars['sonata_admin_code'] = 'parent_code';
     $formView->parent->vars['name'] = 'settings';
     $formView->vars['block_prefixes'] = array('form', 'field', 'text', '_s50b26aa76cb96_settings_format');
     $extension = new FormTypeFieldExtension(array(), array());
     $extension->buildView($formView, $form, array('sonata_help' => 'help text'));
     $this->assertArrayHasKey('block_prefixes', $formView->vars);
     $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
     $this->assertArrayHasKey('sonata_admin', $formView->vars);
     $expected = array('value' => null, 'attr' => array(), 'name' => 'format', 'block_prefixes' => array('form', 'field', 'text', 'parent_code_text', 'parent_code_text_settings_format', 'parent_code_text_settings_settings_format'), 'sonata_admin_enabled' => true, 'sonata_admin' => array('admin' => false, 'field_description' => false, 'name' => false, 'edit' => 'standard', 'inline' => 'natural', 'block_name' => false, 'class' => false, 'options' => array()), 'sonata_admin_code' => 'parent_code');
     $this->assertEquals($expected, $formView->vars);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function getFormConfig()
 {
     $config = parent::getFormConfig();
     $config->parent = null;
     $config->children = array();
     $config->unresolvedChildren = array();
     return $config;
 }
Example #12
0
 public function testMapFormsToDataSkipsVirtualForms()
 {
     $car = new \stdClass();
     $engine = new \stdClass();
     $parentPath = $this->getPropertyPath('name');
     $childPath = $this->getPropertyPath('engine');
     // getValue() and setValue() must never be invoked for $parentPath
     $this->propertyAccessor->expects($this->once())->method('getValue')->with($car, $childPath);
     $this->propertyAccessor->expects($this->once())->method('setValue')->with($car, $childPath, $engine);
     $config = new FormConfigBuilder('name', '\\stdClass', $this->dispatcher);
     $config->setPropertyPath($parentPath);
     $config->setVirtual(true);
     $config->setCompound(true);
     $config->setDataMapper($this->getDataMapper());
     $form = $this->getForm($config);
     $config = new FormConfigBuilder('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);
 }
 public function testFormErrorsWithNonFormComponents()
 {
     if (!class_exists(SubmitType::class)) {
         $this->markTestSkipped('Not using Symfony Form >= 2.3 with submit type');
     }
     $dispatcher = $this->createMock(EventDispatcherInterface::class);
     $factoryBuilder = new FormFactoryBuilder();
     $factoryBuilder->addType(new SubmitType());
     $factoryBuilder->addType(new ButtonType());
     $factory = $factoryBuilder->getFormFactory();
     $formConfigBuilder = new FormConfigBuilder('foo', null, $dispatcher);
     $formConfigBuilder->setFormFactory($factory);
     $formConfigBuilder->setCompound(true);
     $formConfigBuilder->setDataMapper($this->createMock(DataMapperInterface::class));
     $fooConfig = $formConfigBuilder->getFormConfig();
     $form = new Form($fooConfig);
     $form->add('save', method_exists(AbstractType::class, 'getBlockPrefix') ? SubmitType::class : 'submit');
     $this->serialize($form);
     $this->assertTrue(true);
     // Exception not thrown
 }
 protected function addTerms(Terms $terms)
 {
     $index = $this->createIndex($terms);
     if ('' === $index || null === $index || !FormConfigBuilder::isValidName((string) $index)) {
         throw new InvalidConfigurationException(sprintf('The index "%s" created by the choice list is invalid. It should be a valid, non-empty Form name.', $index));
     }
     $value = $this->createValue($terms);
     $label = $this->createLabel($terms);
     $view = new ChoiceView($terms, $value, $label);
     $this->choices[$index] = $terms;
     $this->values[$index] = $value;
     if ($terms->isFinal()) {
         $this->final[$index] = $view;
     } else {
         $this->drafts[$index] = $view;
     }
 }
 public function testSubmitEmptyGetRequestToSimpleForm()
 {
     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 FormConfigBuilder('author', null, $dispatcher);
     $config->setCompound(false);
     $form = new Form($config);
     $event = new FormEvent($form, $request);
     $listener = new BindRequestListener();
     DeprecationErrorHandler::preBind($listener, $event);
     $this->assertNull($event->getData());
 }