ex3v
ex3v

Reputation: 3566

Symfony 2.5 - Get form fields options from listener

Having form like:

class MyForm extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('title', 'text', [
            'required'    => true,
            'trim'        => true,
            'constraints' => [
                new Constraints\NotBlank(),
                new Constraints\Length([
                    'min' => 20,
                    'max' => 255,
                ]),
            ]
        ]);

        $builder->addEventSubscriber(new StripWhitespaceListener());
    }

}

and following event subscriber

class StripWhitespaceListener implements EventSubscriberInterface
{
    /** {@inheritdoc} */
    public static function getSubscribedEvents()
    {
        return [
            FormEvents::PRE_SUBMIT => 'onPreSubmit'
        ];
    }

    /**
     * @param FormEvent $event
     */
    public function onPreSubmit(FormEvent $event)
    {
        (something something)
    }
}

I would like to check if title field has trim option set to true inside onPreSubmit method of StripWhitespaceListener.

How can I do that?

Upvotes: 2

Views: 262

Answers (1)

ex3v
ex3v

Reputation: 3566

Found the solution:

$event->getForm()->get('title')->getConfig()->getOption('trim')

Upvotes: 4

Related Questions