Is there an easy way to check if the form has all manditory fields filled out in a client script?

DrewW
Mega Sage
Mega Sage

Is there an easy way to check if the form has all manditory fields filled out in a client script? I have 9 fields that only have to be filled out at closing of an incident so I make them manditory once someone hits the close button. So now I am looking for a quick way to find out if the form is still valid. I would rather not go and get all 9 fields and check them but if that is what I have to do then that is what I have to do.

Any help would be appreciated...

8 REPLIES 8

Mark Stanger
Giga Sage

I'm not sure exactly what your purpose is with this but here's the client script function that will do it.



g_form.mandatoryCheck();


May be there is a better way to do this, but my reasoning is that I have 9 fields that are only manditory at ticket close. So I have an onSubmit client script that sets these to manditory when the close button is pressed. If the user has not entered any data into the fields I need to prevent the form from being submited to the server. So I figured that what I would do is check to see if all of the fields had some kind of data if one of them did not then cancel the form submition. I was hoping there was a function to do the checking for me and you showed me there was, much appreciated... 🙂

If there is a better way to set fields to manditory at closure only I am not seeing it. Any suggestions are welcome.

Here is the script I have so far

function onSubmit() {
var form = gel('incident.do');
var sClsBtn = form.sys_action;
var inc_state = g_form.getValue('incident_state');

if (sClsBtn.value == 'close_incident' && (inc_state == '6' || inc_state == '7')) {
setMandatoryClosureFields(true);
if(!g_form.mandatoryCheck()){
alert('Please fill out all mandatory fields for closure.');
return false;
}
} else {
setMandatoryClosureFields(false);
}
}

function setMandatoryClosureFields(setting){
var ag = g_form.getReference("assignment_group");
g_form.setMandatory('close_notes', setting);
if(ag == 'Deskside Support' || setting == false){
g_form.setMandatory('u_bk_fx_service_code', setting);
g_form.setMandatory('u_bk_fx_equipment_code', setting);
g_form.setMandatory('u_bk_fx_activity_code', setting);
g_form.setMandatory('u_bk_fx_work_time', setting);
g_form.setMandatory('u_bk_fx_travel_time', setting);
g_form.setMandatory('u_cause_code', setting);
}
g_form.setMandatory('u_resolution_code', setting);
g_form.setMandatory('u_cause_code', setting);
}


That sounds reasonable. We actually check for mandatory fields on every form submission (as I'm sure you know). I guess the thing that surprises me about all of this is that the automatic mandatory check isn't happening after your onSubmit client script sets those fields to mandatory. It must be that the built-in mandatory check happens before onSubmit client scripts are processed. In that case, what you've got should work great.


You are correct in the fact that the check you do happens before the one I wrote. It would be greate if it did it after.

My only issue with the below is that it does not work quite right. My issue is that if they hit the wrong button they have to reload the form. I would like them to be able to hit the save button. So basicly they make a mistake and hit "Close Incident", it marks the fields as required, but that is not what they wanted to do. But now they can not hit the save button because there are manditory fields that they do not know or should not fill out yet. So they can not save because it will fail validation. Because the validation check is done before my onSubmit I do not have the oportunity to turn off the manditory fields for the save.