Yii Framework Forum: Form helps - Yii Framework Forum

Jump to content

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

Form helps Rate Topic: -----

#1 User is offline   Waseem Ullah Khan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-February 12

Posted 21 February 2012 - 12:55 PM

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']))
{
$w->setAttributes($_POST['Abc']); // Model is empty
print_r($w->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
0

#2 User is offline   Karl Zilles 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-June 11
  • Location:Los Angeles, United States

Posted 21 February 2012 - 01:49 PM

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

0

#3 User is offline   Waseem Ullah Khan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-February 12

Posted 26 February 2012 - 01:18 PM

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
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