Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Route("/foobar")
- * @return Response
- */
- public function fooAction()
- {
- $form = $this->createForm(SomeFooType::class);
- $token = $this->get('security.csrf.token_manager')
- ->getToken('some_foo')
- ->getValue();
- $data = [
- 'name' => 'john',
- 'surname' => 'doe',
- '_token' => $token
- ];
- $form->submit($data);
- if ( $form->isSubmitted() ) {
- if ( $form->isValid() ) {
- return new Response("Form submitted and valid");
- }
- $errors = $form->getErrors(TRUE, TRUE);
- return new Response("Form submitted BUT not valid: " . (count($errors) ? $errors[0]->getMessage() : ""), 400);
- }
- return new Response("Form not submitted!");
- }
- class SomeFooType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('name', TextType::class, [
- 'label' => 'First name:',
- 'constraints' => [
- new NotBlank()
- ]
- ])
- ->add('surname', TextType::class, [
- 'constraints' => [
- new NotBlank()
- ]
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement