Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 有关Yii框架表单验证的问题。懂Yii的大神请进来看看吧.

有关Yii框架表单验证的问题。懂Yii的大神请进来看看吧.

Jun 23, 2016 pm 01:55 PM
yii Great God form

我在用Yii做一个简单的小blog,在做后台登陆验证的时候,出现bug,
不提示登陆错误信息,我做的是账号,密码,验证码不能为空。
而且也不与数据库匹配。我已经测试过,数据库连接完全没有问题。

LoginController.php
/**
 * 后台登陆控制器
 */
class LoginController extends Controller{
/**
 * 后台登陆模板
 */
public function actionIndex(){
$loginForm = new LoginForm();
if(isset($_POST['$LoginForm'])){
$loginForm->attributes = $_POST['LoginForm'];
if($loginForm->validate()){
echo 1;die;
}
}
$this->render('index',array('loginForm' => $loginForm));
}

public function actions(){
return array(
'captcha' => array(
'class' => 'system.web.widgets.captcha.CCaptchaAction',
'height' => 25,
'width'  => 80,
'minLength' => 4,
'maxLength' => 4,
'offset' => 3,
'padding' => 1
),
);
}
}

这个是model,User.php
/**
 * 后台用户模型
 */
class User extends CActiveRecord{
/**
 * 必不可缺的方法一,返回模型
 */
public static function model($className = __CLASS__){
return parent::model($className);


/**
 * 必不可却的方法二,返回表名
 */
public function tableName(){
return "{{user}}";
}
}

这个是LoginForm.php中的一个方法
public function rules()
{
return array(
// username and password are required
array('username', 'required', 'message' => '用户名不能为空'),
array('password', 'required', 'message' => '密码不能为空'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
//自定义错误信息
array('captcha', 'captcha', 'message' => '验证码错误'),
);
}

这个是输出错误信息的页面  index.php 
                


  • error($loginForm,'username') ?>



  • error($loginForm,'password') ?>



  • error($loginForm,'captcha') ?>



这个是components目录中的  UserIdentity.php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
/**
 * Authenticates a user.
 * The example implementation makes sure if the username and password
 * are both 'demo'.
 * In practical applications, this should be changed to authenticate
 * against some persistent user identity storage (e.g. database).
 * @return boolean whether authentication succeeds.
 */
public function authenticate()
{
$userInfo = User::model()->find('username = :name', array(':name' => $this->username));
if($userInfo == NULL){
$this->errorCode = self::ERROR_USERNAME_INVALID;
return false;
}
if($userInfo->password !== md5($this->password)){
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
}
$this->errorCode = self::ERROR_NONE;
return true;
}
}


到底哪里出错了,数据库链接已经测过,完全没有问题。
现在的情况是,即使写了错误信息,也不提示,也不执行登陆验证。除了验证码可以更换之外,完全就像一个静态页面。


回复讨论(解决方案)

if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧

if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧




对对对,没错的。谢谢,谢谢...
我都不知道那个$是怎么写上去的...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement page jump after PHP form submission How to implement page jump after PHP form submission Aug 12, 2023 am 11:30 AM

How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

How to use JavaScript to realize the automatic prompt function of the input box content of the form? How to use JavaScript to realize the automatic prompt function of the input box content of the form? Oct 20, 2023 pm 04:01 PM

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

How to handle user rights management in PHP forms How to handle user rights management in PHP forms Aug 10, 2023 pm 01:06 PM

How to handle user rights management in PHP forms With the continuous development of web applications, user rights management is one of the important functions. User rights management can control users' operating rights in applications and ensure the security and legality of data. In PHP forms, user rights management can be implemented through some simple code. This article will introduce how to handle user rights management in PHP forms and give corresponding code examples. 1. Definition and management of user roles First of all, defining and managing user roles is a matter of user rights.

How to use JavaScript to implement real-time verification of the input box content of a form? How to use JavaScript to implement real-time verification of the input box content of a form? Oct 18, 2023 am 08:47 AM

How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML

How to use HTML, CSS and jQuery to realize the advanced function of automatic saving of forms How to use HTML, CSS and jQuery to realize the advanced function of automatic saving of forms Oct 28, 2023 am 08:20 AM

How to use HTML, CSS and jQuery to implement the advanced function of automatic saving of forms. Forms are one of the most common elements in modern web applications. When users enter form data, how to implement the automatic saving function can not only improve the user experience, but also ensure data security. This article will introduce how to use HTML, CSS and jQuery to implement the automatic saving function of the form, and attach specific code examples. 1. Structure of HTML form. Let’s first create a simple HTML form.

PHP form processing: form data query and filtering PHP form processing: form data query and filtering Aug 07, 2023 pm 06:17 PM

PHP form processing: form data query and filtering Introduction In Web development, forms are an important way of interaction. Users can submit data to the server through forms for further processing. This article will introduce how to use PHP to process the query and filter functions of form data. Form design and submission First, we need to design a form that includes query and filter functions. Common form elements include input boxes, drop-down lists, radio buttons, check boxes, etc., which can be designed according to specific needs. When the user submits the form, the data will be sent to POS

Tips for using Laravel form classes: ways to improve efficiency Tips for using Laravel form classes: ways to improve efficiency Mar 11, 2024 pm 12:51 PM

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

See all articles