我的验证器有问题。如果输入无效数据,我的表单也始终有效。 我的验证器似乎没有被考虑。
代码:
// 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] }
//控制器
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')
));
}
}
答案 0 :(得分:1)
在config.yml中:
验证:{enabled:true}
这对我有用了!
答案 1 :(得分: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 }