ztabularinputmanager extension problem

hi, I want to thank for the useful extension ztabularinputmanager. However, the items of the “detail” won’t validate (master-detail relation). In the example given on the extension documentation (http://www.yiiframework.com/extension/ztabularinputmanager), the students won’t validate on the controller call:


$studentManager->validate($model);

I don’t know what is causing this error, since I have included ALL of the attribute error messages (on the _formStudent.php) to see what may be wrong. For example:


<?php echo $form->error($model,"id"); ?>

<?php echo $form->error($model,"id_classroom"); ?>

<?php echo $form->error($model,"description"); ?>

// etc...



At first, the error was that "id_classroom is required" and of course it was my mistake since the foreign key field is not supposed to be validate, so I removed it from the required list of the model class. Now, no error is displayed but the $studentManager->validate($model); is still returning false… any ideas on why?

I tried to check the source code of the extension (TabularInputManager.php), and I can see that the validate function doesn’t accept the $model attribute. It is defined as “public function validate()” without any arguments, so I’m mentioning this in case this might be related to the functionality that isn’t working.

The "save" function is working just fine. In the source code, it does accept the $model attribute and calls the setUnsafeAttribute function so I can actually save the whole model without any error, but I have to do this without calling the validation method on the controller. What may be wrong?

Thank you in advance!

Hi again, I think I have fixed this, but since I have changed part of the extension, I will share it here just in case this is something that really needs to be fixed in the extension as well.

In TabularInputManager.php, I changed this:


public function validate()

{

	$valid=true;

	foreach ($this->_items as $i=>$model)

	 //we want to validate all tags, even if there are errors

		$valid=$model->validate() && $valid;

	return $valid;

}



to this:




public function validate($model)

{

	$valid=true;

	foreach ($this->_items as $i=>$item)

	{

	 //we want to validate all tags, even if there are errors

		$this->setUnsafeAttribute($item, $model);

		$valid=$item->validate() && $valid;

	}

	return $valid;

}



It is now working as expected, but I’m wondering if I’m not fixing this problem in the right way, and maybe I’m silently introducing another bugs.

All comments are welcome!

Hi Tomsea!

My solution is to remove any validator from the attribute you are not setting with input.

You have some validator on some unsafe attribute, maybe the foreign key field.

Your solution works because you are setting this attribute before validation, then you validate.

The foreing key field is not supposed to be validate, so remove the validator. Leaving validator means that this attribute will be massive assigned, so if a malicious user will create an hidden field in the page with the name correct for the foreign key field, it will be assigned, so the record will be linked to a different main model.

In general, you should validate all and only the field that you are gathering from input.

So, remove your fix and try with forach($tabularinputnamager->items as $item) print_r($item->errors), you will immediatly get what is the validator that cause you problems

hi zaccaria! thanks a lot for your reply. I understand it better now.

However, it’s not clear to me why on the example in the extension documentation you have:


$studentManager->validate($model);

I don’t get why you send the $model parameter, if you’re not accepting any parameter in the function definition:


public function validate()

{

        $valid=true;

        foreach ($this->_items as $i=>$model)

         //we want to validate all tags, even if there are errors

                $valid=$model->validate() && $valid;

        return $valid;

}

Would please clear this up for us?