1

I modify my code to follow @Stony advices.

I would like to customise my email input error. I try to follow the symfony2 tutorial but it doesn't work.

I follow symfony validation tutorial and I create my yml file, but when I fill my email fiel with bad adress, the error display is still the symfony default error and not my custom message...

/**
 *
 * @Route("/info")
 */
class InfoController extends BaseController
{
    public function contactusAction(Request $request)
    {   
    $constraint = new Collection(array('email' => new Email(array('message' => 'Adresse email invalide')),));

    $contact = new ContactUs(); 
    $form = $this->createFormBuilder($contact)
        ->add('nom', 'text')
        ->add('mail', 'email')
        ->add('sujet', 'choice', array('choices' => array('pt' => 'Problemes techniques', 'bi' => 'Boite a idees', 'd' => 'Divers')))
        ->add('msg', 'textarea')
        ->getForm();
    }
}

Here is my view

<form action="" method="post" {{ form_enctype(form) }} class="contactus">
        {{ form_errors(form) }}
        <div>
            {{ form_label(form.nom, 'Nom : ') }}
            {{ form_errors(form.nom) }}
            {{ form_widget(form.nom) }}
            </div>
            <div>
            {{ form_label(form.mail, 'Email : ') }}
            {{ form_errors(form.mail) }}
            {{ form_widget(form.mail) }}
            </div>
        <div>
            {{ form_label(form.sujet, 'Sujet : ') }}
            {{ form_errors(form.sujet) }}
            {{ form_widget(form.sujet) }}
            </div>
        <div>
            {{ form_label(form.msg, 'Message : ') }}
            {{ form_errors(form.msg) }}
            {{ form_widget(form.msg) }}
            </div>
        <input type="submit" value="Envoyer" />
        {{ form_rest(form) }}
    </form>

Here is my yml file

PROJECT\CoreBundle\Entity\ContactUs:
    properties:
        mail:
            - Email:
                message: Adresse email invalide.
Tom-pouce
  • 758
  • 2
  • 11
  • 28

2 Answers2

0

I think the error message is very clear:

$form = $this->createFormBuilder($contact, array('constraints' => $constraint))

there is no second option constraint. I don't know what your goal is but this is not the correct way.

If you want some validation you should look at Creating Form Classes in your Tutorial. There you can see how you can define Form and Type classes and how to set validation on the fields.

Some more information: Validation

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • http://symfony.com/doc/current/book/forms.html#creating-form-classes#adding-validation You can find the line, so constraints option exist, but it doesn't work on my code ;) – Tom-pouce Oct 30 '12 at 10:26
  • Its the same like validation ;) constraints are used in another way. With a yml file or the constraint classes. The easiest way is to make a validation class and use the annotations. – René Höhle Oct 30 '12 at 10:32
  • But I only want to change the error message of email input, and I would like to do it in my controller. Not in yml file. That's impossible? – Tom-pouce Oct 30 '12 at 10:35
  • The problem is. "Don't fight the framework" when you work with a framework use the possibilies and split it in different classes and build a structure. Perhaps its easier when you search a Symfony Bundle on Github and look how they do it. – René Höhle Oct 30 '12 at 10:37
  • Look I've updated my post, I create my YML file and put some constraints in it. But when I send my form I've still my error "Please enter an email address" and not "Adresse email invalide" – Tom-pouce Oct 30 '12 at 10:50
0

Use the constraints option on the mail field instead of the form itself.

For Symfony 2.0 use this.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • I tried this, ->add('mail', 'email', array('constraints' => $constraint)) but same error message – Tom-pouce Oct 30 '12 at 10:20
  • I already use this tutorial, and I copy paste the line with constraints option but no result. =/ – Tom-pouce Oct 30 '12 at 10:28
  • I follow some links and I find that [link](http://symfony.com/doc/2.0/book/validation.html#book-validation-raw-values), but where I've to call addEmailFunction? – Tom-pouce Oct 30 '12 at 10:32