簡體   English   中英

鋰和驗證登錄表格(沒有型號) - 怎么樣?

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

有什么方法可以在Lithium教程的Simple Authentication中使用登錄表單上的Validator。 我知道在模型中使用驗證更好,但是使用登錄表單沒有模型,因此,據我所知,我需要在SessionsController中使用Validator,但我不知道如何做(

我想要做的是在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('/');
}

/* ... */
}

我希望在發送空表單后,它會被渲染出錯,就像在教程中創建用戶一樣。 但是沒有顯示錯誤,只需再次登錄表單。 我能否在沒有模型的情況下驗證表格以及如何在Lithium中進行驗證?

先謝謝。

表單中呈現的錯誤綁定到一個實體,當您使用$this->form->create($user)該實體時,該實體將綁定到該表單。 僅在這種情況下,由於表單助手自動顯示錯誤。

如果您需要在控制器中檢查傳入的數據,您可以檢查$this->request->data['password']並在表單視圖中返回您需要自己處理的錯誤(使用if (!empty($errors))例如)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM