This topic is locked

Conditional Formating

9/19/2017 6:19:51 PM
ASPRunner.NET General questions
I
i.NoLim author

Edit: I titled this topic as "Conditional Formatting" when I meant "Conditional Validation."
First of all, thank you for assisting me with my previous issue. I am now attempting to add conditional validation to the same form.

Scenario:

  • I have a dropdown,'choice', with two options: 'X' and 'Y.'
  • The form also contains 6 fields: 'firstName,' 'lastName,' 'phoneNum,''car,' 'tv,''pen,' 'cat,' and 'dog.'
  • 'firstName,' 'lastName,' and 'phoneNum' are always shown.
  • 'car,' 'tv,'and 'pen,' are only shown and should be required when 'X' is selected.
  • 'cat,' and 'dog.' are only shown and should be required when 'Y' is selected.
    I already know how to hide the fields depending on the selection. I also used the first code provided by Jadach in my previous thread to get started. If I stay in 'X' or 'Y' and submit it goes through. But if I switch from 'X' to 'Y' or vice versa I'm not able to submit my form.
    My code:



var ctrlTYPE = Runner.getControl(pageid, 'choice');

var ctrlTYPE = Runner.getControl(pageid, 'firstName');

var ctrlTYPE = Runner.getControl(pageid, 'lastName');

var ctrlTYPE = Runner.getControl(pageid, 'phoneNum');

var ctrlTYPE = Runner.getControl(pageid, 'car');

var ctrlTYPE = Runner.getControl(pageid, 'tv');

var ctrlTYPE = Runner.getControl(pageid, 'pen');

var ctrlTYPE = Runner.getControl(pageid, 'cat');

var ctrlTYPE = Runner.getControl(pageid, 'dog');
ctrlchoice.addValidation("IsRequired");

ctrlfirstName.addValidation("IsRequired");

ctrllastName.addValidation("IsRequired");

ctrlphoneNum.addValidation("IsRequired");
ctrlchoice.on('change', function(e)

{

if (this.getValue() == 'X')

{

ctrlcar.addValidation("IsRequired");

ctrltv.addValidation("IsRequired");

ctrlpen.addValidation("IsRequired");

ctrlcat.removeValidation("IsRequired");

ctrldog.removeValidation("IsRequired");

}

else

{

ctrlcat.addValidation("IsRequired");

ctrldog.addValidation("IsRequired");

ctrlcar.removeValidation("IsRequired");

ctrltv.removeValidation("IsRequired");

ctrlpen.removeValidation("IsRequired");

}

});


I've tried using the following links as guides to no success:

http://www.asprunner.com/forums/topic/15311-conditional-validation/

http://www.asprunner.com/forums/topic/6924-conditional-required-fields/
I've done it in "Before record added" but I wanted to try it with JavaScript. Any help will be greatly appreciated.

I
i.NoLim author 9/19/2017

After playing with it the issue only happens if I try to save the form when I'm in one of the choices, I'm missing required fields, switch to a different choice, fill out all the required fields and try to submit again.
Example: I choose 'X' and fill out al required fields except for 'car.' I click "Save" and I get the error that I'm missing a required field. Instead of filling out 'car' I switch to choice 'Y.' I fill out all required fields but I'm still unable to save unless I go back to 'X' and fill out 'car.' Shouldn't the .removeValidation() work in this scenario as well?
I'm going to try to solve it by doing something like ctrlcar.setValue('');

jadachDevClub member 9/20/2017

Maybe this video will help you.

https://www.youtube.com/watch?v=xTKKgCFNkJM

I
i.NoLim author 9/20/2017



Maybe this video will help you.

https://www.youtube.com/watch?v=xTKKgCFNkJM


Thank you for your reply jadach, but unfortunately it still not working. Here's my code.



var ctrlTYPE = Runner.getControl(pageid, 'TYPE');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');

var ctrlBUSINESS_TYPE = Runner.getControl(pageid, 'BUSINESS_TYPE');

var ctrlBUSINESS_PHONE_NUM = Runner.getControl(pageid, 'BUSINESS_PHONE_NUM');

var ctrlOPEN = Runner.getControl(pageid,'OPEN');

var ctrlCLOSE = Runner.getControl(pageid,'CLOSE');

var ctrlDWELLING_TYPE = Runner.getControl(pageid, 'DWELLING_TYPE');
pageObj.hideField("BUSINESS_NAME")

pageObj.hideField("EMERGENCY_LAST_NAME")
//Validations

ctrlTYPE.addValidation("IsRequired");

ctrlCONTACT_LAST_NAME.addValidation("IsRequired");

ctrlCONTACT_FIRST_NAME.addValidation("IsRequired");
ctrlTYPE.on('change', function(e)

{

if (this.getValue() == 'Business')

{

pageObj.showField("BUSINESS_NAME");

pageObj.showField("EMERGENCY_LAST_NAME");

pageObj.hideField("MGMT_COMPANY");
ctrlBUSINESS_TYPE.addValidation("IsRequired");

ctrlBUSINESS_PHONE_NUM.addValidation("IsRequired");

ctrlOPEN.addValidation("IsRequired");

ctrlCLOSE.addValidation("IsRequired");
ctrlDWELLING_TYPE.removeValidation("IsRequired");

ctrlDWELLING_TYPE.setValue('');

}
else

{

pageObj.hideField("BUSINESS_NAME");

pageObj.hideField("EMERGENCY_LAST_NAME");

pageObj.showField("MGMT_COMPANY");
ctrlDWELLING_TYPE.addValidation("IsRequired");
ctrlBUSINESS_TYPE.removeValidation("IsRequired");

ctrlBUSINESS_PHONE_NUM.removeValidation("IsRequired");

ctrlOPEN.removeValidation("IsRequired");

ctrlCLOSE.removeValidation("IsRequired");

ctrlBUSINESS_TYPE.setValue('');

ctrlBUSINESS_PHONE_NUM.setValue('');

ctrlOPEN.setValue('');

ctrlCLOSE.setValue('');

}

});


DWELLING_TYPE and BUSINESS_TYPE are dropdowns.

OPEN and CLOSE are Time data types.
As I mentioned earlier it works if I don't hit the save button, if I hit the save button, switch TYPE and then try to save the form it wont let me.

I
i.NoLim author 9/20/2017

I managed to resolve my issued by working in "Before display" instead of "JavaScript OnLoad event." One error still persist though, if I'm in TYPE "Business" and submit the form with out completing all required fields I get a pop up saying "Please fill out all required fields in "Business." Which is exactly what I want. Unfortunately, when the pop up is closed the fields that pertain to Business are hidden even though "Business" is still selected in "TYPE."
"Before display" code:



if(values["TYPE"])

{

if(values["TYPE"] == "Business")

{

if((values["BUSINESS_TYPE"]) && (values["BUSINESS_PHONE_NUM"]) && (values["OPEN"]) && (values["CLOSE"]) && (values["CONTACT_LAST_NAME"])

&& (values["CONTACT_FIRST_NAME"]))

{

values["DWELLING_TYPE"] = "";
return true;

}
else

{

XSession.Session["message"] = "";

MVCFunctions.EchoToOutput("<script>alert('Please fill out all required fields in Business.');</script>");

return false;

}

}
else

{

if((values["DWELLING_TYPE"]) && (values["CONTACT_LAST_NAME"]) && (values["CONTACT_FIRST_NAME"]))

values["BUSINESS_NAME"] = "";

{

values["BUSINESS_TYPE"] = "";

values["BUSINESS_PHONE_NUM"] = "";

values["24_HOURS"] = "";

values["OPEN"] = "";

values["CLOSE"] = "";
return true;

}

else

{

XSession.Session["message"] = "";

MVCFunctions.EchoToOutput("<script>alert('Please fill out all required fields');</script>");

return false;

}

}

}
else

{

XSession.Session["message"] = "";

MVCFunctions.EchoToOutput("<script>alert('Please select a Type');</script>");

return false;

}
return true;


"JavaScript OnLoad event" code



var ctrlTYPE = Runner.getControl(pageid, 'TYPE');

var ctrlCONTACT_LAST_NAME = Runner.getControl(pageid, 'CONTACT_LAST_NAME');

var ctrlCONTACT_FIRST_NAME = Runner.getControl(pageid, 'CONTACT_FIRST_NAME');

var ctrlBUSINESS_TYPE = Runner.getControl(pageid, 'BUSINESS_TYPE');

var ctrlBUSINESS_PHONE_NUM = Runner.getControl(pageid, 'BUSINESS_PHONE_NUM');

var ctrlOPEN = Runner.getControl(pageid,'OPEN');

var ctrlCLOSE = Runner.getControl(pageid,'CLOSE');

var ctrlDWELLING_TYPE = Runner.getControl(pageid, 'DWELLING_TYPE');
pageObj.hideField("BUSINESS_NAME")

pageObj.hideField("EMERGENCY_LAST_NAME")
ctrlTYPE.on('change', function(e)

{

if (this.getValue() == 'Business')

{

pageObj.showField("BUSINESS_NAME");

pageObj.showField("EMERGENCY_LAST_NAME");

pageObj.hideField("MGMT_COMPANY");

}

else

{

pageObj.hideField("BUSINESS_NAME");

pageObj.hideField("EMERGENCY_LAST_NAME");

pageObj.showField("MGMT_COMPANY");

}

});