This topic is locked
[SOLVED]

 addValidation method in Field events

3/16/2018 2:07:27 PM
PHPRunner General questions
I
infotech author

Hi all - wow, great product.
I am attempting to addValidation 'required' to a date control when a radio button field is selected "No".

Field events is where I an trying to apply validation.
my code:

var ctrliDate = pageObj.getControl('Inactive');

params["value"] = this.getValue();
if (params["value"] == "No"){
confirm("Please enter the Inactive date in the Services Section.");
ctrliDate.addValidation("IsRequired");

} else {

ctrliDate.setValue('');

ctrliDate.removeValidation("IsRequired");

};
return false;


However, It does not seem to work.

The user does get the confirm prompt, however the code targeted date field does not display red , and the record can be saved without an Inactive date set.
I have tried it with and without the
else* removeValidation code, no joy. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=25427&image=1&table=forumtopics' class='bbcemoticon' alt='<<' />
Should I be taking a different tack??
any input is most certainly appreciated.
{wonk}

woodey2002 3/16/2018

I used validation on an event before.
Try this code(add your then Name Of Your Date Control first)
I have not tested with the alert line, so if that does not work remove it and test again!



var ctrRadio= Runner.getControl(pageid, 'Inactive');

var ctrDate = Runner.getControl(pageid, 'add_The_Name_Of_Your_Date_Control_Here');
ctrRadio.on('change', function(e) {
if (this.getValue()!= "No"){

alert("Please enter the Inactive date in the Services Section.");

ctrDate.addValidation("IsRequired");

}
else {
ctrDate.removeValidation("IsRequired");

ctrDate.setValue('');
}

});


Below is a successful example where I have added a javascript onload event to show/hide a field called "qualifications_Other" if "Other" is selected from the "qualifications" lookup wizard checkbox list control.
Nottice the .addValidation and .removeValidation parameters, Maybe you can adjust the code below to fit your requirements.
Here is the javascript onload code:



var ctrlIvcX841 = Runner.getControl(pageid, 'qualifications');

var ctrlIvcX841T = Runner.getControl(pageid, 'qualifications_Other');
pageObj.hideField("qualifications_Other");
ctrlIvcX841.on('change', function(e) {
if (this.getValue()!= "Other"){
pageObj.showField("qualifications_Other");

ctrlIvcX841T.addValidation("IsRequired");

}
else {
pageObj.hideField("qualifications_Other");

ctrlIvcX841T.removeValidation("IsRequired");

ctrlIvcX841T.setValue('');
}

});


Hope this helps,

James

I
infotech author 3/19/2018

James,
Thank you for your advice! This worked for my scenario.
Creating the function 'e' in Javascript onLoad event ('Edit Page') worked perfect.



var ctrRadio= Runner.getControl(pageid, 'Active');

var ctrDate = Runner.getControl(pageid, 'InactiveDate');
ctrRadio.on('change', function(e) {

if (this.getValue()== "No"){

alert("Please enter the Inactive date in the Services Section.");

ctrDate.addValidation("IsRequired");

}

else {

ctrDate.removeValidation("IsRequired");

ctrDate.setValue('');

}

})


The alert message does work in Chrome 65.0.3325.146 and IE 11.125.16299.0

[indent]While this code does not add the prerequisite Red * on the field (I assume because we do not re-draw the page), it does successfully make the field required.
[/indent]