1

我对验证器有疑问。如果我输入无效数据,我的表格也始终有效。我的验证器似乎没有被考虑。

编码:

//验证.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] }

//控制器

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>');
    }

}

形式

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')
        ));
    }

}
4

2 回答 2

1

在 config.yml 中:

验证:{启用:真}

这对我有用!

于 2012-12-04T10:55:26.870 回答
0

您应该将您的 validation.yml 文件连接到 Bundle

在 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);

在 config.yml 中:

validation:{ enabled: true }

于 2012-10-26T14:51:54.377 回答