我无法理解为什么这段代码不起作用
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as Email;
class LoginController extends ApiController
{
public function indexAction()
{
$validation = new Phalcon\Validation();
$validation->add('email', new Email([
'field' => 'email',
'message' => 'wrong email'
]));
//same add
$messages = $validation->validate($_POST);
...
}
}
结果$ messages ==空数组,即使'email'字段错误
$validation->validate($_POST);
什么都不做。怎么了?
在这种情况下,我需要验证而不是在模型中。
答案 0 :(得分:3)
删除此项。
use Phalcon\Validation;
也改变了这个:
use Phalcon\Validation\Validator\Email as Email;
简单地说:
use Phalcon\Validation\Validator\Email;
你应该可以这样使用它:
$validation = new Phalcon\Validation();
$validation->add('email', new Email(array(
'message' => 'The e-mail is not valid'
)));
$messages = $validation->validate($_POST);
if (count($messages)) {
foreach ($messages as $message) {
echo $message, '<br>';
}
}