【问题标题】:Yii validation of a specific fieldYii 验证特定字段
【发布时间】:2012-10-16 10:39:11
【问题描述】:

问题防御

我有一个名为“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;
        }


}

【问题讨论】:

  • 您是否希望根据输入的电子邮件地址检查用户是否存在?
  • 没错,但是如何只检查一个字段;为同一张表创建不同模型的好方法吗
  • 您可以使用场景,即基于不同场景的不同验证规则,例如创建新记录与更新现有记录有不同的规则。您可以在此处阅读有关场景的信息 - yiiframework.com/wiki/266/understanding-scenarios

标签: php yii


【解决方案1】:

您可以通过以下方式检查提交:

$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
}

这是执行完整操作的一种方法(不是最好的方法,但最容易理解!)

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,
        ));
    }       
}

【讨论】:

  • 我可以检查一下,数据库中是否存在电子邮件字段(如唯一),如果没有,我想抛出错误('消息' => '不是注册的电子邮件地址'))
  • 一个更简单的问题,如果我使用你的代码 $user = User::model()->find("email = '".trim($model->email)."'" ); if (!empty($user)){ // 用户存在 } else { // 用户不存在 } 发现用户未注册,如何将错误推送到对应的视图,即yii生成的表单,$form->错误($模型,'电子邮件')
  • 是的,这正是将要发生的事情。因此,这很可能会在 if(isset($_POST['U​​ser'])) { } 之后进入您的控制器。所以它正在查询数据库以查看是否有一个用户的电子邮件地址等于提交的电子邮件($model->email)
  • 对不起,我不明白你,在控制器中我确认当前用户没有重新注册的电子邮件,我如何将该错误推送到相应的视图,即在我的视图文件中,错误消息显示如下$form->error($model, 'email') ,如何将我的错误值分配给这个变量,
  • 爱斯基摩人,感谢您的宝贵回复。还有一个疑问,在您的回答中,您正在推动错误以像这样查看 this->render("resetpassword",array("model" => $model, "error_message" => "No such user found!", ));但我想将 $error_message 的值分配给 $form->error($model, 'email');这样我就可以在默认的 yii 样式中显示错误,否则我必须编写像 '.$error_message.'
    ' 之类的代码; ?> ,有什么方法可以做到这一点。谢谢你的帮助:)
【解决方案2】:
public function rules() {

    $action = Yii::app()->controller->action->id;
    if($action == 'login'){
        return array(array('username,password', 'required'), array('password', 'length', 'min'=>6, 'max'=>12), array('rememberMe', 'boolean'), array('password', 'authenticate'), array('id, jsid, email, username, password, status', 'safe', 'on'=>'search'));
    }else{
        return array( array('oldpassword,password1,password2', 'required'), array('password1,password2, oldpassword', 'length', 'min'=>6, 'max'=>12),array('password1', 'compare', 'compareAttribute'=>'password2'), array('id, jsid, email, username, password, status', 'safe', 'on'=>'search'),);    
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多