Question

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.

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top