symfony - How to get all form errors symfony2 -
constrains:
/** * @orm\column(type="string", length=15) * @assert\regex(pattern = "/[0-9a-z]+/", message = "[regexp] error") * @assert\notblank(message = "[notblank] error") * @assert\notnull(message = "[notnull] error") * @assert\length * ( * min = "2", * max = "4", * minmessage = "[minlength] error", * maxmessage = "[maxlength] error" * ) */ private $type_name; /** * @orm\column(type="string", length=50) * @assert\regex(pattern = "/[0-9a-za-z\.\:\s]+/", message = "[regexp] error") * @assert\notblank(message = "[notblank] error") * @assert\notnull(message = "[notnull] error") * @assert\length * ( * min = "4", * max = "50", * minmessage = "[minlength] error", * maxmessage = "[maxlength] error" * ) */ private $description; /** * @orm\column(type="string", length=60) * @assert\regex(pattern = "/[0-9a-za-z\.\/]+/", message = "[regexp] error") * @assert\notblank(message = "[notblank] error") * @assert\notnull(message = "[notnull] error") * @assert\length * ( * min = "4", * max = "60", * minmessage = "[minlength] error", * maxmessage = "[maxlength] error" * ) */ private $starterpath;
controller (typesaction , typesaddaction):
public function typesaction() { $em = $this->getdoctrine()->getmanager(); $types = $em->getrepository('csmbundle:type')->findall(); $newtype = new type(); $form = $this->createformbuilder($newtype) ->add('typename', 'text') ->add('description', 'text') ->add('starterpath', 'text') ->getform(); return $this->render('csmbundle:root:types.html.twig', array( 'types' => $types, 'form' => $form->createview() )); } public function typesaddaction(request $request) { $newtype = new type(); $form = $this->createformbuilder($newtype) ->add('typename', 'text') ->add('description', 'text') ->add('starterpath', 'text') ->getform(); if ($request->getmethod() == 'post') { $form->bind($request); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($newtype); $em->flush(); return $this->redirect($this->generateurl('root_types')); } else { $em = $this->getdoctrine()->getmanager(); $types = $em->getrepository('csmbundle:type')->findall(); return $this->render('csmbundle:root:types.html.twig', array( 'types' => $types, 'form' => $form->createview() )); } } }
types.html.twig:
... <form class="well" action="{{ path('root_types_add') }}" method="post" {{ form_enctype(form) }}> <fieldset> <legend>adding new type</legend> <table border="0"> <tr> <td width="100" align="left"><strong>type name:</strong></td><td>{{ form_widget(form.typename, { 'attr': {'class': 'txt'} }) }}</td> </tr> <tr> <td align="left"><strong>description:</strong></td><td>{{ form_widget(form.description, { 'attr': {'class': 'txt'} }) }}</td> </tr> <tr> <td align="left"><strong>starter:</strong></td><td>{{ form_widget(form.starterpath, { 'attr': {'class': 'txt'} }) }}</td> </tr> <tr> <td colspan="2">{{ form_errors(form) }}</td> </tr> <tr> <td colspan="2">{{ form_rest(form) }}</td> </tr> <tr> <td colspan="2" align="right"><button style="" class="btn btn-large btn-success" value="add" name="add">add!</button></td> </tr> </table> </fieldset> </form> ...
problem there: got error first field (typename). if input incorrect data fields html form, got only(!) 1 error first field (typename). if input incorrect data second (description) , third (starterpath) fields - have no error.
please use {{ form_row(form.name) }}
instead of {{ form_widget(form.name)
inside templates resolve issue....
form_widget
renders field's html while form_row
renders combination of form_label
, form_widget
, form_errors
.
have @ documentation here.
if form bubbles errors global errors try setting error_bubbling
false
( defaults true
) inside form's default options this:
$this->createformbuilder($entity, array( 'error_bubbling' => false, 'data_class' => 'vendor\mybundle\entity\name', // ... more options ) )
Comments
Post a Comment