== Sample Rules Definition File == The following is an example of an XML rules definition file that demonstrates all of the elements and attributes that are available in the [[Rules Definition File | xml schema]]. It also illustrates a number of validation types. A version of the file in JSON format which describes the exact same rules follows the XML file. The complete files are listed, followed by excerpts from the files with a more detailed description of each section. === The Complete XML File ===
=== JSON Format File === {"validateThis" : { "conditions" : [ {"name":"MustLikeSomething", "serverTest":"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0", "clientTest":"$("[name='LikeCheese']").getValue() == 0 && $("[name='LikeChocolate']").getValue() == 0;" } ], "contexts" : [ {"name":"Register","formName":"frmRegister"}, {"name":"Profile","formName":"frmProfile"} ], "objectProperties" : [ {"name":"UserName","desc":"Email Address", "rules": [ {"type":"required","contexts":"*"}, {"type":"email","failureMessage":"Hey, buddy, you call that an Email Address?"} ] }, {"name":"Nickname", "rules" : [ {"type":"custom","failureMessage":"That Nickname is already taken. Please try another", "params":[ {"name":"methodName","value":"CheckDupNickname"}, {"name":"remoteURL","value":"CheckDupNickname.cfm"} ] } ] }, {"name":"UserPass","desc":"Password", "rules" : [ {"type":"required"}, {"type":"rangelength", "params" : [ {"name":"minlength","value":"5"}, {"name":"maxlength","value":"10"} ] } ] }, {"name":"VerifyPassword", "rules" : [ {"type":"required"}, {"type":"equalTo", "params" : [ {"name":"ComparePropertyName","value":"UserPass"} ] } ] }, {"name":"UserGroup","clientFieldName":"UserGroupID", "rules" : [ {"type":"required"} ] }, {"name":"Salutation", "rules" : [ {"type":"required","contexts":"Profile"}, {"type":"regex","failureMessage":"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.", "params" : [ {"name":"Regex","value":"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\\.)?$"} ] } ] }, {"name":"FirstName", "rules" : [ {"type":"required"} ] }, {"name":"LastName", "rules" : [ {"type":"required","contexts":"Profile"}, {"type":"required","contexts":"Register", "params" : [ {"name":"DependentPropertyName","value":"FirstName"} ] } ] }, {"name":"LikeOther","desc":"What do you like?", "rules" : [ {"type":"required","condition":"MustLikeSomething","failureMessage":"If you don't like cheese and you don't like chocolate, you must like something!"} ] }, {"name":"HowMuch","desc":"How much money would you like?", "rules" : [ {"type":"numeric"} ] }, {"name":"AllowCommunication","desc":"May we send you information?" }, {"name":"CommunicationMethod", "rules" : [ {"type":"required","failureMessage":"if you are allowing communication, you must choose a method", "params" : [ {"name":"DependentPropertyName","value":"AllowCommunication"}, {"name":"DependentPropertyValue","value":"1"} ] } ] } ] } } === The conditions Element === "conditions" : [ {"name":"MustLikeSomething", "serverTest":"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0", "clientTest":"$("[name='LikeCheese']").getValue() == 0 && $("[name='LikeChocolate']").getValue() == 0;" } ] Here we are defining one condition, and giving it a unique name of ''MustLikeSomething''. We then specify how the condition is to be evaluated on both the server and the client.
In the case of the server we are saying:
"This condition is met when the value of ''getLikeCheese()'' is zero and the value of ''getLikeChocolate()'' is zero."
While on the client we are saying:
"This condition is met when the value of the html element with a name attribute of ''LikeCheese'' is zero and the value of the html element with a name attribute of ''LikeChocolate'' is zero."
Note that because we are placing actual JavaScript code into an xml attribute / json string we must escape the special characters. This condition, ''MustLikeSomething'' will be used in a rule definition later in the file.
=== The contexts Element === "contexts" : [ {"name":"Register","formName":"frmRegister"}, {"name":"Profile","formName":"frmProfile"} ] Here we are defining two contexts and pointing them at particular forms. We are doing this because each form has a unique name, and specifying these here, in the configuration file, means that we won't have to specify the form name when asking the framework to generate client-side validations for us.
If the forms had the same name, e.g., frmUser, there would be no reason to include this ''contexts'' element at all. It '''is not''' required to make use of contexts for validation rules, its sole purpose is to point a context at a particular form.
=== The objectProperties Element === We define all of the properties that the framework needs to know about in the ''objectProperties'' element. There are two reasons that a property might need to be defined to the framework:
  1. It requires that validation rules be defined for it.
  2. It needs a description recorded for it. E.g., if it relates to another property with a rule (e.g., via an equalTo rule type).
Each property's rules is followed by a textual description of what the element implies.
{"name":"UserName","desc":"Email Address", "rules": [ {"type":"required","contexts":"*"}, {"type":"email","failureMessage":"Hey, buddy, you call that an Email Address?"} ] }
{"name":"Nickname", "rules" : [ {"type":"custom","failureMessage":"That Nickname is already taken. Please try another", "params":[ {"name":"methodName","value":"CheckDupNickname"}, {"name":"remoteURL","value":"CheckDupNickname.cfm"} ] } ] }
{"name":"UserPass","desc":"Password", "rules" : [ {"type":"required"}, {"type":"rangelength", "params" : [ {"name":"minlength","value":"5"}, {"name":"maxlength","value":"10"} ] } ] }
{"name":"VerifyPassword", "rules" : [ {"type":"required"}, {"type":"equalTo", "params" : [ {"name":"ComparePropertyName","value":"UserPass"} ] } ] }
{"name":"UserGroup","clientFieldName":"UserGroupID", "rules" : [ {"type":"required"} ] }
{"name":"Salutation", "rules" : [ {"type":"required","contexts":"Profile"}, {"type":"regex","failureMessage":"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.", "params" : [ {"name":"Regex","value":"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\\.)?$"} ] } ] }
{"name":"FirstName", "rules" : [ {"type":"required"} ] }
{"name":"LastName", "rules" : [ {"type":"required","contexts":"Profile"}, {"type":"required","contexts":"Register", "params" : [ {"name":"DependentPropertyName","value":"FirstName"} ] } ] }
{"name":"LikeOther","desc":"What do you like?", "rules" : [ {"type":"required","condition":"MustLikeSomething","failureMessage":"If you don't like cheese and you don't like chocolate, you must like something!"} ] }
{"name":"HowMuch","desc":"How much money would you like?", "rules" : [ {"type":"numeric"} ] }
{"name":"AllowCommunication","desc":"May we send you information?" }, {"name":"CommunicationMethod", "rules" : [ {"type":"required","failureMessage":"if you are allowing communication, you must choose a method", "params" : [ {"name":"DependentPropertyName","value":"AllowCommunication"}, {"name":"DependentPropertyValue","value":"1"} ] } ] }