php - Yii 验证特定字段

标签 php yii

问题定义

我有一个名为“user.php”的模型 我有一些验证规则如下 我现在要创建一个密码重置表单 在那个表格中,我有一个文本框名称电子邮件(用户模型中使用的相同电子邮件) 在密码重置表单中,我想检查此用户是否已注册,如果已注册,则会发送密码重置链接

我不知道如何验证这个电子邮件字段,任何帮助都非常感谢,因为我是 YII 的新手

用户.php

    class Users extends CActiveRecord
{
public $cpassword;
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
public function tableName()
    {
        return 'users';
    }
public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email, password, user_type , cpassword','required'),
            array('email', 'length', 'max'=>200),
                        array('email', 'unique'),
                        array('email', 'email'),
            array('password', 'length', 'max'=>300),
                        array('cpassword', 'length', 'max'=>300),
            array('user_type', 'length', 'max'=>5),
                        array('cpassword', 'compare', 'compareAttribute' => 'password'),

            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, email, password, user_type ', 'safe', 'on'=>'search'),
        );
    }
public function relations()
    {

return array(
        );
    }
public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'email' => 'Email',
            'password' => 'Password',
            'user_type' => 'User Type',
                        'cpassword' => 'Confirm Password'
        );
    }
public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('user_type',$this->user_type,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
        public function beforesave()
        {
            $this->password=md5($this->password);
            return true;
        }


}

最佳答案

您可以使用以下内容检查提交:

$user = User::model()->find("email = '".trim($model->email)."'");
if (!empty($user)){
     // users exists
} else {
    // user does not exist
}

如果你真的想使用该模型,你可以设置一个规则,其中电子邮件必须是唯一的,如下所示:

array('email', 'unique', 'message' => 'Email already in use'),

然后您可以检查模型是否在提交时验证,特别是电子邮件字段。如果它不验证电子邮件地址是否存在

最后,您可以像这样验证单个模型属性:

if($model->validate(array('attribute_name')) 
     // valid
}

这是完成整个 Action 的一种方法(不是最好的方法,但最容易理解!)

public function actionResetpassword(){
        $model = new User;
        if(isset($_POST['User'])){
            $model->attributes = $_POST['User']; // this is the form as completed by the user
            $user = User::model()->find("email = '".trim($model->email)."'");
            if (!empty($user)){
                // send user their new email
                $this->render("passwordreset"); // user exists, render confirmtion page
            } else {
                // user does not exist, render form and pass $error_message variable with messasge 
                $this->render("resetpassword",array(
                    "model"         =>  $model,
                    "error_message" =>  "No such user found!",
                ));
            }
    } else {
        // this will be rendered if the user has not submitted the form yet
        $this->render("resetpassword",array(
            "model" =>  $model,
        ));
    }       
}

关于php - Yii 验证特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12912853/

相关文章:

css - 如何将 Kartik typeahead 置于文本输入上方

yii - 默认为所有输入设置 successCssClass 和 errorCssClass

php - 用于发布书籍/文档的 PHP 脚本

php - Yii 验证规则 - 唯一

php - 我的 PHP 代码中解析错误 : Syntax error, 意外的文件结尾

php - 我可以为 php/postgres pg_escape_string 添加字符限制吗?如果是这样怎么办?

yii - 如何在 Yii 中的 Web 应用程序操作中调用控制台命令?

php - Yii2 - 如何从 Yii::$app->user 获取当前用户名或名称?

php - 无法使用 fopen (php) 打开文件

php - 在 Laravel 查询生成器中设置查询结果文字?