Radio Check

Big(ish) victory over the radio option field type this week.  Still working on implementing legitimate validation checks for our custom request forms feature.  In the current process, a user’s submission is checked via a complicated process- php code ‘prints’ out javascript functions, so that it can use a variable snagged from the database to see if that field is required.  Seems to work, but it also seems to have 2 major drawbacks:

1) Only works with alert boxes- if we get much more complicated with it, it all breaks down
2) It doesn’t really check for valid forms.

#2 means that it checks a very basic form, but admins can create very complex forms.  They might have 4 non-required type=text fields, 2 required type=text fields, a required radio fieldset, a non-required checkbox set, and so on.  The current validation really only checks one level- if there’s a required field of a certain type, and there’s an entry in a required field of a certain type, it returns true.

But what if there are two required fields of the same type?  That’s where it gets tricky.  Seems to be ok on type=text, but with others (radio, checkbox, select box), it will return true if only one (of possibly many) fields is actually filled out.

So it’s been a bit of an ordeal to figure out.  First, we check if the field exists and contains a required property.  Then, we can use jquery to get a jq object of that field, and do whatever we need to do (in this case, turn the outline red and show the error div for that field).  Instead of printing javascript functions in order to have access to php to get variables from the database, we inserted a simple, tiny bit of php into each html element on the form:
<?php print ${$required_prop} ?>
That will print ‘required’ if the database tells us the admin has set that field as required, or nothing if not.  Then, we inserted the same php into the class definition of the parent element (for checkboxes and radio groups, a wrapper div)- that way we can grab it and outline the whole group if an error is returned.

Seems to be working well, but still a lot of testing to go.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s