public function it_return_json_with_error_message_when_form_data_is_not_valid_json(Request $request, Form $composerForm, FormError $composerFormError) { $composerForm->handleRequest($request)->shouldBeCalled(); $composerForm->isValid()->shouldBeCalled()->willReturn(false); $composerForm->isValid()->shouldBeCalled()->willReturn(false); $composerForm->get('body')->shouldBeCalled()->willReturn($composerForm); $composerForm->getErrors()->shouldBeCalled()->willReturn(array($composerFormError)); $composerFormError->getMessage()->shouldBeCalled()->willReturn('Please provide a composer.json'); $this->uploadComposerAction($request)->shouldBeJsonResponse(array('status' => 'ko', 'message' => array('Please provide a composer.json'))); }
function it_does_not_handle_form_when_the_form_is_invalid(Request $request, FormFactoryInterface $formFactory, FormBuilderInterface $formBuilder, FormInterface $form, FormError $formError, FormInterface $child) { $formFactory->createNamedBuilder('', 'kreta_dummy_type', null, [])->shouldBeCalled()->willReturn($formBuilder); $formBuilder->getForm()->shouldBeCalled()->willReturn($form); $request->isMethod('POST')->shouldBeCalled()->willReturn(true); $form->handleRequest($request)->shouldBeCalled()->willReturn($form); $form->isValid()->shouldBeCalled()->willReturn(false); $form->getErrors()->shouldBeCalled()->willReturn([$formError]); $formError->getMessage()->shouldBeCalled()->willReturn('Form error'); $form->all()->shouldBeCalled()->willReturn([$child]); $child->isValid()->shouldBeCalled()->willReturn(false); $child->getName()->shouldBeCalled()->willReturn('Child form error'); $child->getErrors()->shouldBeCalled()->willReturn([]); $child->all()->shouldBeCalled()->willReturn([]); $this->shouldThrow(new InvalidFormException(['Form error', 'Child form error' => []]))->during('handleForm', [$request]); }
private function getErrorMessage(FormError $error) { if (null !== $error->getMessagePluralization()) { return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators'); } return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); }
public function addError(FormError $error) { if (null === $error->getOrigin()) { $error->setOrigin($this); } if ($this->parent && $this->config->getErrorBubbling()) { $this->parent->addError($error); } else { $this->errors[] = $error; } return $this; }
/** * @param FormError $error * @return string */ protected function getErrorCodeByMessage(FormError $error) { if (stristr($error->getMessage(), Error::FORM_TYPE_CSRF)) { return $this->getErrorCode(Error::FORM_TYPE_CSRF); } return $this->getErrorCode(Error::FORM_TYPE_GENERAL); }
public function __construct($propertypath, $messageTemplate, array $messageParameters = array()) { parent::__construct($messageTemplate, $messageParameters); $this->propertypath = $propertypath; }
public function testsTranslateCustomErrorMessage() { $this->tokenManager->expects($this->once())->method('isTokenValid')->with(new CsrfToken('TOKEN_ID', 'token'))->will($this->returnValue(false)); $this->translator->expects($this->once())->method('trans')->with('Foobar')->will($this->returnValue('[trans]Foobar[/trans]')); $form = $this->factory->createBuilder('form', null, array('csrf_field_name' => 'csrf', 'csrf_token_manager' => $this->tokenManager, 'csrf_message' => 'Foobar', 'csrf_token_id' => 'TOKEN_ID', 'compound' => true))->getForm(); $form->submit(array('csrf' => 'token')); $errors = $form->getErrors(); $expected = new FormError('[trans]Foobar[/trans]'); $expected->setOrigin($form); $this->assertGreaterThan(0, count($errors)); $this->assertEquals($expected, $errors[0]); }
public function testExtractSubmittedDataStoresErrorOrigin() { $form = $this->createBuilder('name')->getForm(); $error = new FormError('Invalid!'); $error->setOrigin($form); $form->submit('Foobar'); $form->addError($error); $this->assertSame(array( 'submitted_data' => array( 'norm' => 'Foobar', ), 'errors' => array( array('message' => 'Invalid!', 'origin' => spl_object_hash($form), 'trace' => array()), ), 'synchronized' => true, ), $this->dataExtractor->extractSubmittedData($form)); }
/** * @dataProvider getPostMaxSizeFixtures */ public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = array()) { $this->serverParams->expects($this->once())->method('getContentLength')->will($this->returnValue($contentLength)); $this->serverParams->expects($this->any())->method('getNormalizedIniPostMaxSize')->will($this->returnValue($iniMax)); $options = array('post_max_size_message' => 'Max {{ max }}!'); $form = $this->factory->createNamed('name', 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType', null, $options); $this->setRequestData('POST', array(), array()); $this->requestHandler->handleRequest($form, $this->request); if ($shouldFail) { $error = new FormError($options['post_max_size_message'], null, $errorParams); $error->setOrigin($form); $this->assertEquals(array($error), iterator_to_array($form->getErrors())); $this->assertTrue($form->isSubmitted()); } else { $this->assertCount(0, $form->getErrors()); $this->assertFalse($form->isSubmitted()); } }
/** * Formatting the property_path * * @param FormError $error The form error * * @return string|null */ private function formatPropertyPath(FormError $error) { $path = $error->getCause()->getPropertyPath(); if (empty($path)) { return; } $path = preg_replace('/^(data.)|(.data)|(\\])|(\\[)|children/', '', $path); return $path; }
/** * @return FormError */ protected function getFormError(ConstraintViolationInterface $violation, FormInterface $form) { $error = new FormError($this->message, $this->messageTemplate, $this->params, null, $violation); $error->setOrigin($form); return $error; }
public static function formatErrors(FormError $error) { return $error->getMessageTemplate(); }
function propertyPath(FormError $error) { $parent = $error->getOrigin(); $out = ''; while ($parent) { $out = $parent->getPropertyPath() . $out; $parent = $parent->getParent(); if ($parent) { $out = '_' . $out; } } return $out; }
private function getErrorMessage(FormError $error) { return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); }