Required

Hi guys, I have a form which consists of textfields and a single checkbox. At the moment about half of the textfields are required to be filled out. What i want is to have the other half be required if, and only if, the checkbox is checked. How can i do that?

Dear Freind

Trying to simulate your scenario…

If one chooses the marital status true, spouseName and spouseAge are required.




class Profile extends CFormModel

{

	public $name;

	public $age;

	public $married; //checkBox field

	public $spouseName;

	public $spouseAge;

	

	public function rules()

	{

		

		return array(

			array('name,age', 'required'),

			array('age,spouseAge', 'numerical','integerOnly'=>true),

			array('married','safe'),//Important to declare it safe to get its value in side the custom validator method.

			array('spouseName',"checkMaritalStatus"),

			array('spouseAge',"checkMaritalStatus"),

			

			

		);

	}

	

	public function checkMaritalStatus($attribute, $params)

	{

		if($this->married)

		{

			if($this->$attribute=="")

				$this->addError($attribute,$attribute." is reqiured.");

		}

		

		else

		{

			if($this->$attribute!=="" )

				$this->addError($attribute,"You have not checked the marital status.");

		}

	}

}



Regards.

Thank you, it worked :)