2015-09-30 39 views
6

मैंने व्यवस्थापक पासवर्ड बदलने के लिए एक परिवर्तन पासवर्ड कार्यक्षमता बनाई है। मैंने this toutorial का उपयोग किया था।
अब मुझे $ मॉडल-> मान्य() में कोई समस्या आती है।
क्या कोई मेरी मदद कर सकता है ??Yii व्यवस्थापक के लिए पासवर्ड बदलें

नियंत्रक

public function actionIndex() 
    { 
     $id = 1; 
     $model = User::model()->findByAttributes(array('usertype' => $id)); 
     $model->setScenario('changePwd'); 

     if (isset($_POST['User'])) { 
      $model->attributes = $_POST['User']; 
      if ($model->validate()) { 
       $model->password = md5($model->new_password); 
       if ($model->save()) { 
        Yii::app()->user->setFlash('success', "Password Changed Successfully!"); 
       } 

      } else { 
       Yii::app()->user->setFlash('error', "Change Password failed!"); 
      } 
     } 

     $this->render('index', array('model' => $model)); 
    } 

मॉडल

class User extends CActiveRecord 
{ 
    public $old_password; 
    public $new_password; 
    public $repeat_password; 
    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
     return '{{user}}'; 
    } 

    /** 
    * @return array validation rules for model attributes. 
    */ 
    public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      array('usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon', 'required'), 
      array('usertype, country, area', 'numerical', 'integerOnly'=>true), 
      array('firstname, lastname, email, mobile, dob, city, street, housenumber', 'length', 'max'=>155), 
      array('password', 'length', 'max'=>225), 
      array('gender', 'length', 'max'=>6), 
      array('status', 'length', 'max'=>1), 
      array('updatedon', 'safe'), 
      // The following rule is used by search(). 
      // @todo Please remove those attributes that should not be searched. 
      array('id, usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon, updatedon, status', 'safe', 'on'=>'search'), 
      array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'), 
      array('old_password', 'findPasswords', 'on' => 'changePwd'), 
      array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'), 
     ); 
    } 

public function findPasswords($attribute, $params) 
    { 
     $user = User::model()->findByPk(Yii::app()->user->id); 
     //echo '<pre>';print_r($user);echo '</pre>'; 
     if ($user->password != md5($this->old_password)) 
      $this->addError($attribute, 'Old password is incorrect.'); 
    } 

फार्म

<div class="login_con_new"> 
    <div class="form">     
     <?php  
     $form=$this->beginWidget('CActiveForm', array(
     'id'=>'change-password-form', 
     //'action' => Yii::app()->createUrl('login/authenticate'), 
     // 'enableAjaxValidation' => FALSE, 
     'enableClientValidation' => true, 
     'clientOptions' => array('validateOnSubmit' => true,), 
     'htmlOptions' => array(
     'class' => 'form', 
     ) 
     )); 
     ?> 
     <div class="col-sm-6"> 
      <h2 class="title">Change Password</h2> 
      <?php 
    foreach(Yii::app()->user->getFlashes() as $key => $message) { 
     echo '<div class="flash-' . $key . '">' . $message . "</div>\n"; 
    } 
?> 
      <div class="form-group">    
      <?php echo $form->labelEx($model,'Current_password'); ?> 
      <?php echo $form->passwordField($model,'old_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> 
      <?php echo $form->error($model,'old_password'); ?> 
      </div> 
      <div class="form-group"> 
      <?php echo $form->labelEx($model,'new_password'); ?> 
      <?php echo $form->passwordField($model,'new_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> 
      <?php echo $form->error($model,'new_password'); ?> 
      </div> 
      <div class="form-group"> 
      <?php echo $form->labelEx($model,'repeat_password'); ?> 
      <?php echo $form->passwordField($model,'repeat_password',array('class'=>'form-control login-field','size'=>60,'maxlength'=>222)); ?> 
      <?php echo $form->error($model,'repeat_password'); ?> 
      </div>   
      <div class="form-group"> 
      <div class="col-lg-4" style="padding-left: 0px;"> 
      <?php echo CHtml::submitButton('Change',array('class' => 'btn btn-success','style'=>'color:white')); ?></div> 
     </div>  
    </div> 
    <?php $this->endWidget(); ?> 
</div>  </div> 

$ मान्य मुझे झूठा देता है और दूसरे भाग में प्रवेश करता है।

+0

शो फॉर्म भी। –

उत्तर

-1

मुझे एक विधि मिली जो मुझे नहीं पता कि उत्तर सही तरीके से है या नहीं।
मैंने पाया कि समस्या सत्यापन के दौरान होती है। इस ऑपरेशन के लिए changepassword नामक एक और मॉडल बनाया गया है, केवल नियमों में दिए गए तीन विशेषताओं को सत्यापित करें।
और यह ठीक काम किया।

<?php 
class Changepassword extends CActiveRecord 
{ 
    public $old_password; 
    public $new_password; 
    public $repeat_password; 
    /** 
    * @return string the associated database table name 
    */ 
    public function tableName() 
    { 
     return '{{user}}'; 
    } 

    /** 
    * @return array validation rules for model attributes. 
    */ 
    public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      // array('usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon', 'required'), 
      // array('usertype, country, area', 'numerical', 'integerOnly'=>true), 
      // array('firstname, lastname, email, mobile, dob, city, street, housenumber', 'length', 'max'=>155), 
      // array('password', 'length', 'max'=>225), 
      // array('gender', 'length', 'max'=>6), 
      // array('status', 'length', 'max'=>1), 
      // array('updatedon', 'safe'), 
      // The following rule is used by search(). 
      // @todo Please remove those attributes that should not be searched. 
      array('id, usertype, firstname, lastname, email, password, mobile, gender, dob, country, area, city, address, street, housenumber, extradirection, createdon, updatedon, status', 'safe', 'on'=>'search'), 
      array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'), 
      array('old_password, new_password, repeat_password','length','max'=>225), 
      array('old_password', 'findPasswords', 'on' => 'changePwd'), 
      array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'), 
     ); 
    } 

public function findPasswords($attribute, $params) 
    { 
     $user = User::model()->findByPk(Yii::app()->user->id); 
     if ($user->password != md5($this->old_password)) 
      $this->addError($attribute, 'Old password is incorrect.'); 
    } 
    /** 
    * @return array relational rules. 
    */ 
    public function relations() 
    { 
     // NOTE: you may need to adjust the relation name and the related 
     // class name for the relations automatically generated below. 
     return array(
      'events' => array(self::HAS_MANY, 'Events', 'createdby'), 
      'eventsJoinees' => array(self::HAS_MANY, 'EventsJoinee', 'userid'), 
      'eventsRatings' => array(self::HAS_MANY, 'EventsRating', 'userid'), 
      'usertype0' => array(self::BELONGS_TO, 'UserroleMaster', 'usertype'), 
      'area0' => array(self::BELONGS_TO, 'AreaMaster', 'area'), 
      'country0' => array(self::BELONGS_TO, 'CountryMaster', 'country'), 
     ); 
    } 

    /** 
    * @return array customized attribute labels (name=>label) 
    */ 
    public function attributeLabels() 
    { 
     return array(
      'old_password'=>'Current Password', 
      'new_password'=> 'New Password', 
      'repeat_password'=>'Confirm Password', 
     ); 
    } 

    /** 
    * Retrieves a list of models based on the current search/filter conditions. 
    * 
    * Typical usecase: 
    * - Initialize the model fields with values from filter form. 
    * - Execute this method to get CActiveDataProvider instance which will filter 
    * models according to data in model fields. 
    * - Pass data provider to CGridView, CListView or any similar widget. 
    * 
    * @return CActiveDataProvider the data provider that can return the models 
    * based on the search/filter conditions. 
    */ 
    public function search() 
    { 
     // @todo Please modify the following code to remove attributes that should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id); 
     $criteria->compare('usertype',$this->usertype); 
     $criteria->compare('firstname',$this->firstname,true); 
     $criteria->compare('lastname',$this->lastname,true); 
     $criteria->compare('email',$this->email,true); 
     $criteria->compare('password',$this->password,true); 
     $criteria->compare('mobile',$this->mobile,true); 
     $criteria->compare('gender',$this->gender,true); 
     $criteria->compare('dob',$this->dob,true); 
     $criteria->compare('country',$this->country); 
     $criteria->compare('area',$this->area); 
     $criteria->compare('city',$this->city,true); 
     $criteria->compare('address',$this->address,true); 
     $criteria->compare('street',$this->street,true); 
     $criteria->compare('housenumber',$this->housenumber,true); 
     $criteria->compare('extradirection',$this->extradirection,true); 
     $criteria->compare('createdon',$this->createdon,true); 
     $criteria->compare('updatedon',$this->updatedon,true); 
     $criteria->compare('status',$this->status,true); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

    /** 
    * Returns the static model of the specified AR class. 
    * Please note that you should have this exact method in all your CActiveRecord descendants! 
    * @param string $className active record class name. 
    * @return User the static model class 
    */ 
    public static function model($className=__CLASS__) 
    { 
     return parent::model($className); 
    } 
} 

?> 

मित्र अगर कोई सही विधि प्राप्त करता है तो कृपया इसे पोस्ट करें।

1

मुझे लगता है कि इस लाइन में $model = User::model()->findByAttributes(array('usertype' => $id)); आपने usertype के लिए गलती की है। यह उपयोगकर्ता आईडी है।

+0

@ दनिला गंचर: मैंने मैन्युअल रूप से $ id = 1 असाइन किया है, क्योंकि आईडी 1 व्यवस्थापक है। मैंने व्यवस्थापक को पासवर्ड बदलने के लिए यह किया था। नियंत्रक में समस्या होती है सत्यापन को ओवरराइड नहीं करता है। –

+0

@ मोहम्मद इकबाल क्या आप अपना पोस्ट डेटा दिखा सकते हैं? और ** मान्य() ** के बाद मॉडल में अपनी त्रुटियां दिखाएं?'var_dump ($ model-> getErrors());' –

+0

@ मोहम्मद इकबाल और इस '$ मॉडल = नए उपयोगकर्ता को बदलें; $ मॉडल = उपयोगकर्ता :: मॉडल() -> findByAttributes (सरणी ('usertype' => $ id)); 'to' $ model = उपयोगकर्ता :: मॉडल() -> findByPk ($ id); ' –

1

इस प्रकार की त्रुटियों को डीबग करने का सबसे अच्छा तरीका वास्तव में जांचना क्यों वैध है झूठी वापसी।

यह मॉडल पर errors की जांच करके किया जा सकता है। आप या तो फ़्लैश संदेश में सभी त्रुटियों को आउटपुट कर सकते हैं, इसलिए उपयोगकर्ता को पता होना चाहिए कि क्या सही करना है, या इसे डीबग करने में आपकी सहायता के लिए इसे var_dump में डालें।

इस राशि के लिए अपने नियंत्रक हिस्सा बदलें:

if($valid) 
{ 
    $model->password = md5($model->new_password); 
    if($model->save()) 
    {  
     Yii::app()->user->setFlash('success', "Password Changed Successfully!"); 
     // $this->redirect(array('dashboard/index', 'id' => 1)); 
    } 
    else 
    { 
     Yii::app()->user->setFlash('error', "Change Password failed!"); 
    } 
} 
else 
{ 
    var_dump($model->errors); 
    die(); 
} 

यह आप एक सरणी में सत्यापन त्रुटि दिखाई देगा। यह प्रदर्शित करना कि किस विशेषता में सत्यापन त्रुटि है।

यदि आप केवल पासवर्ड फ़ील्ड को सत्यापित और सहेजना चाहते हैं। आप उन्हें वैध और विधि में सहेज सकते हैं।

$model->validate(array('password')) and $model->save(TRUE, array('password')) 
+0

VdBerge: इस विधि से मुझे पता चला कि यह एक सत्यापन त्रुटि है .. मैं पासवर्ड बदलने पर दायर लोगों को मान्य नहीं करता हूं। मुझे –

+0

पर सवारी करने की आवश्यकता है, – ThomasVdBerge

0

आप अपडेट के लिए सेनारियो का उपयोग कर सकते हैं। पसंद: http://www.yiiframework.com/forum/index.php/topic/31357-priority-to-rules-with-a-scenario/

+0

के लिए उत्तर जोड़ने के लिए मेरी पोस्ट बदल दी गई है बाहरी साइट पर लिंक पोस्ट करने के बजाय उत्तर प्रदान करते हुए, यहां जवाब पोस्ट करना बेहतर है। लिंक टूटा आदि हो सकता है ... – SnIpY

+0

प्रिय यह आधिकारिक वाईआई फ्रेमवर्क साइट है। तो टूटी हुई लिंक के लिए यह दुर्लभ मामला है। –

संबंधित मुद्दे