How to load multiple context in create form

Hi everybody

I have a form that affects multiple models, but when a model fails validation I want to send error messages to the form.

If I create a form like this " this-> Form-> create ( context)" works, how can I pass multiple contexts so that my form shows me all errors from all models?

Thank you

As far as I know, Forms can only have one context.

Typically, multiple models are sent to a form in an entity that resulted from a ‘contain’ query. If your models can be pulled together through an association, then you’re golden.

This would be ideal.

$bigContext = $this->SomeTable->find()
   ->contain(['ExtraStuff'])
   ->where($conditions)
   ->toArray();

If not, you can artificially put them in the same object.


$thingOne = $this->ThingOneTable->get($thingOneId);
$notAssociatedStuff = $this->Stuff->find()->where($conditions)->toArray();

$thingOne->not_associated_stuff = $notAssociatedStuff;

I’m guessing you could make a form work with this as a context.

If you can’t do the first, and the second doesn’t work for some reason, then it’s time to look into Modelless Forms.

1 Like