As a man I never read the instructions; it’s baked into our DNA. There were no instruction manuals back when we lived in caves, right? So if we didn’t need them then, we sure as heck don’t need them now. This applies doubly so when I’m working with XAF. Why? Two reason really. Firstly, it allows me to see how intuitive XAF is – does it work, out of the box, the way I naturally think it should? Secondly, when I get stuck, it allows me to see how easy it is to get unstuck using our documentation. (okay when I said I never read instructions, I meant never *before* the fact.)
Right now I’m working on an XAF application (more on that later) and I want to set up some validation rules; specifically I want to validate that a StartDate is greater than or equal to today’s date. So, naturally, I intuitively I write:
[RuleCriteria("StartDateMustBeAfterToday", DefaultContexts.Save, "StartDate >= DateTime.Now.Date")]
Which doesn’t work and throws this error:
Well okay, so much for intuition, time to find out how it should really be done. So, I open the documentation page for RuleCriteria and I read it; and it’s right there, you saw it didn’t you? No? Meh, me neither first time round. It’s there in the “Remarks” section.
“In criteria, you can use Read-Only Parameters. For instance, the "Today = '@CurrentDate'" criterion means that the Today property must be set to the current date.”
That single sentence, tucked away in the remarks section is important, hence my post today. It means two things. One, that my code should have be written as:
[RuleCriteria("StartDateMustBeAfterToday", DefaultContexts.Save, "StartDate >= '@CurrentDate'")]
Which will throw the following validation error, as required:
It also means that all the read-only parameters, which we document as being used for filtering (which is true) can also be used in RuleCriteria, so go check them out and have fun!