2012-10-08 11 views
16

वाईआई मॉडल सत्यापन नियम फ़ंक्शन कोड का उपयोग करके ईमेल को कैसे सत्यापित करें। वाईआई में मॉडल सत्यापन नियम फ़ंक्शन का उपयोग करके ईमेल की जांच कैसे करें या नहीं।वाईआई फ्रेमवर्क में ईमेल और ईमेल को पहले से मौजूद या सत्यापित नहीं करने के लिए कैसे सत्यापित करें?

उत्तर

24

आप अधिक विस्तार के लिए नीचे दिए गए

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
      //First parameter is your field name of table which has email value 
     array('email', 'email','message'=>"The email isn't correct"), 
     array('email', 'unique','message'=>'Email already exists!'),    
    ); 
} 

Yii संदर्भ लिंक के रूप में अपने मॉडल सत्यापन सेट कर सकते हैं: http://www.yiiframework.com/wiki/56/

6

आप अपने की आवश्यकता को पूरा करने के लिए अपने कस्टम सत्यापन विधि बना सकते हैं।

मॉडल कक्षा में एक समारोह बनाएँ: नियमों में

public function uniqueEmail($attribute, $params) 
{ 
    // Set $emailExist variable true or false by using your custom query on checking in database table if email exist or not. 
    // You can user $this->{$attribute} to get attribute value. 

    $emailExist = true; 

    if($emailExist) 
    $this->addError('email','Email already exists'); 
} 

उपयोगकर्ता इस सत्यापन विधि:

array('email', 'uniqueEmail','message'=>'Email already exists!'),  
3

आप आसानी से पा सकते हैं या तो ईमेल शासन को परिभाषित करते हुए पहले से ही अपने डाटाबेस में मौजूद है या नहीं ।

यहां नियम है।

array('xxx', 'unique', 'className' => 'SomeClass', 'attributeName' => 'SomeAttribute'), 

उदाहरण।

public function rules() { 
    return array(
    ... 
    array('email', 'unique', 'className' => 'User', 'attributeName' => 'email', 'message'=>'This Email is already in use'), 
    ... 
); 
} 

यहाँ मैं ईमेल, जो अद्वितीय है पर मान्यता रखना चाहते हैं, अपने मॉडल वर्ग के नाम उपयोगकर्ता है, AttributeName तालिका जैसे ईमेल के क्षेत्र का नाम है और ईमेल अपनी तालिका में पहले से ही मौजूद है तो संदेश प्रदर्शित करते हैं।

यदि यह त्रुटि देता है तो आप अपनी तालिका बदल सकते हैं और ईमेल फ़ील्ड को अद्वितीय बना सकते हैं।

ALTER तालिका उपयोगकर्ता जोड़ें अद्वितीय (ईमेल)

तब की जाँच करें।

अन्य ईमेल सत्यापन नीचे दिए गए हैं। जो मुझे लगता है कि ईमेल सत्यापन का पूरा सेट है।

public function rules() { 
    return array(
    ... 
    array('email', 'required'), 
    array('email', 'length', 'max'=>200), 
    array('email', 'email', 'message'=>'Email is not valid'), 
    array('email', 'unique', 'className' => 'User', 'attributeName' => 'email', 'message'=>'This Email is already in use'), 
    ... 
); } 

यही है। धन्यवाद

4

कस्टम सत्यापन, लघु और मीठा कोड। एक ही मॉडल में

public function rules() 
    { 
     return array(
      array('email, first_name, last_name, password, repeat_password', 'required'), 
      array('email', 'email','message'=>"The email isn't correct"), 
      array('email', 'uniqueEmail'), 
     ); 
    } 

लिखने इस कस्टम समारोह - - यह कोशिश यह ठीक काम कर रहा है

public function uniqueEmail($attribute, $params) 
    { 
     if($user = User::model()->exists('email=:email',array('email'=>$this->email))) 
      $this->addError($attribute, 'Email already exists!'); 
    } 
4

Yii2 के लिए मैं एक मॉडल है जो उपयोगकर्ता क्लास का उपयोग करेगा रजिस्टर बुलाया में निम्नलिखित का इस्तेमाल किया। अपने मॉड्यूल के अनुसार आप का इस्तेमाल किया है अपनी फ़ाइल का पालन करें:

public function rules() 
{ 
    return [ 

     ['Email', 'filter', 'filter' => 'trim'], 
     ['Email', 'required'], 
     ['Email', 'email'], 
     ['Email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'], 

    ]; 
} 

आप targetClass का उपयोग करें और कक्षा उपयोगकर्ता

0

के लिए Namepsace डाल नीचे के रूप में कुछ संशोधनों का पालन करने की जरूरत है।

मॉडल पर जाएं -> open-> users.php -> नीचे उल्लिखित रेखा संशोधित करें।कोड लिखने> ओपन _form.php-> के रूप में नीचे

<div class="users-form"> 

    <?php $form = ActiveForm::begin([ 
      'id' => $model->formName(), 
      'enableAjaxValidation' => true, 
     ]); ?> 

<?= $form->field($model, 'User_Email')->textInput(['maxlength' => true])?> 
<?= $form->field($model, 'User_Mobile')->textInput(['maxlength' => true])?> 
    <div class="form-group"> 
      <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
     </div> 

     <?php ActiveForm::end(); ?> 

    </div> 

उल्लेख अब नियंत्रक पर जाएं -

public function rules() 
    { 
     return [ 
       [['User_Email'], 'unique'], 
       [['User_Mobile'],'unique'], 
       ]; 
    } 

अब views- पर जाएं> उपयोगकर्ताओं -> खोलें UserController.php -> नीचे उल्लिखित कोड wirte

public function actionCreate() 
    { 
     if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){ 
      Yii :: $app->response->format = 'json'; 
      return \yii\bootstrap\ActiveForm::validate($model); 
      } 
} 

धन्यवाद

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

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