How to add a translated validation message to a form without entity with Symfony 2.3 -
i'm using symfony 2.3.
have simple contact form , add custom translated validation messages, (but don't have entity) :
class author { /** * @assert\notblank(message = "author.name.not_blank") */ public $name; }
i have done :
namespace me\mybundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; use symfony\component\validator\constraints\notblank; class contacttype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('name', 'text', array('constraints' => new notblank(array('message' => "contact.name.not_blank")),)) ;
where in me/mybundle/resources/translations, have added in "validators.fr.yml" :
contact.name.not_blank: "please enter name."
the message "contact.name.not_blank",
class contacttype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('name', 'text', array('constraints' => new notblank(array('message' => $this->get('translator')->trans('contactname_notblank'))), ));
and english translation yaml file:
// appbundle/resources/translations/messages.en.yml contactname_notblank: name field required!
Comments
Post a Comment