Forums

Set Value of Field

gotpowr 05 Sep, 2024

I have a form that allows new submissions, or editing existing submissions.

Most of it works fine.

However, I want to load data into certain fields IF it's a "new" submission.

I have 2 models, 1 for new/existing registrations, 1 for saved options

registration  (model for new/existing)

savedoption (value I want to use for NEW registrations only)

For a dynamic dropdown, I found a simple solution.  The dropdown is named registration.vehicle1_number

I use the "dynamic selections" and set to {var: savedoption.vehicle1_number}.  It seems to default to "registration.vehicle1_number" when loading an existing form and properly populate with "savedoption.vehicle1_number" when using a new registration.

Perfect.

However, I'm having an issue with the checkboxes.  I can't get them to work properly.

They're named registration.vehicle1_24ghz.  I tried putting "savedoption.vehicle1_24ghz" in the checked field, and {var:savedoption.vehicle1_24ghz} and neither worked.

I tried adding a custom PHP to the "not found" on the read data action for an existing registration form.

$this->set('registration.vehicle1_24ghz','savedoption.vehicle1_24ghz') but that didn't work either.

How can I set the value on registration.vehicle1_24ghz using some sort of action?

gotpowr 06 Sep, 2024
Answer
1 Likes

I found a way to do this and wanted to post if anyone was looking for the same thing.

I have 2 reads set up.  1 using model "savedoption" followed by "registration"

In the "registration" one, the "not found" section, I added a "variables" action.

In variables, I set up as follows:  Type: "Request"  name: "registration.vehicle1_24ghz"   value: "{var: savedoption.vehicle1_24ghz}"

so to translate above if anyone is confused, it's "modelname.field" and "{var: modelname.field}

When it finds the registration, it ignores the variable set since it is in the "not found" area.

When it doesn't find an existing registration, it sets the values on the form to the saved options.

So far with my testing it seems to work correctly.  If I create a new registration, it pulls my saved options, if I edit an existing, it properly pulls what I registered with.

UPDATE:

I found an issue where upon re-entry, the fields were not displaying/resetting properly.  No matter if the form was submitted or navigated from.

Solution: Create a second "Variables" action in the "found" section of "registration"

In that, literally use Type: "Request" name: "registration.vehicle1_24ghz"  value: "{var: registration.vehicle1_24ghz}" 

I know it sounds weird as it actually is setting the field to itself, but it's truly it's setting the form field to the database value.

Works perfect.

You need to login to be able to post a reply.