doujia9204 2012-07-27 09:04
浏览 21
已采纳

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

Is there any way to use Validator on login form from Simple Authentication in Lithium tutorial. 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 (

What I am trying to do is in 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?

Thanks beforehand.

  • 写回答

1条回答 默认 最新

  • donglefu6195 2012-07-27 13:48
    关注

    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). 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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗