symfony - Form: I don't get errors when I submit wrong data -
i have controller code validate form (i'm using ajax reques). problem: dont error when submit wrong data, can see in output belower:
$app->post('/contacto', function (request $request) use ($app) { $form = $app['form.factory']->createbuilder('form', $data) ->add('nombre', 'text', array( 'constraints' => new assert\notblank(array('message' => 'el campo nombre es obligatorio')) )) ->add('email', 'text', array( 'required' => false, 'constraints' => new assert\email(array('message' => 'has introducido un email no válido. revísalo, por favor.')), )) ->add('telefono', 'text', array( 'label' => 'teléfono', 'constraints' => array( new assert\regex(array('pattern' => "/^(?:\d\s*){8}\d$/", 'message' => 'el teléfono debe tener 9 dígitos')), new assert\notblank(array('message' => 'el campo teléfono es obligatorio')), ))) ->add('texto', 'textarea', array( 'constraints' => new assert\notblank(array('message' => 'el campo texto es obligatorio')), 'attr' => array('cols' => '76', 'rows' => '8'), )) ->getform(); $post = $request->request->get('form'); $form->bind($post); if ($form->isvalid()) { $data = $form->getdata(); $app['mailer']->send($message); $my_array = array('gracias, hemos recibido tu mensaje, te contactaremos lo antes posible'); return new response('true'); } else { var_dump($form->getdata()); var_dump($form->geterrors()); die("jfklas"); return new response(json_encode($form)); } }); array(4) { ["nombre"]=> string(8) "fasdfasd" ["email"]=> string(5) "fasdf" ["telefono"]=> string(7) "9999999" ["texto"]=> string(7) "fasdfas" } array(0) { } jfklas
$form->geterrors() return returns errors of form itself, , not errors child fields. can errors if specify field, example $form['telefono']->geterrors() or using $form->geterrorsasstring()
Comments
Post a Comment