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)

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?