Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Conditionally Required Field/validation
Conditionally Required Field/validation
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
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
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Conditionally Required Field/validation
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.
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;
}
Last edited by kev1n on Thu Jul 30, 2020 2:06 pm, edited 1 time in total.
Re: Conditionally Required Field/validation
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.
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.
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Conditionally Required Field/validation
Question: Are you using an SQL query or a separated list in your select objects?
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Conditionally Required Field/validation
Does your list look like this
Or with numberic (bound) values?
Code: Select all
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only
Code: Select all
0|Loaded|1|Unloaded|2|Truck only
Re: Conditionally Required Field/validation
Hi Kev1n
My list looks like this
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only
Thanks
My list looks like this
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only
Thanks
-
- nuBuilder Team
- Posts: 4297
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 445 times
- Contact:
Re: Conditionally Required Field/validation
A validation with PHP would look like this.
☛ Add the code in the PHP BS (Before Save) event.
☛ In the line
replace type and trl_number with your Object IDs.
☛ Add the code in the PHP BS (Before Save) event.
☛ In the line
Code: Select all
errors = validateForm("#type#","trl_number#");
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);
}