简体   繁体   English

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

[英]Lithium and validating login form (without model) - how?

Is there any way to use Validator on login form from Simple Authentication in Lithium tutorial. 有什么方法可以在Lithium教程的Simple Authentication中使用登录表单上的Validator。 I know that it is better to use validation in model, but with login form there's no model, so, as I understand, I need to use Validator in the SessionsController, but I don't know how to do it ( 我知道在模型中使用验证更好,但是使用登录表单没有模型,因此,据我所知,我需要在SessionsController中使用Validator,但我不知道如何做(

What I am trying to do is in SessionsController: 我想要做的是在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('/');
}

/* ... */
}

and I'm expect that after empty form was sent, it will be rendered with errors, like in user creation from tutorial. 我希望在发送空表单后,它会被渲染出错,就像在教程中创建用户一样。 But there are no errors showed, just login form again. 但是没有显示错误,只需再次登录表单。 Can I ever validate forms without models and how to do it in Lithium? 我能否在没有模型的情况下验证表格以及如何在Lithium中进行验证?

Thanks beforehand. 先谢谢。

The errors rendered in the form are bound to an entity, which is bound to the form when you create it with $this->form->create($user) . 表单中呈现的错误绑定到一个实体,当您使用$this->form->create($user)该实体时,该实体将绑定到该表单。 In this case only, the errors are displayed thanks to the form helper automatically. 仅在这种情况下,由于表单助手自动显示错误。

If your need is to check, in the controller, the incoming data, you can check $this->request->data['password'] and return errors that you need to handle by yourself in the form view (with if (!empty($errors)) for example) 如果您需要在控制器中检查传入的数据,您可以检查$this->request->data['password']并在表单视图中返回您需要自己处理的错误(使用if (!empty($errors))例如)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM