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; }
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]); }
/** * @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()); } }
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)); }
/** * @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; }