Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Conditionally Required Field/validation

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Conditionally Required Field/validation

Unread post 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
kev1n
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

Unread post 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;
}
Last edited by kev1n on Thu Jul 30, 2020 2:06 pm, edited 1 time in total.
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Conditionally Required Field/validation

Unread post 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.
kev1n
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

Unread post by kev1n »

Question: Are you using an SQL query or a separated list in your select objects?
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Conditionally Required Field/validation

Unread post by ricklincs »

Hi Kev1n

I am using a separated list. Thanks
kev1n
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

Unread post by kev1n »

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
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Conditionally Required Field/validation

Unread post by ricklincs »

Hi Kev1n

My list looks like this

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

Thanks
kev1n
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

Unread post 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);
}
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Conditionally Required Field/validation

Unread post by ricklincs »

Thanks Kev1n

Will give it a go and see.
Post Reply