简体   繁体   中英

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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