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.
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)),
),
))
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With