Yii模型验证规则

时间:2013-07-05 10:40:48

标签: php yii

yii验证规则存在问题,我创建了一个注册表单,其中包含我的产品的优惠券代码。当用户输入优惠券代码时,我想检查优惠券表中是否存在该值。 当用户输入优惠券时,我希望我的验证规则有效,否则,如果用户没有输入代码,则此验证规则不起作用,对于会员注册,我有会员模型,对于优惠券,我有优惠券模型, 我在验证规则中使用此方法。

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

任何suggesstion对我都有帮助


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



  }

1 个答案:

答案 0 :(得分:5)

如果我理解正确,您的优惠券代码不是必需的,只有用户输入优惠券才能验证优惠券代码。

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

编辑:

在yii中,即使不需要验证的字段,也将执行模型中的所有验证方法。即使您的字段couponcode不是必需的,也始终会执行验证方法isCouponCodeExist()

这意味着我们必须在方法isCouponCodeExist()中编辑您的代码以允许空的优惠券代码,如下所示:

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

此外,您不必在验证方法中返回true或false。如果出现问题,您只需添加错误。