锂和验证登录表格(没有型号) - 怎么样?

时间:2012-07-27 09:04:08

标签: php lithium

有没有办法在Simple Authentication in Lithium教程的登录表单上使用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中进行验证?

事先谢谢。

1 个答案:

答案 0 :(得分:4)

表单中呈现的错误绑定到实体,当您使用$this->form->create($user)创建表单时绑定到该表单。仅在这种情况下,由于表单助手自动显示错误。

如果您需要在控制器中检查传入数据,您可以在表单视图中检查$this->request->data['password']并返回您需要自己处理的错误(例如if (!empty($errors))