Setting invalid message within form types in Symfony using model transformers

In Symfony, you can set the parameter “invalid_message” in your form types.

For example:

class FooShortlistChoiceType extends AbstractType {
    protected $em;

    public function __construct(EntityManager $entityManager)
    {
        $this->em  = $entityManager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $fooTransformer = new FooToStringTransformer($this->em);
        
        $builder
            ->add('yourField', 'text', array('invalid_message' => 'Invalid message pertaining to this form field here.'))
            ->get('yourField')->addModelTransformer($fooTransformer);
        
    }

    public function getParent() {
        return 'choice';
    }

    public function getName() {
        return 'fooShortlist';
    }
}


For more about Data Transformers and what they’re used for, visit the Symfony Cookbook:  http://symfony.com/doc/current/cookbook/form/data_transformers.html

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *