5.6 Property constraint for Dynamic Validation
Model property constraint can be used to declare validity constraints to be evaluated during user interaction. These serve to refine the static constraints defined via XML Schema. Property constraint takes an XPath expression that is evaluated to return a boolean value. XForms user interface controls can be styled to reflect the state of this model property via CSS3 pseudoclasses :valid and :invalid . See Section 5.1.1.
Property constraint is especially useful in constraining the cardinality of node-sets used with repeat ; this usage will be described separately in Section 5.7. Additionally, property constraint is useful in creating XForms user interfaces where values provided by the user can be used in computing additional constraints on the remaining fields in a form.
5.6.1 Constraining Travel Dates Using constraint
Consider a travel application that needs to collect departure and return dates. The XForms author can use property constraint to declare that the return date must come after the departure date and that the departure and return dates may not be the same. XForms extends XPath with the functions needed to perform such date comparisons; see Figure 5.8. Function days-from-date takes a string representing a value of type xsd:date and returns the number of days since January 1, 1970. We use this function to compute the number of days between the departure and return dates in property constraint .
Figure 5.8 Constraining return date via property constraint .
< model id ="t" xmlns ="http://www.w3.org/2002/xforms" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"> < instance > < trip xmlns =""> < depart xsi:type ="xsd:date"/> < return xsi:type ="xsd:date"/> </ trip ></ instance > < bind nodeset ="/travel"> < bind nodeset ="return" constraint ="days-from-date(return) - days-from-date(depart) > 0"/> </ bind > </ model >