/**
  *
  * @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);
 }
 public function getLocalePreferenceForm($currentLocale, $route, array $routeParameters = [])
 {
     $command = new SwitchLocaleCommand();
     $command->locale = $currentLocale;
     $form = $this->formFactory->create('stepup_switch_locale', $command, ['route' => $route, 'route_parameters' => $routeParameters]);
     return $form->createView();
 }
Beispiel #3
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']);
 }
 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();
 }
Beispiel #5
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;
 }
 /**
  * @test
  */
 public function couldBeCreatedByFormFactory()
 {
     $form = $this->formFactory->create(GatewayConfigType::class, null, array('data_class' => GatewayConfig::class));
     $view = $form->createView();
     $this->assertInstanceOf(FormInterface::class, $form);
     $this->assertInstanceOf(FormView::class, $view);
 }
 /**
  * @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);
 }
 /**
  * @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);
 }
Beispiel #9
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));
 }
 /**
  * @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]);
 }
Beispiel #11
0
 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();
 }
 /**
  * @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);
 }
Beispiel #13
0
 public function testLabelAdd()
 {
     $form = $this->factory->create(CollectionType::class, [], ['allow_add' => true, 'label_add' => $label = 'foo'])->submit([]);
     $view = $form->createView();
     $this->assertArrayHasKey('label_add', $view->vars);
     $this->assertSame($label, $view->vars['label_add']);
 }
 /**
  * {@inheritdoc}
  */
 public function handle(Request $request, FormHandlerInterface $handler, FormInterface $form = null)
 {
     if (null !== $form) {
         $handler->setForm($form);
     } elseif (null === ($form = $handler->getForm())) {
         if ($handler instanceof NamedFormHandlerInterface && null !== ($name = $handler->getName())) {
             $form = $this->form_factory->createNamed($name, $handler->getType(), $handler->getData(), $handler->getOptions());
         } else {
             $form = $this->form_factory->create($handler->getType(), $handler->getData(), $handler->getOptions());
         }
         if (!$form instanceof FormInterface) {
             throw new FormNotFoundException($handler);
         }
         // set the form which is associated with the handler
         $handler->setForm($form);
     }
     $form->handleRequest($request);
     if (!$form->isSubmitted()) {
         return null;
     }
     if ($form->isValid()) {
         if ($handler instanceof FormSuccessHandlerInterface) {
             return $handler->onSuccess($request);
         }
     } elseif ($handler instanceof FormFailureHandlerInterface) {
         return $handler->onFailure($request);
     }
 }
Beispiel #15
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']);
 }
 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);
 }
 /**
  * @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);
 }
 /**
  * {@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;
 }
Beispiel #21
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();
 }
 /**
  * @test
  */
 public function shouldHideDaySelectAndSetDayFromGivenDateTimeAsValue()
 {
     $date = new \DateTime('2020-01-10');
     $form = $this->formFactory->create(CreditCardExpirationDateType::class, $date, array('widget' => 'choice', 'input' => 'datetime', 'csrf_protection' => false));
     $view = $form->createView();
     $this->assertEquals(array('style' => 'display:none'), $view['day']->vars['attr']);
     $this->assertEquals(10, $view['day']->vars['value']);
 }
 public function getMediacenterForm(Mediacenter $mediacenter = null)
 {
     if ($mediacenter === null) {
         $mediacenter = $this->getMediacenterOrEmpty();
     }
     $form = $this->formFactory->create('inwicast_plugin_type_mediacenter', $mediacenter);
     return $form;
 }
 public function loginAction(Request $request)
 {
     $form = $this->formFactory->create(LoginType::class);
     if ($error = $this->getAuthenticationError($request)) {
         $form->addError(new FormError($error->getMessage()));
     }
     return $this->templating->renderResponse('Security/login.html.twig', ['form' => $form->createView()]);
 }
Beispiel #25
0
 public function render($options)
 {
     $name = $this->getOption('name', $options, 'contact');
     $configuration = $this->configurationFactory->create($name);
     $template = $this->getOption('template', $options, $configuration->getFormTemplate());
     $form = $formFactory = $this->formFactory->create($configuration->getFormName());
     return $this->renderTemplate($template, array('form' => $form->createView(), 'name' => $name));
 }
 /**
  * @param NotificationPluginConfiguration $config
  *
  * @return \Symfony\Component\Form\FormInterface
  */
 public function getForm(NotificationPluginConfiguration $config = null)
 {
     if ($config === null) {
         $config = $this->getConfigOrEmpty();
     }
     $form = $this->formFactory->create('icap_notification_type_pluginConfiguration', $config);
     return $form;
 }
Beispiel #27
0
 /**
  * @param array $options
  *
  * @return FormView
  */
 public function getItemFormView(array $options = [])
 {
     /** @var OrderItemInterface $cartItem */
     $cartItem = $this->orderItemFactory->createNew();
     $this->orderItemQuantityModifier->modify($cartItem, 1);
     $form = $this->formFactory->create(CartItemType::class, $cartItem, $options);
     return $form->createView();
 }
 /**
  * @param CreateAddressCommand $createAddressCommand
  * @return Form The form
  */
 public function createCreateForm(CreateAddressCommand $createAddressCommand = null)
 {
     $walletChoices = $this->generateWalletHtmlSelectChoices();
     $defaultSelectedWalletId = $createAddressCommand->getWalletId();
     $form = $this->formFactory->create(new CreateAddressType($walletChoices, $defaultSelectedWalletId), $createAddressCommand, array('action' => $this->router->generate('bc_app_wallet_address.generate', array('walletId' => $defaultSelectedWalletId)), 'method' => 'POST', 'csrf_protection' => true));
     //$form->add('submit', 'submit'); // Using bootstrap button
     return $form;
 }
Beispiel #29
0
 public function testSubmit()
 {
     $this->repository->expects($this->once())->method('findAll')->will($this->returnValue([$locale = $this->createLocaleMock()]));
     $locale->expects($this->once())->method('getCode')->will($this->returnValue('fr'));
     $form = $this->factory->create(LocaleCodeType::class, $code = 'en')->submit($code);
     $this->assertSame($code, $form->getData());
     $this->assertCount(562, $form->createView()->vars['choices']);
 }
Beispiel #30
0
 public function testSubmit()
 {
     $form = $this->factory->create(CsrfProtectionType::class)->submit([]);
     $view = $form->createView();
     $this->assertArrayHasKey('submit', $view->children);
     $this->assertArrayHasKey('clicked', $view->children['submit']->vars);
     $this->assertSame([[$this->flashCsrfProtectionSubscriber, 'onPostSubmit']], $form->getConfig()->getEventDispatcher()->getListeners(FormEvents::POST_SUBMIT));
 }