protected function setUp()
 {
     $this->dispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $this->factory = $this->getMock('Symfony\\Component\\Form\\FormFactoryInterface');
     $this->builder = new TestFormBuilder('name', null, $this->dispatcher, $this->factory);
     $this->factory->expects($this->any())->method('createNamedBuilder')->willReturn($this->builder);
 }
Exemple #2
0
 /**
  * @param mixed|null $data
  * @param array      $options
  *
  * @return \Symfony\Component\Form\FormInterface
  */
 public function createForm($data = null, array $options = [])
 {
     if ($data != null) {
         $this->setData($data);
     }
     return $this->formFactory->create($this->type, null, array_merge($this->options, $options));
 }
 public function switchLocaleAction(Request $request)
 {
     $this->guard->userIsLoggedIn();
     $this->logger->notice('User requested to switch locale');
     $returnUrl = $request->query->get('return-url');
     // Return URLs generated by us always include a path (ie. at least a forward slash)
     // @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L878
     $domain = $request->getSchemeAndHttpHost() . '/';
     if (strpos($returnUrl, $domain) !== 0) {
         $this->logger->error(sprintf('Illegal return-url ("%s") for redirection after changing locale, aborting request', $returnUrl));
         throw new BadRequestHttpException('Invalid return-url given');
     }
     $command = new ChangeLocaleCommand();
     $form = $this->formFactory->create('profile_switch_locale', $command, [])->handleRequest($request);
     $this->logger->notice(sprintf('Switching locale from "%s" to "%s"', $request->getLocale(), $command->newLocale));
     if ($form->isValid()) {
         $this->userService->changeLocale($command);
         $this->flashBag->add('success', 'profile.locale.locale_change_success');
         $this->logger->notice(sprintf('Successfully switched locale from "%s" to "%s"', $request->getLocale(), $command->newLocale));
     } else {
         $this->flashBag->add('error', 'profile.locale.locale_change_fail');
         $this->logger->error('Locale not switched: the switch locale form contained invalid data');
     }
     return new RedirectResponse($returnUrl);
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function create($namespace, $data = null, array $options = [])
 {
     $schema = $this->schemaRegistry->getSchema($namespace);
     $builder = $this->formFactory->createBuilder('form', $data, array_merge_recursive(['data_class' => null], $options));
     $schema->buildForm($builder);
     return $builder->getForm();
 }
 /**
  * {@inheritDoc}
  */
 public function getForm()
 {
     if (!$this->form) {
         $this->form = $this->formFactory->create($this->getFormType(), [], array_merge($this->getOr(FilterUtility::FORM_OPTIONS_KEY, []), ['csrf_protection' => false]));
     }
     return $this->form;
 }
Exemple #6
0
 /**
  * @param array $options
  *
  * @return FormView
  */
 public function getItemFormView(array $options = array())
 {
     $cartItem = $this->cartItemFactory->createNew();
     $this->orderItemQuantityModifier->modify($cartItem, 1);
     $form = $this->formFactory->create('sylius_cart_item', $cartItem, $options);
     return $form->createView();
 }
 /**
  * @param Location $location
  * @return \Symfony\Component\Form\FormInterface
  * @throws \Heliopsis\eZFormsBundle\Exceptions\UnknownFormException
  */
 public function getForm(Location $location)
 {
     if (!isset($this->map[$location->contentInfo->remoteId])) {
         throw new UnknownFormException(sprintf("No form could be mapped to content remote id %s", $location->contentInfo->remoteId));
     }
     return $this->formFactory->create($this->map[$location->contentInfo->remoteId]);
 }
Exemple #8
0
 public function testSubmit()
 {
     $this->translatableResource->expects($this->once())->method('getModel')->will($this->returnValue(TranslatableTest::class));
     $this->translatableResource->expects($this->once())->method('getRelation')->with($this->identicalTo('translation'))->will($this->returnValue($translationResource = $this->createResourceMock()));
     $translationResource->expects($this->once())->method('getForm')->will($this->returnValue(TranslationTestType::class));
     $this->translatableFactory->expects($this->once())->method('create')->will($this->returnValue(new TranslatableTest()));
     $form = $this->formFactory->create(TranslatableTestType::class)->submit(['translations' => ['en' => ['value' => 'value-en'], 'fr' => ['value' => 'value-fr']]]);
     $translatable = $form->getData();
     $view = $form->createView();
     $this->assertInstanceOf(TranslatableTest::class, $translatable);
     $this->assertCount(1, $view->children);
     $this->assertArrayHasKey('translations', $view->children);
     $this->assertCount(2, $translatable->getTranslations());
     $this->assertTrue($translatable->getTranslations()->containsKey('en'));
     $this->assertTrue($translatable->getTranslations()->containsKey('fr'));
     $this->assertCount(2, $view->children['translations']);
     $this->assertArrayHasKey('en', $view->children['translations']);
     $this->assertArrayHasKey('fr', $view->children['translations']);
     $this->assertInstanceOf(TranslationTest::class, $translatable->getTranslations()['en']);
     $this->assertSame('value-en', $translatable->getTranslations()['en']->getValue());
     $this->assertCount(1, $view->children['translations']['en']);
     $this->assertArrayHasKey('value', $view->children['translations']['en']);
     $this->assertInstanceOf(TranslationTest::class, $translatable->getTranslations()['fr']);
     $this->assertSame('value-fr', $translatable->getTranslations()['fr']->getValue());
     $this->assertCount(1, $view->children['translations']['fr']);
     $this->assertArrayHasKey('value', $view->children['translations']['fr']);
 }
 /**
  *
  * @param Request $request
  * @param AbstractType|string $formType
  * @param Query $query
  * @param string $sessionKey
  * @param array $formOptions
  * @return array
  */
 public function process(Request $request, $formType, $query, $sessionKey, array $formOptions = array())
 {
     $filterForm = $this->formFactory->create($formType, null, $formOptions);
     $method = $filterForm->getConfig()->getMethod();
     $requestData = $method == 'GET' ? $request->query->get($filterForm->getName()) : $request->request->get($filterForm->getName());
     if (isset($requestData['reset'])) {
         if ($method == 'POST') {
             $request->getSession()->remove($sessionKey);
             return array($filterForm, null, true);
         }
         $request->getSession()->set($sessionKey, array());
         $requestData = array();
     }
     $filterData = empty($requestData) ? $request->getSession()->get($sessionKey, $requestData) : $requestData;
     if (!empty($filterData)) {
         $this->applyFilter($filterForm, $filterData, $query);
         if (empty($filterData)) {
             $request->getSession()->remove($sessionKey);
         } else {
             $request->getSession()->set($sessionKey, $filterData);
         }
     }
     $this->updatePerPage($request, $sessionKey);
     $page = $request->query->get($this->knpParams['pageParameterName'], 1);
     $event = $this->eventDispatcher->dispatch(FilterEvent::POST_FILTER, new FilterEvent($query));
     if ($event->hasNewQuery()) {
         $query = $event->getNewQuery();
     }
     return array($filterForm, $this->paginator->paginate($query, $page, $this->perPage, $this->knpParams), false);
 }
 /**
  * Gets the form
  *
  * @param  \Sonata\AdminBundle\Util\AdminObjectAclData $data
  * @return \Symfony\Component\Form\Form
  */
 public function createForm(AdminObjectAclData $data)
 {
     // Retrieve object identity
     $objectIdentity = ObjectIdentity::fromDomainObject($data->getObject());
     $acl = $data->getSecurityHandler()->getObjectAcl($objectIdentity);
     if (!$acl) {
         $acl = $data->getSecurityHandler()->createAcl($objectIdentity);
     }
     $data->setAcl($acl);
     $masks = $data->getMasks();
     // Create a form to set ACL
     $formBuilder = $this->formFactory->createBuilder('form');
     foreach ($data->getAclUsers() as $aclUser) {
         $securityIdentity = UserSecurityIdentity::fromAccount($aclUser);
         foreach ($data->getUserPermissions() as $permission) {
             try {
                 $checked = $acl->isGranted(array($masks[$permission]), array($securityIdentity));
             } catch (NoAceFoundException $e) {
                 $checked = false;
             }
             $formBuilder->add($aclUser->getId() . $permission, 'checkbox', array('required' => false, 'data' => $checked));
         }
     }
     $form = $formBuilder->getForm();
     $data->setForm($form);
     return $form;
 }
Exemple #11
0
 public function testIcon()
 {
     $form = $this->factory->create(FormType::class, null, ['icon' => $icon = 'my.icon']);
     $view = $form->createView();
     $this->assertArrayHasKey('icon', $view->vars);
     $this->assertSame($icon, $view->vars['icon']);
 }
 /**
  * @test
  */
 public function couldBeCreatedByFormFactory()
 {
     $form = $this->formFactory->create('payum_gateway_config', null, array('data_class' => 'Payum\\Core\\Model\\GatewayConfig'));
     $view = $form->createView();
     $this->assertInstanceOf('Symfony\\Component\\Form\\FormInterface', $form);
     $this->assertInstanceOf('Symfony\\Component\\Form\\FormView', $view);
 }
 public function testLabelTranslationArguments()
 {
     $form = $this->factory->create(ButtonType::class, null, ['label_translation_arguments' => $arguments = ['foo' => 'bar']]);
     $view = $form->createView();
     $this->assertArrayHasKey('label_translation_arguments', $view->vars);
     $this->assertSame($arguments, $view->vars['label_translation_arguments']);
 }
 /**
  * @test
  */
 public function couldBeCreatedByFormFactory()
 {
     $form = $this->formFactory->create(GatewayFactoriesChoiceType::class);
     $view = $form->createView();
     $this->assertInstanceOf(FormInterface::class, $form);
     $this->assertInstanceOf(FormView::class, $view);
 }
 function it_creates_the_object_if_form_is_a_class_name(RequestConfiguration $requestConfiguration, ResourceInterface $resource, FormFactoryInterface $formFactory, FormInterface $form)
 {
     $requestConfiguration->isHtmlRequest()->willReturn(true);
     $requestConfiguration->getFormType()->willReturn(TextType::class);
     $formFactory->create(Argument::type(TextType::class), $resource)->willReturn($form);
     $this->create($requestConfiguration, $resource)->shouldReturn($form);
 }
 function it_gets_form_from_class_name(Configuration $configuration, FormFactoryInterface $formFactory, FormInterface $form)
 {
     $formClass = 'spec\\Sylius\\Bundle\\ResourceBundle\\Controller\\TestFormType';
     $configuration->getFormType()->willReturn($formClass);
     $formFactory->create(Argument::type($formClass), Argument::cetera())->shouldBeCalled()->willReturn($form);
     $this->getForm()->shouldReturn($form);
 }
 /**
  * {@inheritdoc}
  */
 public function create($namespace)
 {
     $schema = $this->schemaRegistry->getSchema($namespace);
     $builder = $this->formFactory->createBuilder('form', null, array('data_class' => null));
     $schema->buildForm($builder);
     return $builder->getForm();
 }
 /**
  * {@inheritdoc}
  */
 public function getFormBuilder(FormFactoryInterface $factory)
 {
     if (!isset($this->formBuilder)) {
         $this->formBuilder = $factory->createNamedBuilder($this->getResourceProperty(), $this->getFormType(), null, $this->buildFormOptions());
     }
     return $this->formBuilder;
 }
 /**
  * @test
  */
 public function couldBeCreatedByFormFactory()
 {
     $form = $this->formFactory->create('payum_gateway_factories_choice');
     $view = $form->createView();
     $this->assertInstanceOf('Symfony\\Component\\Form\\FormInterface', $form);
     $this->assertInstanceOf('Symfony\\Component\\Form\\FormView', $view);
 }
Exemple #20
0
 public function testSetInvalidYearsOption()
 {
     $help = 'Having trouble with the internets? Tell us about it.';
     $form = $this->factory->createNamed('__test___field', $this->getTestedType(), null, ['help' => $help]);
     $view = $form->createView();
     $this->assertSame($help, $view->vars['help']);
 }
 /**
  * {@inheritdoc}
  */
 public function create($pollId)
 {
     $type = $this->formFactory->getType($this->type);
     $type->setPollFields($pollId);
     $builder = $this->formFactory->createNamedBuilder(sprintf('%s_%s', $this->name, $pollId), $type);
     return $builder->getForm();
 }
 /**
  * @param FormInterface $form
  * @param string        $ruleType
  * @param array         $data
  */
 protected function addConfigurationFields(FormInterface $form, $ruleType, array $data = [])
 {
     /** @var RuleCheckerInterface $checker */
     $checker = $this->checkerRegistry->get($ruleType);
     $configurationField = $this->factory->createNamed('configuration', $checker->getConfigurationFormType(), $data, ['auto_initialize' => false]);
     $form->add($configurationField);
 }
 public function testSubmit()
 {
     $choice = new \stdClass();
     $choice->{$identifierPath = 'id'} = $id = 1;
     $choice->{$labelPath = 'label'} = $label = 'label';
     $this->resource->expects($this->once())->method('getModel')->will($this->returnValue($model = get_class($choice)));
     $this->resource->expects($this->once())->method('getIdPropertyPath')->will($this->returnValue($identifierPath));
     $this->resource->expects($this->once())->method('getLabelPropertyPath')->will($this->returnValue($labelPath));
     $this->managerRegistry->expects($this->once())->method('getManagerForClass')->with($this->identicalTo($model))->will($this->returnValue($documentManager = $this->createDocumentManagerMock()));
     $documentManager->expects($this->once())->method('getRepository')->with($this->identicalTo($model))->will($this->returnValue($repository = $this->createRepositoryMock()));
     $documentManager->expects($this->exactly(2))->method('getClassMetadata')->with($this->identicalTo($model))->will($this->returnValue($classMetadata = $this->createClassMetadataMock()));
     $classMetadata->expects($this->once())->method('getName')->will($this->returnValue($model));
     $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue([$identifier = $identifierPath]));
     $classMetadata->expects($this->once())->method('getTypeOfField')->with($this->identicalTo($identifier))->will($this->returnValue('integer'));
     $repository->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock($documentManager)));
     $queryBuilder->expects($this->once())->method('getQuery')->will($this->returnValue($query = $this->createQueryMock()));
     $query->expects($this->once())->method('execute')->will($this->returnValue($iterator = $this->createIteratorMock()));
     $iterator->expects($this->once())->method('toArray')->will($this->returnValue([$choice]));
     $form = $this->factory->create(ResourceChoiceType::class, null, ['resource' => $this->resource])->submit($id);
     $this->assertSame($this->resource, $form->getConfig()->getOption('resource'));
     $this->assertSame($identifierPath, $form->getConfig()->getOption('choice_value'));
     $this->assertSame($labelPath, $form->getConfig()->getOption('choice_label'));
     $this->assertSame('', $form->getConfig()->getOption('placeholder'));
     $this->assertSame($choice, $form->getData());
     $form->createView();
 }
 /**
  * @return FormBuilderInterface
  */
 protected function getFormBuilder()
 {
     if (!$this->formBuilder) {
         $this->formBuilder = $this->formFactory->createBuilder($this->getFormType(), [], array_merge($this->getOr(FilterUtility::FORM_OPTIONS_KEY, []), ['csrf_protection' => false]));
     }
     return $this->formBuilder;
 }
Exemple #25
0
 public function invoke(Request $request)
 {
     $type = new LoginFormType($request);
     $builder = $this->formFactory->createBuilder($type, [], ['action' => $this->checkPath]);
     $form = $builder->getForm();
     return ['form' => $form->createView()];
 }
 /**
  * @param Request $request
  *
  * @return JsonResponse
  */
 public function batchUpdateAction(Request $request)
 {
     if (!$this->isAuthorized()) {
         return new JsonResponse(null, JsonResponse::HTTP_FORBIDDEN);
     }
     $form = $this->formFactory->create(BatchType::class);
     $form->submit($this->getDataFromJsonRequest($request));
     if (!$form->isValid()) {
         $errors = $this->getErrors($form);
         return $this->createErrorResponse($errors);
     }
     $errors = [];
     $contents = [];
     foreach ($form->getData()->contents as $data) {
         $content = $this->manager->find($data->id);
         if (null === $content) {
             $errors[] = $this->createContentNotFoundError($data->id);
             continue;
         }
         $data->update($content);
         $contents[] = $content;
     }
     if (0 !== count($errors)) {
         return $this->createErrorResponse($errors);
     }
     foreach ($contents as $content) {
         $this->manager->update($content);
     }
     return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
 }
 public function testSubmit()
 {
     $form = $this->formFactory->create(get_class($this->resourceType))->submit([]);
     $this->assertSame($this->resource, $form->getConfig()->getOption('resource'));
     $this->assertSame($this->resourceFactory, $form->getConfig()->getOption('factory'));
     $form->createView();
 }
 /**
  * {@inheritdoc}
  */
 public function getForm($group)
 {
     $block = $this->getSubtree($group);
     $toAdd = array();
     $bc = $block->toBlockConfig();
     if (!$block->isEmpty()) {
         $sbc = array();
         /** @var $subblock GroupNodeDefinition */
         foreach ($block as $subblock) {
             $sbc += $subblock->toBlockConfig();
             if (!$subblock->isEmpty()) {
                 /** @var $field FieldNodeDefinition */
                 foreach ($subblock as $field) {
                     $field->replaceOption('block', $block->getName())->replaceOption('subblock', $subblock->getName());
                     $toAdd[] = $field;
                 }
             }
         }
         $bc[$block->getName()]['subblocks'] = $sbc;
     }
     $fb = $this->factory->createNamedBuilder($group, 'oro_config_form_type', null, array('block_config' => $bc));
     foreach ($toAdd as $field) {
         $this->addFieldToForm($fb, $field);
     }
     return $fb->getForm();
 }
 /**
  * @param FormTypeInterface $type
  * @param mixed $data
  * @param array $options
  * @return FormInterface
  * @throws MissingDependencyException
  */
 public function createForm(FormTypeInterface $type, $data = null, array $options = array())
 {
     if (null === $this->formFactory) {
         throw new MissingDependencyException('No form factory present.');
     }
     return $this->formFactory->create($type, $data, $options);
 }
Exemple #30
0
 public function getForm(ResourceInterface $resource)
 {
     $resourceClassName = \Doctrine\Common\Util\ClassUtils::getClass($resource);
     $resourceParts = explode("\\", $resourceClassName);
     $resourceParts[count($resourceParts) - 2] = 'Form';
     $resourceParts[count($resourceParts) - 1] .= 'Type';
     $formType = implode("\\", $resourceParts);
     if (class_exists($formType)) {
         return $this->formFactory->create(new $formType(), $resource);
     }
     $options = array('data_class' => $resourceClassName);
     $builder = $this->formFactory->createBuilder('form', $resource, $options);
     $reflectionClass = new \ReflectionClass($resourceClassName);
     $annotationClass = 'uebb\\HateoasBundle\\Annotation\\FormField';
     foreach ($reflectionClass->getProperties() as $propertyReflection) {
         /**
          * @var \uebb\HateoasBundle\Annotation\FormField $annotation
          */
         $annotation = $this->annotationReader->getPropertyAnnotation($propertyReflection, $annotationClass);
         if ($annotation) {
             $builder->add($propertyReflection->getName(), $annotation->type, is_array($annotation->options) ? $annotation->options : array());
         }
     }
     $form = $builder->getForm();
     return $form;
 }