简体   繁体   English

验证器无法正常工作Symfony2.1

[英]Validators not working Symfony2.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 //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')
        ));
    }

}

in config.yml: 在config.yml中:

validation:{ enabled: true } 验证:{已启用:true}

This did the trick for me! 这对我有用!

you should connect your validation.yml file to Bundle 您应该将您的validate.yml文件连接到Bundle

in JobeetBundle\\DependencyInjection\\JobeetExtension add following lines: 在JobeetBundle \\ DependencyInjection \\ JobeetExtension中添加以下行:

$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: 在config.yml中:

validation:{ enabled: true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM