Yii Framework Forum: Difrent validation rules depending on selected options - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Difrent validation rules depending on selected options Rate Topic: -----

#1 User is offline   micz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 05-June 09

Posted 11 May 2010 - 05:05 AM

I have question is it possible to create model rules that are dependent from selection. Maybe I give example.

I have radio button with two possible choices individual client and company, then i have some fields like name, address city and so on. And last field is NIP (Tax Identification Number), witch should be required only if someone chooses company in first field. How can I do that.

I know i can use scenarios, but how can I change scenario depending on changes in my model?

Thx for help.
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 11 May 2010 - 05:23 AM

I'd also use scenarios here ('client' and 'company' maybe). Then before you do validate() or save() on the model, just change the scenario:

// If your radio button attribute is clientType and 1=company, other=client:
$model->scenario = $model->clientType===1 ? 'company':'client';
if ($model->save()) 
...

1

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 11 May 2010 - 09:05 AM

The method of scenarious is confortable if you have a limited number of choices.

If, for example, you have 4 checkbox address1, address2, address3 and address4, each of them make some field to be required (country, city, zip, address), you can try a different approach.

you can add a custom rule

    public function rules()
    {
        return array(
            array(...),
            array(...),
            array('address1', 'address1Required'),
        );
    }



and then you create a function for do the validation itself:


public function address1Required()
{
    if ($this->address1)
    {
         CRequiredValidator::validate($this, array('country1', 'city1', 'zip1', 'address1'))
    }
}


1

#4 User is offline   ortenheim 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 29-August 10

Posted 02 December 2010 - 07:06 AM

View Postzaccaria, on 11 May 2010 - 09:05 AM, said:

The method of scenarious is confortable if you have a limited number of choices.

If, for example, you have 4 checkbox address1, address2, address3 and address4, each of them make some field to be required (country, city, zip, address), you can try a different approach.

you can add a custom rule

    public function rules()
    {
        return array(
            array(...),
            array(...),
            array('address1', 'address1Required'),
        );
    }



and then you create a function for do the validation itself:


public function address1Required()
{
    if ($this->address1)
    {
         CRequiredValidator::validate($this, array('country1', 'city1', 'zip1', 'address1'))
    }
}




thanks for the tip but its not wokring in my case: (yii 1.1.5)

i have a dropdown with the value "kjøpmann", this works to see this but creating the validation for the other fields is not responding

public function kjopmann()
        {
            if ($this->type == 'Kjøpmann')
            {
                CRequiredValidator::validate($this, array('kjopmann_butikk, kjopmann_region'));
            }
        }

0

#5 User is offline   vicaassharma 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-June 11

Posted 24 September 2011 - 05:27 AM

i ld like to share trick i use to on dependent validation


Assign custom validator to your Model attribute.

public function rules()
    {
        return array(
            array(...),
            array(...),
            array('type', 'customValidators'),
        );
    }



Now write another function named "customValidators" as follows and on condition basis write related validations


        public function customValidators(){
            if ((int)$this->type===1)
            {
                $labels = $this->attributeLabels(); // Getting labels of the attributes
                if((int)$this->dependendentAttribute<=0){  // dependendentAttribute is dependent on type ... must be checked only when the value of type attribute is 1
                     $this->addError("dependendentAttribute", $labels["dependendentAttribute"]." cannot be blank."); 
                }
                // More dependent on type can be written here 
            }
        }
	

0

#6 User is offline   deavt 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 03-October 13

Posted 06 February 2014 - 04:05 AM

How to add this rules to client side validation?

My model:
public function rules()
    {
        return array(
            array(...),
            array('Name', 'NameUnique'),
        );
    }
    public function NameUnique()
    {
        $m = Users::model()->findByAttributes(array('Name'=>$this->Name, 'Active'=>1));
         if(isset($m))
             $this->addError("Name", $labels["Name"]."User with this name already exist and is active."); 
    }


My view:
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'users-form',
        'enableClientValidation'=>true,
        'clientOptions'=>array(
        'validateOnSubmit'=>true,
        ),
        ));


This check in client side all other rules, but this one is checked in server side. How to fix it?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users