Adderror To A Different Model

Hi guys

I use modelA in my view, which contains data from tableA, as well as a check-box for each row.

After submission, for each checked check-box in modelA, I create a new instance of modelB, fill it with data and then save it to tableB.

The problem is that if there is a validation error for modelB, the error is not displayed in the view, which uses modelA.

So how do I add an error to modelA from within modelB?

The long solution would probably involve storing the error in session and adding it to modelA from the controller after modelB was validated. Is there a quicker way?

Thanx

I don’t see why you’d need to use sessions, you can retrieve the errors from modelB in the same controller.

Where is the data for modelB coming from if the user only has to check a checkbox to cause it to be created?

Can you post the code you’re using in your action?

Hi Keith

The code is very long, but the idea is quite simple.

It is a normal many-many relation with a junction table in the middle.

Here is what I do:

I have a gridview showing records of school classes.

If you want to add a learner to a class, then you edit that class in a CJuiTabs.

On one of the tabs I have another gridview showing all the available learners - not yet in that class. Each learner has a checkbox.

If you want to add a learner to the class you simply check his check-box.

So after submission, in the controller I simply take all the checked learners’ IDs and add them to the junction table - together with the class’s ID (which I already have).

The problem is that the junction table’s model is not shown in the view - only the class’s and the available learners’ models are shown. So if the junction table’s record does not validate (or if the user does not have permission to create records in the junction table), I have no way to show the error.

Hello,

Please check with this code…




$model->validate();

$errores = $model->getErrors();

echo "<pre>";

print_r($errores);



If you want to show a non-specific error, you could do something like the following in your controller:




    if (!$modelB->save)

        $modelA->addError('checkboxAttrName', 'Failed to add learner.');



The error would then appear alongside the checkbox.

If you want to capture the exact error from modelB, it’ll involve a little more work and there are various ways it could be done.

Both ideas work gr8.

$modelA->addErrors($modelB->getErrors());

Many thanx.