This topic is locked
[SOLVED]

 Conditionally Required Fields

9/3/2012 7:56:00 PM
PHPRunner General questions
S
Stucco author

Hi,
I have a field that needs to be required dependent on the value of another field. How can I accomplish this?
I would like it to show the dependency in JavaScript, without saving the form, but it would also be ok to show a message if they attempt to save it under the required conditions.
Thanks!!

W
wildwally 9/3/2012



Hi,
I have a field that needs to be required dependent on the value of another field. How can I accomplish this?
I would like it to show the dependency in Javascript, without saving the form, but it would also be ok to show a message if they attempt to save it under the required conditions.
Thanks!!


Something like this in javascript onLoad event:



var ctrlfield= Runner.getControl(pageid, 'field');

var ctrlanotherfiled= Runner.getControl(pageid, 'anotherfiled');
//-----ON CHANGE CODE

ctrlanotherfiled.on('change', function(e){

if (this.getValue() != 'condition')

{

ctrlfield.addValidation("IsRequired");

}else{

ctrlfield.removeValidation("IsRequired");

}

});
S
Stucco author 9/24/2012



Something like this in javascript onLoad event:



var ctrlfield= Runner.getControl(pageid, 'field');

var ctrlanotherfiled= Runner.getControl(pageid, 'anotherfiled');
//-----ON CHANGE CODE

ctrlanotherfiled.on('change', function(e){

if (this.getValue() != 'condition')

{

ctrlfield.addValidation("IsRequired");

}else{

ctrlfield.removeValidation("IsRequired");

}

});



This works great! I have expanded the code to function with one controlling field and four controlled fields. Two of the controlled fields are date fields and the other two are time fields. The date fields function as expected, but the time fields do not become required as expected. Do you have any insights?



var status_field = Runner.getControl(pageid, 'status_id');

var sch_dep_date_field = Runner.getControl(pageid, 'scheduled_departure_date');

var sch_dep_time_field = Runner.getControl(pageid, 'scheduled_departure_time');

var seal_brk_date_field = Runner.getControl(pageid, 'seal_break_date');

var seal_brk_time_field = Runner.getControl(pageid, 'seal_break_time');
//-----ON CHANGE CODE

status_field.on('change', function(e) {

if (this.getValue() == '4' || this.getValue() == '7') {

sch_dep_date_field.addValidation("IsRequired");

sch_dep_time_field.addValidation("IsRequired");

seal_brk_date_field.addValidation("IsRequired");

seal_brk_time_field.addValidation("IsRequired");

} else {

sch_dep_date_field.removeValidation("IsRequired");

sch_dep_time_field.removeValidation("IsRequired");

seal_brk_date_field.removeValidation("IsRequired");

seal_brk_time_field.removeValidation("IsRequired");

}

});


Thanks!!

S
Stucco author 9/30/2012

Hi,
Does anyone know how to reference time fields with Runner.getControl?

S
Stucco author 9/30/2012

And I think I figured it out.
It looks like there is an overridden addValidation function on time elements that only accepts 'isRequired' instead of 'IsRequired'. Since there is no 'isRequired' function, this fails if I attempt to add the lower case version instead. If I do a find/replace all on isRequired -> IsRequired on RunnerAll.js then it functions as expected.
I would like to avoid editing RunnerAll.js in the program files directory in order to stay in sync with the releases and be able to get updates to that file later on. Is there another suggested option besides manually editing the RunnerAll.js after every build?

S
Stucco author 9/30/2012

Yep. That's it. I was able to bypass the overloaded addValidation method and just manually add 'IsRequired' to the array. This is my final code.



var status_field = Runner.getControl(pageid, 'status_id');

var sch_dep_date_field = Runner.getControl(pageid, 'scheduled_departure_date');

var sch_dep_time_field = Runner.getControl(pageid, 'scheduled_departure_time');

var seal_brk_date_field = Runner.getControl(pageid, 'seal_break_date');

var seal_brk_time_field = Runner.getControl(pageid, 'seal_break_time');
//-----ON CHANGE CODE

status_field.on('change', function(e) {

if (this.getValue() == '4' || this.getValue() == '7') {

sch_dep_date_field.addValidation("IsRequired");

//Must bypass addValidation function due to isRequired case bug

sch_dep_time_field.validationArr.push("IsRequired");

seal_brk_date_field.addValidation("IsRequired");

//Must bypass addValidation function due to isRequired case bug

seal_brk_time_field.validationArr.push("IsRequired");

} else {

sch_dep_date_field.removeValidation("IsRequired");

sch_dep_time_field.removeValidation("IsRequired");

seal_brk_date_field.removeValidation("IsRequired");

seal_brk_time_field.removeValidation("IsRequired");

}

});


removeValidation functions as expected.
Hope this helps someone else.