jeudi 9 avril 2015

Symfony Validating Froms

I have a symfony form. Now, I'm planning to implement validation for which I've written a validation.yml file as follows



Core\AppBundle\Entity\User:
properties:
name:
- Length:
min: 5
max: 255
minMessage: "Your name should be at least {{ limit }} characters"
maxMessage: "Your name cannot be more than {{ limit }} characters"


Now symfony validates with the given constraints when I use the following snippet in my Action



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


However, If I use



if ($form->isValid()) {

}

return $response;


The validation is not done.


But according to this article, Synfony uses internally the validator service and additionally converts ConstraintViolationList objects into FieldErrors which comes in really handy for my purpose.


What might be the mistake in my workflow ?


EDIT1:



$form = $this->createForm(new RegType(), $reg);


and $reg is an object which contains the entity User as a variable.


Aucun commentaire:

Enregistrer un commentaire