Form helps

I am new in Yii. I am developing some task but I have problem. I Create a Form Like this

<?php

echo CHtml::beginForm();

echo CHtml::activeLabel($w,"name");

echo CHtml::activeTextField($w,"name");

echo CHtml::activeLabel($w,"email");

echo CHtml::activeTextField($w,"email");

echo CHtml::submitButton("Register");

echo CHtml::endForm();

?>

I have also created CFormModel

<?php

class Abc extends CFormModel

{

public $name;

public $email;

}

?>

public function actionRegister()

{

$w=new Abc();

$this->render(“reg”,array(‘w’=>$w)); //reg is name of form file

if(isset($_POST[‘Abc’]))

{


&#036;w-&gt;setAttributes(&#036;_POST['Abc']); // Model is empty


print_r(&#036;w-&gt;attributes); //this line creates problem





}

}

This set of code create problem and model is empty.Can you solve this sort of task.

Thank you very much

I suspect the attributes are not being filled by the setAttributes call because you have not declared them “safe” in the rules. If this is true, you’ll see something in your logs like “attempt to set unsafe attribute”.

If this is the problem, add this to your model (class Abc):


	public function rules()

	{

		return array(

			array('name,email', 'safe'),

		);

	}



The safe attribute only affects the bulk population function: setAttributes. You can still set unsafe attributes manually, e.g.


$w->name = "Fred";

You can also override the default behavior of setAttributes to read all attributes, not just the safe ones:




public void setAttributes(array $values, boolean $safeOnly=true)

So this would also fix your problem:




$w->setAttributes($_POST['Abc'],false);

Thank You very much

Can you describe steps how to trigger validation?

I have tested this.

In register action

if($w->validate())

{

set of code

}

validation is not performed and error is not displayed

Thank you