1

I have a problem with validators. My form is always valid also if i enter invalid data. My validator does not seem to be considered.

The code:

//validation.yml

AskedTech\JobeetBundle\Entity\Users:
properties:
        email:
            - Email:
                message: The email "{{ value }}" is not a valid email.
                groups: [registration]
            - NotBlank: { groups: [registration] }
            - UniqueEntity: { groups: [registration] }
        password:
            - NotBlank: { groups: [registration] }
            - MinLength: { limit: 7, groups: [registration] }
        first_name:
            - NotBlank: { groups: [registration] }
        last_name:
            - NotBlank: { groups: [registration] }

//Controller

namespace AskedTech\JobeetBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AskedTech\JobeetBundle\Entity\Users;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

use AskedTech\JobeetBundle\Form\SignupType;

class UsersController extends Controller {

    public function signupAction(Request $request) {

        $users = new Users();
        $form = $this->createForm(new SignupType(), $users);

        if ($request->isMethod('POST')) {
            $form->bind($request);

            if ($form->isValid()) {

                $post_value = $request->request->get($form->getName());

                return $this->redirect($this->generateUrl('welcome', array('name' => $post_value['email'])));

            }
        }

        return $this->render('AskedTechJobeetBundle:Users:signup.html.twig', array(
                    'form' => $form->createView(),
                ));
    }

    public function welcomeAction($name) {
        return new Response('<html><body>Welcome in Jobeet '.$name.'!</body></html>');
    }

}

Form

namespace AskedTech\JobeetBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SignupType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('email', 'email');
        $builder->add('first_name', 'text');
        $builder->add('last_name', 'text');
        $builder->add('email', 'text');
        $builder->add('password', 'password');
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'AskedTech\JobeetBundle\Entity\Users',
            'validation_groups' => array('AskedTech\JobeetBundle\Entity\Users', 'registration')
        ));
    }

}
0

2 Answers 2

1

in config.yml:

validation:{ enabled: true }

This did the trick for me!

Sign up to request clarification or add additional context in comments.

Comments

0

you should connect your validation.yml file to Bundle

in JobeetBundle\DependencyInjection\JobeetExtension add following lines:

$yamlMappingFiles = $container-> getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');

$yamlMappingFiles[] = DIR . '/../Resources/config/validation.yml'; $container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $yamlMappingFiles);

in config.yml:

validation:{ enabled: true }

1 Comment

I don't think it is necessary to manually connect the validation file to the bundle is it? I never had to do that. Linking to the right entity in the validation.yml file is supposed to be enough. On the other hand setting validation enabled might solve his problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.