Welcome to the nuBuilder Forums!

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

Conditional Mandatory subform field

Questions related to using nuBuilder Forte.
Post Reply
nc07
Posts: 118
Joined: Tue Jun 04, 2019 4:05 am
Has thanked: 5 times
Been thanked: 22 times

Conditional Mandatory subform field

Unread post by nc07 »

Hi,

I have a form that has two subforms. I want to make one of the fields conditional mandatory based on the selection in one of the fields in the main form. Before save i want to check if the value selected in the main form field is '3' for example then one of the fields in the subform must be filled otherwise it can be null.

the main form field id is po_categ, subform id is field04 and subform field id is co_actn

how can this be done either before save in PHP or javascript?
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Conditional Mandatory subform field

Unread post by admin »

nc07,

You can use either JS or PHP to validate your Subforms with ...

https://wiki.nubuilder.cloud/ ... BeforeSave
and
https://wiki.nubuilder.cloud/ ... formObject in Javascript (in the browser)

or

https://wiki.nubuilder.cloud/ ... splayError
and
https://wiki.nubuilder.cloud/ ... formObject in PHP - in Before Save (on the server)


Steven
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Conditional Mandatory subform field

Unread post by kev1n »

Here's how it can be done with Javascript. (The preferred way would be a server side check with PHP)

Code: Select all


function sumFormColArray(id, fieldname) {

    var a = Array();
    var sf = nuSubformObject(id);
    var c = sf.fields.indexOf(fieldname);
    for(var i = 0; i < sf.rows.length; i++) {
        if(sf.deleted[i] == 0) {
            var rv = sf.rows[i][c];
            a.push(rv);
        }
    }
    return a;
}

function subFormHasMissingValuesInColumn(id, fieldname) {
	var arr = sumFormColArray(id, fieldname)
	return arr.findIndex(e => e === "") > -1
}

function nuBeforeSave() {
    if (nuFORM.edited === true) {

		if ($('#po_categ').val() !== "") {
			if (subFormHasMissingValuesInColumn('field04','co_actn')) {
				nuMessage(["There are missing values in column xyz."]);
				return false;
			}
		}   
    }
    return true;
}
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Conditional Mandatory subform field

Unread post by kev1n »

The PHP equivalent - to be added in the BS (Before Save) event:

Code: Select all

$id = 'field04';        // subform object id
$fieldname = 'co_actn';   // subform field name
    
    
if ("#po_categ#" !== "") {
    if (blankValuesInSubformField($id, $fieldname) > 0) {
        nuDisplayError('Field xyz '.nuTranslate('cannot be left blank'));       
    }
}

function blankValuesInSubformField($id, $fieldname) {

    $sf = nuSubformObject($id);
    
    $c = array_search($fieldname, $sf->fields);

    for($i = 0 ; $i < count($sf->rows) ; $i++){
             
          $value = $sf->rows[$i][$c];
          $notDeleted = $sf->deleted[$i] == 0;
   
          if ($notDeleted && $value == "") {
                $r++;
          }
    }
   
    return $r;

}
nc07
Posts: 118
Joined: Tue Jun 04, 2019 4:05 am
Has thanked: 5 times
Been thanked: 22 times

Re: Conditional Mandatory subform field

Unread post by nc07 »

Thank You, kev1n;

I have tried both the codes, JS & PHP, and both work fine.

Thank You so much for your time and help, really appreciated.

Cheers.
nc07
kev1n
nuBuilder Team
Posts: 4307
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 448 times
Contact:

Re: Conditional Mandatory subform field

Unread post by kev1n »

You're welcome!
Post Reply