我对验证器有疑问。如果我输入无效数据,我的表格也始终有效。我的验证器似乎没有被考虑。
编码:
//验证.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')
));
}
}