When building a form in Symfony2 with validation and custom error messages for required fields, I manually have to specify NotBlank constraints and custom error message attributes for client side validation (using Bootstrap Validator). The code looks like so for every form field:
$builder->add('name', 'text', array(
'required' => true,
'constraints' => array(
new NotBlank(array('message' => 'Bitte geben Sie Ihren Namen ein.')),
),
'attr' => array(
// This is for client side bootstrap validator
'data-bv-notempty-message' => 'Bitte geben Sie Ihren Namen ein.'
)
));
What I am looking for is the possibility to make this shorter by specifying the required_message only once:
$builder->add('name', 'text', array(
'required_message' => 'Bitte geben Sie Ihren Namen ein.'
));
and I would like the builder to create the NotBlank constraint and the data-bv-notempty-message attribute.
What's the best way to achieve this? By creating a form type extension?