RFC: Constraints and Filters API for FormHandlerby zby (Vicar)
|on Apr 17, 2009 at 09:23 UTC||Need Help??|
For the simple cases of constraints and value transformations in FormHandler we decided to add Constraints and Filters. For the more complex needs you'll still be allowed to just subclass Field as it used to be, now we just add a way to quickly solve the simple cases.
I've looked at Gerdas latest commit - defining the has_constraint method. Initially I did not like it - as it creates another layer of indirection - but now I think it can nicely fit together with Filters.
Here is my API proposal:
So here we have one simple text field that is required to contain the string 'aaa' - this is the simple case. Then we have a number that needs to be greater than 10, but also it is being saved to the database as a string formatted with up to 8 leading zeros. And the most complex one is a DateTime field with three subfields for the year, month and day. It is saved to the database as a DateTime object, and the user is allowed to enter only dates that are Mondays. This example shows how this complex requirement is relatively easily declared using this API. One important note here is that the Compound field assembles the values of the sub-fields into a hash - and that hash is then passed to the filter as the parameter. This should be convenient for all subs with named parameters and in particular to object creators for inflation. And if the inflation fails then the error message is used.
The main point above is that with named filters and constraints the programmer can weave them together in whatever order he needs and is not constrained by the order defined by the library (like in FormFu - where there is a specific order of applying the different actions).
I am not sure about the names - transformations would be more appropriate than filters but also two times longer. Any proposals?
To save some typing (and reading) we also could use positional declarations instead of named:
Update: The alternative:
Shorter - but more identation - which one would you chose?