how can I have multiple scenarios but keep all attribute data for page

[size=“2”][color="#808080"]TLDR: (note: at the end I’m simply asking if there’s a better way to implement this without setting the scenario and attributes twice, once for the validation, and again with everything ‘safe’ for the page rendering)[/color]

I have a form that uses multiple models and has a few different submit buttons using different scenarios. After submitting, it needs to reload the page with everything selected how it was.

For example:[/size]

[size=“2”]There is a ‘type’ drop down used elsewhere in the form for another related model, but there is also a new type box that saves a new type_value. I need to submit the type_id so it can select whatever was chosen in the dropdown when it submits back to itself. But when creating a new type it only needs the type_value since the type_id auto increments. (There might also be an ‘update type’ button that would use the selected type_id and update it with what’s in the new type box for type_value, or a delete type button that only needs type_id.)

So when it’s submitted, I check to see which button was selected, suppose it was “new type”, then I go:




$type->setScenario('create');

$type->attributes=$_POST['Type'];

$type->save();



I have to set a scenario first as nothing is assigned to the attributes if I don’t. The problem here is, because only type_value was part of the create scenario, none of the other posted items were assigned to attributes.

So at this point I do this:


$type->setScenario('everything');

$type->attributes=$_POST['Type'];



And the ‘everything’ scenario simply sets everything to safe, so all the attributes are set and then I render the page.

In the actual use case I set scenario ‘everything’ for all models and then assign attributes right before rendering. There are a few other buttons and more complicated scenarios.

Overall, it works, and setting the attributes the second time doesn’t overwrite any validation errors that may occur.

So is there any better way of doing that? It seems clunky, but I didn’t see any other way. Do most people simply use a lot of different pages for this? There are a bunch of little options that are all inter-related with lots of foreign key relationships on this page so a single page seems the most efficient. Once on the page there’s also some dependent dropdowns and AJAX calls. Once it’s all working I’m going to also make sure everything will work and update using AJAX submits and in that case I don’t need to worry about assigning all attributes since the page is never left.[/size]

made the title a bit more clear