我有这些验证规则:
Cgboard\SignupBundle\Entity\SignupData
properties:
email:
- NotBlank:~
- MinLength:3 // not validating neither this nor the rest
nickname:
- NotBlank:~
password:
- NotBlank:~
password_repeat:
- NotBlank:~
我的实体
<?php
namespace Cgboard\SignupBundle\Entity;
class SignupData {
public $email;
public $nickname;
public $password;
public $password_repeat;
}
我的控制员:
<?php
namespace Cgboard\SignupBundle\Controller\Frontend;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SignupController extends Controller
{
...
if($request->getMethod() === 'POST') {
$form->bind($request);
if($form->isValid()) {
$this->save($SignupData);
}
}
.....
}
目前$form->isValid()
始终返回true
。
答案 0 :(得分:0)
确保validation.yml
文件位于Cgboard/SignupBundle/Resources/config
目录中,因为nifr提到了清除缓存。还尝试使用以下方式验证它是否真的有效
$testobj = new SignupData();
$validator = $this->get('validator');
$errors = $validator->validate($testobj);
if (count($errors) > 0) {
return new Response(print_r($errors, true));
} else {
return new Response('The testobj is valid! Yes!');
}