[英]Lithium and validating login form (without model) - how?
有什么方法可以在Lithium教程的Simple Authentication中使用登录表单上的Validator。 我知道在模型中使用验证更好,但是使用登录表单没有模型,因此,据我所知,我需要在SessionsController中使用Validator,但我不知道如何做(
我想要做的是在SessionsController中:
<?php
namespace app\controllers;
use lithium\security\Auth;
use lithium\storage\Session;
use lithium\util\Validator;
class SessionsController extends \lithium\action\Controller {
private $rules = array(
'password' => array(
array('notEmpty', 'message' => 'password is empty'),
),
'email' => array(
array('notEmpty', 'message' => 'email is empty'),
array('email', 'message' => 'email is not valid')
)
);
public function add() {
if ($this->request->data && Auth::check('default', $this->request)) {
return $this->redirect('/');
}
// Handle failed authentication attempts
$errors = Validator::check($this->request->data, $this->rules);
return compact('errors');
}
public function delete() {
Auth::clear('default');
return $this->redirect('/');
}
/* ... */
}
我希望在发送空表单后,它会被渲染出错,就像在教程中创建用户一样。 但是没有显示错误,只需再次登录表单。 我能否在没有模型的情况下验证表格以及如何在Lithium中进行验证?
先谢谢。
表单中呈现的错误绑定到一个实体,当您使用$this->form->create($user)
该实体时,该实体将绑定到该表单。 仅在这种情况下,由于表单助手自动显示错误。
如果您需要在控制器中检查传入的数据,您可以检查$this->request->data['password']
并在表单视图中返回您需要自己处理的错误(使用if (!empty($errors))
例如)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.