Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating form without Doctrine Entity

Tags:

symfony

I have a situation where I need to validate a form without actually having an object to store anywhere. In this scenario, would I still create an Entity without doctrine and validate it as ususal to perform whatever I want to do if form is valid or is there another way?

Example would be sending an email to a user by his name.

like image 945
DavidW Avatar asked Feb 03 '12 05:02

DavidW


1 Answers

See the Using a Form without a Class section — there is a subsection on validation, too.

The answer is to setup the constraints yourself, and attach them to the individual fields.

use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

$builder
   ->add('firstName', 'text', array(
       'constraints' => new Length(array('min' => 3)),
   ))
   ->add('lastName', 'text', array(
       'constraints' => array(
           new NotBlank(),
           new Length(array('min' => 3)),
       ),
   ))
;
like image 88
Elnur Abdurrakhimov Avatar answered Nov 14 '22 04:11

Elnur Abdurrakhimov