Conditionally Required Field/validation

Questions related to customising nuBuilder Forte with Javascript or PHP.

Conditionally Required Field/validation

Postby ricklincs » Wed Jul 29, 2020 10:17 pm

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
ricklincs
 
Posts: 21
Joined: Tue Aug 02, 2011 1:09 am

Re: Conditionally Required Field/validation

Postby kev1n » Wed Jul 29, 2020 11:26 pm

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;
}
Last edited by kev1n on Thu Jul 30, 2020 9:36 pm, edited 1 time in total.
kev1n
 
Posts: 838
Joined: Mon Oct 15, 2018 2:13 am

Re: Conditionally Required Field/validation

Postby ricklincs » Wed Jul 29, 2020 11:41 pm

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.
ricklincs
 
Posts: 21
Joined: Tue Aug 02, 2011 1:09 am

Re: Conditionally Required Field/validation

Postby kev1n » Thu Jul 30, 2020 1:45 am

Question: Are you using an SQL query or a separated list in your select objects?
kev1n
 
Posts: 838
Joined: Mon Oct 15, 2018 2:13 am

Re: Conditionally Required Field/validation

Postby ricklincs » Thu Jul 30, 2020 8:12 pm

Hi Kev1n

I am using a separated list. Thanks
ricklincs
 
Posts: 21
Joined: Tue Aug 02, 2011 1:09 am

Re: Conditionally Required Field/validation

Postby kev1n » Thu Jul 30, 2020 8:52 pm

Does your list look like this
Code: Select all
Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only


Or with numberic (bound) values?
Code: Select all
0|Loaded|1|Unloaded|2|Truck only
kev1n
 
Posts: 838
Joined: Mon Oct 15, 2018 2:13 am

Re: Conditionally Required Field/validation

Postby ricklincs » Thu Jul 30, 2020 9:28 pm

Hi Kev1n

My list looks like this

Loaded|Loaded|Unloaded|Unloaded|Truck only|Truck only

Thanks
ricklincs
 
Posts: 21
Joined: Tue Aug 02, 2011 1:09 am

Re: Conditionally Required Field/validation

Postby kev1n » Thu Jul 30, 2020 9:35 pm

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);
}
kev1n
 
Posts: 838
Joined: Mon Oct 15, 2018 2:13 am

Re: Conditionally Required Field/validation

Postby ricklincs » Fri Jul 31, 2020 10:15 pm

Thanks Kev1n

Will give it a go and see.
ricklincs
 
Posts: 21
Joined: Tue Aug 02, 2011 1:09 am


Return to Custom Code



cron