Yii Framework Forum: Widget : Undefined Index Passing Variable - Yii Framework Forum

Jump to content

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

Widget : Undefined Index Passing Variable Rate Topic: -----

#1 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 13 November 2012 - 06:31 AM

Hi all....

I did my widget...its working well..i mean..i get all category and sub category..
now the problem its that i get "Undefined index" for categoryIds variable..
how can i pass a variable from inside a widget to the actionCreat function...?
thank you so much...this its the code:

WIDGET CLASS:
class TutteCategorie extends CWidget 
{

     private $_parent;
   
     public function init()
               {
				 $this->_parent  = Category::model()->findAll('parent_id = 0'); 
			
      		   }
     
         
     
	 
	 
     public function getTutteCategorie()
     {
       return $this->_parent;
     }
     
     
     public function run()
       {
           // this method is called by CController::endWidget()
           $this->render('tutteCategorie');
       }
}


WIDGET VIEW
foreach($this->getTutteCategorie() as $parent): 
		{
	
	$child= $parent->getChilds(); 
			
			//var_dump($child);
	echo $parent->nome ;	echo "<br />";	
 	echo CHtml::activeCheckBoxList(
    $parent,
    'categoryIds',
   $parents = Chtml::listData(Category::model()->findAll('parent_id = :id',array(':id'=> $parent->id)),'id','nome')   );
          

			} 
 
  endforeach;


PostController
if($model->save())

		 foreach ($_POST['Post']['categoryIds'] as $categoryId) {
                $postCategory = new PostCategory;
                $postCategory->post_id = $model->id;
                $postCategory->category_id = $categoryId;
                if (!$postCategory->save()) print_r($postCategory->errors);
        
				}

				$this->redirect(array('view','id'=>$model->id));
			
		}
		


this is my form view HTML

<div class="row">
 Category 1 <input type="hidden" name="Category[categoryIds]" value="" id="ytCategory_categoryIds">

<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Cat1 - Sub-Category 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Cat1 - Sub-Category 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Cat1 - Sub-Category 3</label>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Cat1 - Sub-Category 4</label></span>

Category 2 <input type="hidden" name="Category[categoryIds]" value="" id="ytCategory_categoryIds">

<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Cat2 - Sub-Category 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Cat2 - Sub-Category 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Cat2 - Sub-Category 3</label>
</span>     
 
</div>

i want to save in the post_categories junction table the values stored in the sub categories field....

thank you all!! :) :)
0

#2 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,305
  • Joined: 21-February 11
  • Location:Greece

Posted 13 November 2012 - 07:46 AM

Hi oflodor


where the message with undefined categoryIds occurs? On submit or in the first time form shows?

check with var_dump if $parent->getChilds() if exist the field categoryIds
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 13 November 2012 - 08:33 AM

View Postkonapaz, on 13 November 2012 - 07:46 AM, said:

Hi oflodor


when occurs the message with undefined categoryIds? On submit or in the first time form shows?

check with var_dump if $parent->getChilds() if exist the field categoryIds


hi konapaz!
i get that code when i sumbit the form creat post
i put var_dump and i get:
array(4) { [0]=> object(Category)#104 (13) { ["categoryIds"]=> NULL ["_md":"CActiveRecord":private]=> object(CActiveRecordMetaData)#96 (5) { ["tableSchema"]=> object(CMysqlTableSchema)#91 (9) { ["schemaName"]=> NULL ["name"]=> string(8) "category" ["rawName"]=> string(10) "`category`" ["primaryKey"]=> string(2) "id" ["sequenceName"]=> string(0) "" ["foreignKeys"]=> array(0) { } ["columns"]=> array(3) { ["id"]=> object(CMysqlColumnSchema)#92 (14) { ["name"]=> string(2) "id" ["rawName"]=> string(4) "`id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> NULL ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["nome"]=> object(CMysqlColumnSchema)#93 (14) { ["name"]=> string(4) "nome" ["rawName"]=> string(6) "`nome`" ["allowNull"]=> bool(false) ["dbType"]=> string(12) "varchar(255)" ["type"]=> string(6) "string" ["defaultValue"]=> NULL ["size"]=> int(255) ["precision"]=> int(255) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["parent_id"]=> object(CMysqlColumnSchema)#97 (14) { ["name"]=> string(9) "parent_id" ["rawName"]=> string(11) "`parent_id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> int(0) ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["columns"]=> array(3) { ["id"]=> ......................


i dont paste all the code because its very long but it seems that the variables are ok....thank you!! :) :)
0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,305
  • Joined: 21-February 11
  • Location:Greece

Posted 13 November 2012 - 08:42 AM

View Postoflodor, on 13 November 2012 - 08:33 AM, said:

hi konapaz!
i get that code when i sumbit the form creat post
i put var_dump and i get:
array(4) { [0]=> object(Category)#104 (13) { ["categoryIds"]=> NULL ["_md":"CActiveRecord":private]=> object(CActiveRecordMetaData)#96 (5) { ["tableSchema"]=> object(CMysqlTableSchema)#91 (9) { ["schemaName"]=> NULL ["name"]=> string(8) "category" ["rawName"]=> string(10) "`category`" ["primaryKey"]=> string(2) "id" ["sequenceName"]=> string(0) "" ["foreignKeys"]=> array(0) { } ["columns"]=> array(3) { ["id"]=> object(CMysqlColumnSchema)#92 (14) { ["name"]=> string(2) "id" ["rawName"]=> string(4) "`id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> NULL ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(true) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(true) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["nome"]=> object(CMysqlColumnSchema)#93 (14) { ["name"]=> string(4) "nome" ["rawName"]=> string(6) "`nome`" ["allowNull"]=> bool(false) ["dbType"]=> string(12) "varchar(255)" ["type"]=> string(6) "string" ["defaultValue"]=> NULL ["size"]=> int(255) ["precision"]=> int(255) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["parent_id"]=> object(CMysqlColumnSchema)#97 (14) { ["name"]=> string(9) "parent_id" ["rawName"]=> string(11) "`parent_id`" ["allowNull"]=> bool(false) ["dbType"]=> string(7) "int(11)" ["type"]=> string(7) "integer" ["defaultValue"]=> int(0) ["size"]=> int(11) ["precision"]=> int(11) ["scale"]=> NULL ["isPrimaryKey"]=> bool(false) ["isForeignKey"]=> bool(false) ["autoIncrement"]=> bool(false) ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } ["columns"]=> array(3) { ["id"]=> ......................


i dont paste all the code because its very long but it seems that the variables are ok....thank you!! :) :)



Before of foreach var_dump the $_POST['Post'] to see what variables are passed

foreach ($_POST['Post']['categoryIds'] as $categoryId) {
.....
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 13 November 2012 - 09:02 AM

View Postkonapaz, on 13 November 2012 - 08:42 AM, said:

Before of foreach var_dump the $_POST['Post'] to see what variables are passed

foreach ($_POST['Post']['categoryIds'] as $categoryId) {
.....


you mean in the PostController ? i put this

	
if($model->save())
		
	  var_dump ( $_POST['Post']) ;
		  
		    foreach ($_POST['Post']['categoryIds'] as $categoryId) {
                $postCategory = new PostCategory;
                $postCategory->post_id = $model->id;
                $postCategory->category_id = $categoryId;
                if (!$postCategory->save()) print_r($postCategory->errors);
        
				}
				
		
	$this->redirect(array('view','id'=>$model->id));
	
		}



and after i submit the form nothing its printed..only
Undefined index: categoryIds 

0

#6 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,305
  • Joined: 21-February 11
  • Location:Greece

Posted 13 November 2012 - 09:15 AM

View Postoflodor, on 13 November 2012 - 09:02 AM, said:

you mean in the PostController ? i put this

	
if($model->save())
		
	  var_dump ( $_POST['Post']) ;
		  
		    foreach ($_POST['Post']['categoryIds'] as $categoryId) {
                $postCategory = new PostCategory;
                $postCategory->post_id = $model->id;
                $postCategory->category_id = $categoryId;
                if (!$postCategory->save()) print_r($postCategory->errors);
        
				}
				
		
	$this->redirect(array('view','id'=>$model->id));
	
		}



and after i submit the form nothing its printed..only
Undefined index: categoryIds 



put the var_dump ($_POST['Category']) or var_dump ($_POST) better, before of save() call
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
1

#7 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 13 November 2012 - 09:22 AM

View Postkonapaz, on 13 November 2012 - 09:15 AM, said:

put the var_dump ($_POST['Category']) or var_dump ($_POST) better, before of save() call


i did.. but nothing its printed !! :blink: :blink:

	  var_dump ( $_POST['Post']) ;
	 var_dump ( $_POST['Category']) ;	  
		  	if($model->save())
		
		    foreach ($_POST['Post']['categoryIds'] as $categoryId) {
                $postCategory = new PostCategory;
                $postCategory->post_id = $model->id;
                $postCategory->category_id = $categoryId;
                if (!$postCategory->save()) print_r($postCategory->errors);
        
				}



neither print anything !! :unsure: :unsure:
0

#8 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,305
  • Joined: 21-February 11
  • Location:Greece

Posted 13 November 2012 - 09:32 AM

check with var_dump ($_POST) ;

make your form accordingly of
http://www.yiiframew....1/en/form.view
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#9 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 13 November 2012 - 11:03 AM

View Postkonapaz, on 13 November 2012 - 09:32 AM, said:

check with var_dump ($_POST) ;

make your form accordingly of
http://www.yiiframew....1/en/form.view


thank you konapaz...
0

#10 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,305
  • Joined: 21-February 11
  • Location:Greece

Posted 13 November 2012 - 12:28 PM

View Postoflodor, on 13 November 2012 - 11:03 AM, said:

thank you konapaz...


Welcome :)
Don't feel uncomfortable if I/we dont find the solution directly!
Try to solve the problem byself and if you don't find the solution, we will try one of us of this forum to give the answer :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#11 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 14 November 2012 - 03:29 PM

View PostKonApaz, on 13 November 2012 - 12:28 PM, said:

Welcome :)
Don't feel uncomfortable if I/we dont find the solution directly!
Try to solve the problem byself and if you don't find the solution, we will try one of us of this forum to give the answer :)



Hi...so i got what its the problem in the form:

this its the code of the widget:

CLASS:
class TutteCategorie extends CWidget 
{

     private $_parent;
     public $categoryIds;
     public function init()
               {
				
 $this->_parent  = Post::model()->stampaCategorie('parent_id = 0'); 
		
			
      		   }
  
	 
     public function getTutteCategorie()
     {
       return $this->_parent;
     }
     
     
     public function run()
       {
        
           $this->render('tutteCategorie', array($this->categoryIds));
       }
}

VIEW
<?php 


foreach($this->getTutteCategorie() as $parent): 
		{
	

 echo CHtml::activeCheckBoxList(
    $parent,
    'categoryIds',
   Chtml::listData(Post::model()->getChilds($parent->id),'id','nome')   );
          

			} 
 
  endforeach; ?>



the problem its that the html output is this:
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 1</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 2</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 3</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Name 4</label></span>

<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 5</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 6</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 7</label>
</span>

and as you can see the variables are duplicated (Category_categoryIds_0,Category_categoryIds_1 ecc...)
so if i select the last 4 variables it works well and create the post
otherwise if i check the frist variables( froma Name 1 to 4) it doesn work...

the code should be like this:
<span id="Category_categoryIds">
<input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Aborto</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Eutanasia</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Famiglia</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Pedofilia</label></span>

<input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_5"> <label for="Category_categoryIds_5">Aborto</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_6"> <label for="Category_categoryIds_6">Europa</label><br>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_7"> <label for="Category_categoryIds_7">Famiglia</label>
</span>


how can i get that ?!? <_< <_< <_<
thank very much for your help...
0

#12 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 14 November 2012 - 04:01 PM

my 2cts: maybe you can pass an extra htmlOption in the activeCheckBoxList like :
$base_id='my_custom_';
echo CHtml::activeCheckBoxList(
    $parent,
    'categoryIds',
   Chtml::listData(Post::model()->getChilds($parent->id),'id','nome'),
   array('id'=>$base_id.$parent->id);

By reading checkBoxList source code, I'm not sure that it is possible ...
Hey Ho !
Let's go !
0

#13 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 14 November 2012 - 05:24 PM

View Postluc, on 14 November 2012 - 04:01 PM, said:

my 2cts: maybe you can pass an extra htmlOption in the activeCheckBoxList like :
$base_id='my_custom_';
echo CHtml::activeCheckBoxList(
    $parent,
    'categoryIds',
   Chtml::listData(Post::model()->getChilds($parent->id),'id','nome'),
   array('id'=>$base_id.$parent->id);

By reading checkBoxList source code, I'm not sure that it is possible ...

thank you luc for the reply...i tried but the html now its


<input type="hidden" name="Category[categoryIds]" value="" id="ytmy_custom_5">
<span id="Category_categoryIds"><input type="checkbox" name="Category[categoryIds][]" value="1" id="Category_categoryIds_0"> 
<label for="Category_categoryIds_0">Name 1</label>
<input type="checkbox" name="Category[categoryIds][]" value="2" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 2</label>
<input type="checkbox" name="Category[categoryIds][]" value="3" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 3</label>
<input type="checkbox" name="Category[categoryIds][]" value="4" id="Category_categoryIds_3"> <label for="Category_categoryIds_3">Name 4</label></span>

<input type="hidden" name="Category[categoryIds]" value="" id="ytmy_custom_6">
<span id="Category_categoryIds"><input type="checkbox" name="Category[categoryIds][]" value="7" id="Category_categoryIds_0"> <label for="Category_categoryIds_0">Name 7</label>
<input type="checkbox" name="Category[categoryIds][]" value="8" id="Category_categoryIds_1"> <label for="Category_categoryIds_1">Name 8</label>
<input type="checkbox" name="Category[categoryIds][]" value="9" id="Category_categoryIds_2"> <label for="Category_categoryIds_2">Name 9</label>
</span>     
 


and doesnt work...if i select the check box of the first group (ytmy_custom_5)
i get this message...

Invalid argument supplied for foreach() 

  if($model->save())
   
          
                 foreach ($_POST['Category']['categoryIds'] as $categoryId) {
                $postCategory = new PostCategory;
                $postCategory->post_id = $model->id;
                $postCategory->category_id = $categoryId;
                if (!$postCategory->save()) print_r($postCategory->errors);
         }

0

#14 User is offline   luc 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 22-June 10
  • Location:france

Posted 14 November 2012 - 05:38 PM

it's strange. Ithought that my trick will give a custom id to the onput of type="checkbox" and not on the hiddenfield ...

activeCheckBoxList html options should apply to each checkbox input as sayd the documentation
Hey Ho !
Let's go !
0

#15 User is offline   oflodor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-November 12
  • Location:Italy

Posted 15 November 2012 - 01:21 AM

View Postluc, on 14 November 2012 - 05:38 PM, said:

it's strange. Ithought that my trick will give a custom id to the onput of type="checkbox" and not on the hiddenfield ...

activeCheckBoxList html options should apply to each checkbox input as sayd the documentation

hi luc thanks...it works but change this value ytmy_custom_5 ytmy_custom_6
and no the checkbox inside...yes strange..
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