2

問題定義

「user.php」と呼ばれるモデルが 1 つあります 次のようないくつかの検証ルールがあります そのフォームでパスワード リセット フォームを作成します パスワードにテキスト ボックス名の電子メール (ユーザー モデルで使用されるものと同じ電子メール) がありますリセット フォーム このユーザーが登録済みかどうかを確認したいのですが、登録済みのユーザーがパスワードのリセット リンクを送信するかどうかを確認します。

この電子メール フィールドを確認する方法がわかりません。YII を初めて使用するので、非常に役立ちます。

user.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;
        }


}
4

2 に答える 2

6

次のような方法で送信を確認できます。

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

以下は、完全なアクションを実行する 1 つの方法です (最良の方法ではありませんが、最も理解しやすい方法です!)。

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,
        ));
    }       
}
于 2012-10-16T11:06:07.860 に答える