Page 1 of 1
Conditionally Required Field/validation
Posted: Wed Jul 29, 2020 2:47 pm
by ricklincs
I have a form [Inwards]with:
a select field [Type] with the following values; Loaded, Unloaded, Truck only.
a text field [Trl Number]
if the select value [Type] is Loaded or Unloaded then the text field [Trl Number] can not be blank.
if the select value [Type] is Truck only then the text field [Trl Number] must be blank.
Should I be looking at server side validation and the before save and php code to do this?, or JavaScript validation?
Any examples would be great. Thanks
Re: Conditionally Required Field/validation
Posted: Wed Jul 29, 2020 3:56 pm
by kev1n
Hi,
Server-side validation is better for overall security.
Here is some code to validate on the client side. If you want, I could post some code for server-side validation later on.
☛ Add this JavaScript code to your form's Custom Code field.
☛ Replace
Type and
trl_number with your Object IDs.
Code: Select all
function validateForm() {
var result = '';
var type = $( "#Type option:selected" ).text();
var tryNumber = $( "#trl_number" ).val();
if ((type == 'Loaded' || type == 'Unloaded') && tryNumber == '') {
result = '[Trl Number] cannot be blank.';
} else
if ((type == 'Truck Only') && tryNumber !== '') {
result = '[Trl Number] must be blank.';
}
return result;
}
// Form validation is taking place when the user saves the form
function nuBeforeSave() {
if (nuFORM.edited === true) {
var err = validateForm();
// If there are any errors
if (err !== '') {
// Display (all) error messages
nuMessage([err]);
// and abort saving
return false;
}
// In case of no errors, continue saving
return true;
}
nuMessage(["Form not saved because no data was changed"]);
return false;
}
Re: Conditionally Required Field/validation
Posted: Wed Jul 29, 2020 4:11 pm
by ricklincs
Thanks Kev1n.
That certainly was a quick reply and the example supplied is excellent. If you could post some server-side validation later that would be great.
Re: Conditionally Required Field/validation
Posted: Wed Jul 29, 2020 6:15 pm
by kev1n
Question: Are you using an SQL query or a separated list in your select objects?
Re: Conditionally Required Field/validation
Posted: Thu Jul 30, 2020 12:42 pm
by ricklincs
Hi Kev1n
I am using a separated list. Thanks
Re: Conditionally Required Field/validation
Posted: Thu Jul 30, 2020 1:22 pm
by kev1n
Does your list look like this
Code: Select all
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only
Or with numberic (bound) values?
Re: Conditionally Required Field/validation
Posted: Thu Jul 30, 2020 1:58 pm
by ricklincs
Hi Kev1n
My list looks like this
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only
Thanks
Re: Conditionally Required Field/validation
Posted: Thu Jul 30, 2020 2:05 pm
by kev1n
A validation with PHP would look like this.
☛ Add the code in the
PHP BS (Before Save) event.
☛ In the line
Code: Select all
errors = validateForm("#type#","trl_number#");
replace
type and
trl_number with your Object IDs.
Code: Select all
function validateForm($type, $trl_number) {
$result = '';
if (($type == 'Loaded' || $type == 'Unloaded') && $trl_number == '') {
$result = '[Trl Number] cannot be blank.';
} else
if ($type == 'Truck only' && $trl_number !== '') {
$result = '[Trl Number] must be blank.';
}
return $result;
}
$errors = validateForm("#type#","trl_number#");
if ($errors !== '') {
nuDisplayError($errors);
}
Re: Conditionally Required Field/validation
Posted: Fri Jul 31, 2020 2:45 pm
by ricklincs
Thanks Kev1n
Will give it a go and see.