Yii Framework Forum: Cannot Add A Variable To The Form That Is Not In The Db - Yii Framework Forum

Jump to content

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

Cannot Add A Variable To The Form That Is Not In The Db Rate Topic: -----

#1 User is offline   Peter Hayman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 11-June 12
  • Location:Wake Forest

Posted 25 January 2013 - 08:53 AM

When I add a variable, that is not in the DB, to my model it does not show up from a form submit unless I have the rule for it defined. But when I add it to the rules, the $model->save() results in mb_strlen() expects parameter 1 to be string, array given. The variable is an array and I'm using checkboxlist widget in the form view.

Here's how the array is declared in my model:
	public $names=array();
	public function getTheNames()
	{
		return $this->names;
	}	
	public function setTheNames($value)
	{
		$this->names = array_merge ($this->names, array($value));
	}
         public function rules()
	{
		return array(
			array('names', 'length', 'max'=>64),


Thank you anyone who has an idea!!
0

#2 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 25 January 2013 - 09:15 AM

The length (=StringValidator) rule only works with strings.

I think there are 2 options:
  • If $names is not filled with user input you can declare it as safe
  • If you have to validate $names in some way write your own custom validator.

There is also an extension called
array-validator but I don't know if it is still working with current version of yii nor how stable it is.


P.S.: Declaring Validation Rules
1

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,598
  • Joined: 04-March 10
  • Location:UK

Posted 25 January 2013 - 09:15 AM

The length rule is a string validator, it's not for arrays.

Just put that attribute as a safe rule for now:
    array('names', 'safe'),


EDIT: Ninja'd
1

#4 User is offline   Peter Hayman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 11-June 12
  • Location:Wake Forest

Posted 25 January 2013 - 09:37 AM

Thanks for the advise! Before I read this I solved it by putting the non-DB array into a CFormModel and created a new object in the model and passed it to the form view. I'm guessing I didn't have to do all this and just could have made it safe.

class Tabnames extends CFormModel 
{
	public $names=array();

0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 25 January 2013 - 09:53 AM

Dear Friend

As I have similar problem in saving array from checkbox list in database.

Before saving it I converted it to string.

Before displaying it, I converted it again to array.

This way when validation fails or during update the checkbox list is nicely updated.

public function actionCreate()
	{
		$model=new Habit;

		if(isset($_POST['Habit']))
		{
			$model->attributes=$_POST['Habit'];
			if($model->habits!=='')
				$model->habits=implode(',',$model->habits);//converting to string...
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}
               $model->habits=explode(',',$model->habits);//converting to array...
		$this->render('create',array(
			'model'=>$model,
		));
	}

public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Habit']))
		{
			$model->attributes=$_POST['Habit'];
			if($model->habits!=='')
				$model->habits=implode(',',$model->habits);
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}
        $model->habits=explode(',',$model->habits);
		$this->render('update',array(
			'model'=>$model,
		));
	}



Regards.
0

#6 User is offline   Peter Hayman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 11-June 12
  • Location:Wake Forest

Posted 26 January 2013 - 08:04 AM

View Postseenivasan, on 25 January 2013 - 09:53 AM, said:

Dear Friend

As I have similar problem in saving array from checkbox list in database.

Before saving it I converted it to string.

Before displaying it, I converted it again to array.

This way when validation fails or during update the checkbox list is nicely updated.

public function actionCreate()
	{
		$model=new Habit;

		if(isset($_POST['Habit']))
		{
			$model->attributes=$_POST['Habit'];
			if($model->habits!=='')
				$model->habits=implode(',',$model->habits);//converting to string...
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}
               $model->habits=explode(',',$model->habits);//converting to array...
		$this->render('create',array(
			'model'=>$model,
		));
	}

public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Habit']))
		{
			$model->attributes=$_POST['Habit'];
			if($model->habits!=='')
				$model->habits=implode(',',$model->habits);
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}
        $model->habits=explode(',',$model->habits);
		$this->render('update',array(
			'model'=>$model,
		));
	}



Regards.

Thank you for this idea. I like it. I actually had to process the checkboxlist for creating another table list in a one to many table. So I just moved thru it with a foreach loop.

Thanks again for taking the time to share your idea. It's a good one.

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