Canser Yanbakan
Canser Yanbakan

Reputation: 3870

Symfony2 Custom Field Type and Form Events on Submit

I made a captcha field type that show captcha images to users.

I want to check if user is correct or not on my custom field type (self validation on custom field).

My field type:

<?php
namespace CS\CommonBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CaptchaType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('mapped' => false));
    }

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

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

My service:

cs_common.form.type.captcha:
    class: CS\CommonBundle\Form\Type\CaptchaType
    tags:
        - { name: form.type, alias: captcha }

This works fine when we call from any form type.

enter image description here

Now i have to validate the form data is matching with session captcha value in my custom field type.

How can i do this?

Thank you!

Upvotes: 0

Views: 480

Answers (2)

Canser Yanbakan
Canser Yanbakan

Reputation: 3870

Complete Solution:

<?php
namespace CS\CommonBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

class CaptchaType extends AbstractType
{
    private $session;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
            $form = $event->getForm();
            $captcha_value = $form->getData();

            if($captcha_value !== $this->session->get('captcha')) {
                $form->addError(new FormError('Doğrulama Kodu Yanlış!'));
            }
        });
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('mapped' => false));
    }

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

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

Don't forget to update service:

cs_common.form.type.captcha:
    class: CS\CommonBundle\Form\Type\CaptchaType
    arguments:
        session: "@session"
    tags:
        - { name: form.type, alias: captcha }

Upvotes: 0

Derick F
Derick F

Reputation: 2769

Add this to your FormType to add a validator.

public function buildForm(FormBuilderInterface $builder, array $options) {
    $validator = new CustomValidator();
    $builder->addEventListener(FormEvents::POST_BIND, array($validator, 'validate'));
}

Upvotes: 1

Related Questions