symfony3

Validation

Remarks#

In fact, form validation is based from a component, named ”Validator Component“.

You can often use the dedicated service if you did’t have to show a form in a template. Like APIs. You may validate datas in the same way, like this :

For example, based on symfony doc :

$validator = $this->get('validator');
$errors = $validator->validate($author);

if (count($errors) > 0) {
    /*
     * Uses a __toString method on the $errors variable which is a
     * ConstraintViolationList object. This gives us a nice string
     * for debugging.
     */
    $errorsString = (string) $errors;
}

Symfony validation using annotations

  • Enable validation using annotations in app/config/config.yml file

    framework: validation: { enable_annotations: true }

  • Create an Entity in AppBundle/Entity directory. The validations are made with @Assert annotations.

    id; } /** * Set name * * @param string $name * * @return Car */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set number * * @param integer $number * * @return Car */ public function setNumber($number) { $this->number = $number; return $this; } /** * Get number * * @return integer */ public function getNumber() { return $this->number; } }
  • Create a new form in AppBundle/Form directory.

    add('name', TextType::class, ['label'=>'Name']) ->add('number', IntegerType::class, ['label'=>'Number']) ; } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Car' )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { // TODO: Implement setDefaultOptions() method. } public function getName() { return 'car_form'; } }
  • Create a new route and a new action method in AppBundle/Controller/DefaultController.php. The route will be declared with annotations too, so make sure you’ve imported this route in the main route file (app/config/routing.yml).

    createForm( CarType::class, $car, [ 'action' => $this->generateUrl('app_car'), 'method'=>'POST', 'attr'=>[ 'id'=>'form_car', 'class'=>'car_form' ] ] ); $form->handleRequest($request); return $this->render( 'AppBundle:Default:car.html.twig',[ 'form'=>$form->createView() ] ); } }
  • Create the view in AppBundle/Resources/views/Default/car.html.twig.

    {% extends ‘::base.html.twig’ %}

    {% block body %} {{ form_start(form, {‘attr’: {‘novalidate’:‘novalidate’}}) }} {{ form_row(form.name) }} {{ form_row(form.number) }} {{ form_end(form) }} {% endblock %}

  • Start Symfony’s built-in server (php bin/console server:run) and access 127.0.0.1:8000/car route in your browser. There should be a form consisting of two input boxes and a submit button. If you press the submit button without entering any data into the input boxes, then the error messages will be displayed.

Symfony validation using YAML

  • Create an Entity in AppBundle/Entity directory. You can do this manually, or by using Symfony’s command php bin/console doctrine:generate:entity and filling in the required information in each step. You must specify yml option at Configuration format (yml, xml, php or annotation) step.

    id; } /** * Set name * * @param string $name * * @return Person */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set age * * @param integer $age * * @return Person */ public function setAge($age) { $this->age = $age; return $this; } /** * Get age * * @return int */ public function getAge() { return $this->age; } }
  • Create the Entity mapping information for the Entity class. If you are using Symfony’s command php bin/console doctrine:generate:entity, then the following code will be auto-generated. Otherwise, if you don’t use the command, you can create the following code by hand.

    AppBundle/Resources/config/doctrine/Person.orm.yml

    AppBundle\Entity\Person: type: entity table: persons repositoryClass: AppBundle\Repository\PersonRepository id: id: type: integer id: true generator: strategy: AUTO fields: name: type: string length: ‘50’ age: type: integer lifecycleCallbacks: { }

  • Create the validation for the Entity class.

    AppBundle/Resources/config/validation/person.yml

    AppBundle\Entity\Person: properties: name: - NotBlank: message: “Name is required” - Length: min: 3 max: 50 minMessage: “Please use at least 3 chars” maxMessage: “Please use max 50 chars” - Regex: pattern: “/1+$/” message: “Please use only letters” age: - NotBlank: message: “Age is required” - Length: min: 1 max: 3 minMessage: “The age must have at least 1 number in length” maxMessage: “The age must have max 3 numbers in length” - Regex: pattern: “/2+$/” message: “Please use only numbers”

  • Create a new form in AppBundle/Form directory.

    add('name', TextType::class, ['label'=>'Name']) ->add('age', IntegerType::class, ['label'=>'Age']) ; } /** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Person' )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { // TODO: Implement setDefaultOptions() method. } public function getName() { return 'person_form'; } }
  • Create a new route in AppBundle/Resources/config/routing.yml

    app_person: path: /person defaults: { _controller: AppBundle:Default:person }

  • Now create a new action method for that route.

    createForm( PersonType::class, $person, [ 'action' => $this->generateUrl('app_person'), 'method'=>'POST', 'attr'=>[ 'id'=>'form_person', 'class'=>'person_form' ] ] ); $form->handleRequest($request); return $this->render( 'AppBundle:Default:person.html.twig', [ 'form'=>$form->createView() ] ); } }
  • Create the view in AppBundle/Resources/views/Default/person.html.twig

    {% extends ‘::base.html.twig’ %}

    {% block body %} {{ form_start(form, {‘attr’: {‘novalidate’:‘novalidate’}}) }} {{ form_row(form.name) }} {{ form_row(form.age) }} {{ form_end(form) }} {% endblock %}

  • Start Symfony’s built-in server (php bin/console server:run) and access 127.0.0.1:8000/person route in your browser. There should be a form consisting of two input boxes and a submit button. If you press the submit button without entering any data into the input boxes, then the error messages will be displayed.


  1. A-Za-z
  2. 0-9

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow