2013-07-05 3 views
0

Salut à vous tous de grands développeurs php,validation du modèle Yii règles

J'ai un problème dans une règle de validation de yu, j'ai créé un formulaire d'inscription avec un code promo pour mon produit. Lorsque l'utilisateur saisit un code promo, je veux vérifier si la valeur est présente dans la table des coupons ou non. Lorsque l'utilisateur saisit le code de validation je souhaite que ma règle de validation fonctionne, sinon l'utilisateur ne saisira pas le code cette règle de validation ne devrait pas fonctionner, pour l'inscription des membres j'ai un modèle membre et pour le coupon j'ai un modèle de coupon J'utilise cette méthode dans les règles de validation.

class MemberSignup extends CActiveRecord 
{ 
    public $couponcode; 

    public function rules(){ 
     array('couponcode', 'isCouponCodeExist'), 
    }//end rules 

    public function isCouponCodeExist($attribute, $params) 
    {  
     $record = Coupon::model()->findByAttributes(array('couponcode' => $this->couponcode)); 

     if($record === null){ 
      $this->addError($attribute, 'Invalid Coupon'); 
      return false; 
     } 

     return true; 
    } 
} //class end 

tout suggesstion sera helpfull pour moi


<?php 

class MemberSignup extends CActiveRecord 
    { 
public $confPassword; 
public $couponcode; 
/** 
* Returns the static model of the specified AR class. 
* @param string $className active record class name. 
* @return MemberSignup the static model class 
*/ 
public static function model($className=__CLASS__) 
{ 
    return parent::model($className); 
} 

/** 
* @return string the associated database table name 
*/ 
public function tableName() 
{ 
    return 'members'; 
} 

/** 
* @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('member_login, member_password,gateway_id, confPassword,email, first_name, packageid,agreed,trafficesource', 'required'),   

     array('couponcode', 'isCouponCodeExist'),   

     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 

     array('member_id, member_login', 'safe', 'on'=>'search'), 
    ); 
} 


public function isCouponCodeExist($attribute,$params){    

    $record=Coupon::model()->findByAttributes(array('couponcode'=>$this->couponcode)); 

    if($record===null){ 
     $this->addError($attribute, 'Invalid Coupon');   
    }   

} 

/** 
* @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(
    ); 
} 

/** 
* @return array customized attribute labels (name=>label) 
*/ 
public function attributeLabels() 
{ 
    return array(
     'member_id' => 'Member', 
     'member_login' => 'Username', 
     'user_id' => 'User', 
     'member_password' => 'Password', 
     'confPassword' =>'Confirm Password', 
     'member_level' => 'Member Level', 
     'affiliate_id' => 'Affiliate', 
     'first_name' => 'First Name', 
     'last_name' => 'Last Name', 
     'email' => 'Email', 
     'address' => 'Address', 
     'city' => 'City', 
     'state' => 'State', 
     'country' => 'Country', 
     'zip' => 'Zip', 
     'home_phone' => 'Home Phone', 
     'work_phone' => 'Work Phone', 
     'refered_by' => 'Refered By', 
     'location' => 'Location', 
     'product_id' => 'Product', 
     'product_path' => 'Product Path', 
     'product_description' => 'Product Description', 
     'confirmation_hash' => 'Confirmation Hash', 
     'status' => 'Status', 
     'cancellation_reason' => 'Cancellation Reason', 
     'cancellation_date' => 'Cancellation Date', 
     'registration_date' => 'Registration Date', 
     'next_billingdate' => 'Next Billingdate', 
     'CC_no' => 'Cc No', 
     'CC_expiry' => 'Cc Expiry', 
     'last_login' => 'Last Login', 
     'total_rebillings' => 'Total Rebillings', 
     'ufa_list_size' => 'Ufa List Size', 
     'billing_amount' => 'Billing Amount', 
     'privilege' => 'Privilege', 
     'maximportlimit' => 'Maximportlimit', 
     'mailingcount' => 'Mailingcount', 
     'mailinglimit' => 'Mailinglimit', 
     'registration_ip' => 'Registration Ip', 
     'address2' => 'Address2', 
     'Reactivation_Note' => 'Reactivation Note', 
     'call_date' => 'Call Date', 
     'CC_last_four' => 'Cc Last Four', 
     'slidenumber' => 'Slidenumber', 
     'domain' => 'Domain', 
     'registerdomain' => 'Registerdomain', 
     'gb1_affilateID' => 'Gb1 Affilate', 
     'agreed' => 'Agreed', 
     'packageid' => 'Packageid', 
     'ppid' => 'Ppid', 
     'sendmeitemizedbill' => 'Sendmeitemizedbill', 
     'is_superstarmember' => 'Is Superstarmember', 
     'activationdate' => 'Activationdate', 
     'reactivationdate' => 'Reactivationdate', 
     'suspensiondate' => 'Suspensiondate', 
     'is_editor' => 'Is Editor', 
     'mobile_phone' => 'Mobile Phone', 
     'member_quta' => 'Member Quta', 
     'notification' => 'Notification', 
     'cancellationrequest' => 'Cancellationrequest', 
     'siteiD' => 'Sitei D', 
     'companyname' => 'Companyname', 
     'companywebsite' => 'Companywebsite', 
     's3_quota' => 'S3 Quota', 
     's3_quota_consume' => 'S3 Quota Consume', 
     'gateway_id' => 'Gateway', 
     'invoice_id' => 'Invoice', 
     'couponid' => 'Couponid', 
     'coupon_success' => 'Coupon Success', 
     'dont_cancel' => 'Dont Cancel', 
     'notes' => 'Notes', 
     'trafficesource' => 'Traffice Source', 
     'othersource' => 'Othersource', 
     'couponcode'=>'Coupon Code', 
    ); 
} 

/** 
* Retrieves a list of models based on the current search/filter conditions. 
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
*/ 
public function search() 
{ 
    // Warning: Please modify the following code to remove attributes that 
    // should not be searched. 

    $criteria=new CDbCriteria; 

    $criteria->compare('member_id',$this->member_id); 
    $criteria->compare('member_login',$this->member_login,true); 
    $criteria->compare('user_id',$this->user_id); 
    $criteria->compare('member_password',$this->member_password,true); 
    $criteria->compare('member_level',$this->member_level); 
    $criteria->compare('affiliate_id',$this->affiliate_id,true); 
    $criteria->compare('first_name',$this->first_name,true); 
    $criteria->compare('last_name',$this->last_name,true); 
    $criteria->compare('email',$this->email,true); 
    $criteria->compare('address',$this->address,true); 
    $criteria->compare('city',$this->city,true); 
    $criteria->compare('state',$this->state,true); 
    $criteria->compare('country',$this->country,true); 
    $criteria->compare('zip',$this->zip,true); 
    $criteria->compare('home_phone',$this->home_phone,true); 
    $criteria->compare('work_phone',$this->work_phone,true); 
    $criteria->compare('refered_by',$this->refered_by,true); 
    $criteria->compare('location',$this->location,true); 
    $criteria->compare('product_id',$this->product_id); 
    $criteria->compare('product_path',$this->product_path,true); 
    $criteria->compare('product_description',$this->product_description,true); 
    $criteria->compare('confirmation_hash',$this->confirmation_hash,true); 
    $criteria->compare('status',$this->status,true); 
    $criteria->compare('cancellation_reason',$this->cancellation_reason,true); 
    $criteria->compare('cancellation_date',$this->cancellation_date,true); 
    $criteria->compare('registration_date',$this->registration_date,true); 
    $criteria->compare('next_billingdate',$this->next_billingdate,true); 
    $criteria->compare('CC_no',$this->CC_no,true); 
    $criteria->compare('CC_expiry',$this->CC_expiry,true); 
    $criteria->compare('last_login',$this->last_login,true); 
    $criteria->compare('total_rebillings',$this->total_rebillings); 
    $criteria->compare('ufa_list_size',$this->ufa_list_size); 
    $criteria->compare('billing_amount',$this->billing_amount); 
    $criteria->compare('privilege',$this->privilege,true); 
    $criteria->compare('maximportlimit',$this->maximportlimit); 
    $criteria->compare('mailingcount',$this->mailingcount,true); 
    $criteria->compare('mailinglimit',$this->mailinglimit,true); 
    $criteria->compare('registration_ip',$this->registration_ip,true); 
    $criteria->compare('address2',$this->address2,true); 
    $criteria->compare('Reactivation_Note',$this->Reactivation_Note,true); 
    $criteria->compare('call_date',$this->call_date,true); 
    $criteria->compare('CC_last_four',$this->CC_last_four,true); 
    $criteria->compare('slidenumber',$this->slidenumber,true); 
    $criteria->compare('domain',$this->domain,true); 
    $criteria->compare('registerdomain',$this->registerdomain,true); 
    $criteria->compare('gb1_affilateID',$this->gb1_affilateID,true); 
    $criteria->compare('agreed',$this->agreed,true); 
    $criteria->compare('packageid',$this->packageid); 
    $criteria->compare('ppid',$this->ppid); 
    $criteria->compare('sendmeitemizedbill',$this->sendmeitemizedbill,true); 
    $criteria->compare('is_superstarmember',$this->is_superstarmember); 
    $criteria->compare('activationdate',$this->activationdate,true); 
    $criteria->compare('reactivationdate',$this->reactivationdate,true); 
    $criteria->compare('suspensiondate',$this->suspensiondate,true); 
    $criteria->compare('is_editor',$this->is_editor); 
    $criteria->compare('mobile_phone',$this->mobile_phone,true); 
    $criteria->compare('member_quta',$this->member_quta,true); 
    $criteria->compare('notification',$this->notification,true); 
    $criteria->compare('cancellationrequest',$this->cancellationrequest,true); 
    $criteria->compare('siteiD',$this->siteiD); 
    $criteria->compare('companyname',$this->companyname,true); 
    $criteria->compare('companywebsite',$this->companywebsite,true); 
    $criteria->compare('s3_quota',$this->s3_quota); 
    $criteria->compare('s3_quota_consume',$this->s3_quota_consume); 
    $criteria->compare('gateway_id',$this->gateway_id,true); 
    $criteria->compare('invoice_id',$this->invoice_id); 
    $criteria->compare('couponid',$this->couponid); 
    $criteria->compare('coupon_success',$this->coupon_success); 
    $criteria->compare('dont_cancel',$this->dont_cancel); 
    $criteria->compare('notes',$this->notes,true); 
    $criteria->compare('trafficesource',$this->trafficesource,true); 
    $criteria->compare('othersource',$this->othersource,true); 

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



    } 
+0

Comment avez-vous instancié ce modèle? –

Répondre

5

Si je vous ai bien compris, votre couponcode n'est pas nécessaire et que vous souhaitez valider le couponcode que si l'utilisateur a entré un.

public function rules() 
{ 
    return array(
     array('field1, field2, field3', 'required'), 
     array('couponcode', 'isCouponCodeExist'), 
    ); 
} 

EDIT:

En yu, toutes les méthodes de validation d'un modèle sera exécuté, même si n'est pas nécessaire le champ qui est en cours de validation. Même si votre champ couponcode n'est pas requis, la méthode de validation isCouponCodeExist() sera toujours exécutée.

Cela signifie que nous allons devoir modifier votre code dans la méthode isCouponCodeExist() pour permettre à un couponcode vide, un petit quelque chose comme ceci:

public function isCouponCodeExist($attribute, $params) 
{ 
    if(!empty($this->couponcode)) 
    {  
     $record = Coupon::model()->findByAttributes(array('couponcode' => $this->couponcode)); 

     if($record === null) 
     { 
      $this->addError($attribute, 'Invalid Coupon'); 
     } 
    } 
} 

En outre, vous n'avez pas retourner vrai ou faux méthodes de validation. Tout ce que vous devez faire est d'ajouter une erreur si quelque chose ne va pas.

+0

Merci, je n'ai pas couponcode dans le tableau nécessaire –

+0

'array ('member_login, member_password, gateway_id, confPassword, email, prenom, PackageID, a accepté, trafficesource', 'nécessaire'), \t \t \t tableau \t ('couponcode' , 'isCouponCodeExist'), 'Si je laisse champ de code promo vide il me renvoie cette erreur" Veuillez corriger les erreurs de saisie suivantes: Invalid Coupon " –

+0

sont les seules 2 règles dans votre méthode' rules() '? – deacs

Questions connexes