Expert Spring MVC and Web Flow

(Dana P.) #1

Internally Valang converts property number values to java.math.BigDecimal, while
BigDecimalalso parses number literals. Valang’s internal date parser parses date literals that
allow for powerful date constructs.
Valang uses org.springframework.beans.BeanWrapperImplto read property values. The
property notation constructs of this class are supported in Valang. They include simple prop-
erties, nested properties, and access to Listelements and Mapentries. See Listing 9-10.


Listing 9-10.Example of Property Accessors in Valang


name
address.location
customers[0].name
salesParameters[seasonStartDate]


When accessing Mapentries keys are expected to be Strings. Since BeanWrapperImpl’s
notation is reused in Valang the Stringvalues that represent key values are not quoted.
Valang supports mathematical expressions that can contain literal values of bean prop-
erty values. Parentheses support proper mathematical prevailing. Valang considers the result
of mathematical expressions like the example in Listing 9-11 as values that can be tested.


Listing 9-11.Example of Mathematical Expressions in Valang


((10 - (5 + -3)) * (salesParameters[reduction] mod 3)) div 13

Supported mathematical operators are


•+

•–

•*

•/ or div

•% or mod

Date literals deserve special attention when discussing Valang. They are always encapsu-
lated in square brackets (“[” and “]”). Valang’s internal date parser recognizes a limited
number of date formats, but you can add your own formats.
Its date operators are the real power of the Valang date parser. Suppose we want to test if a
date is today. It would be fairly trivial to write this in Java code, but it becomes more challeng-
ing when you have to provide a date value to test a property value in a validation language
notation.
To start discovering the functionalities of the Valang date parser we introduce the
Tconstruct. Trepresents the current time, comparable to creating a new java.util.Date()
instance.
To test whether the value of the order timestamp property is earlier than the current time-
stamp with Valang, we use the constraint in Listing 9-12.


Listing 9-12.Example of Using T As a Date Literal in Valang


{ order.timestamp :? < [T] : '' : 'order.timestamp.in_past' }


CHAPTER 9 ■VALIDATION 271
Free download pdf