Datamapper get_rules on related tables |
[eluser]Dunmail[/eluser]
I have a set of get_rules that format fields depending on user preferences. They work fine when I call get() directly on the model but when fields from the table are pulled in via an include_related() call they don't get run. Code: //Here's the validation array within the Club class: It looks like the rule is directly hooked in to the primary get() call. So how do I ensure that a get_rule (or similar) is invoked on a field no matter if it's retrieved directly or via a relation?
[eluser]Dunmail[/eluser]
Found a solution. It may not be the ideal or recommended way though. I extended the DataMapper class and put the formatting functions in the new class then had my DataMapper models extend the new class. Code: class DataMapperExt extends DataMapper { Then the club class does this Code: class Club extends DataMapperExt{ Doing it this way lets all my models have access to the formatting functions, I just have to remember to name the fields correctly when accessed via a relation - see the second rule.
[eluser]WanWizard[/eluser]
The best and most flexible way to share validation rules between models is to use an extension. Load it (or autoload it), and make sure the method has a "rule_" prefix, so in this case "rule_format_name". See http://datamapper.wanwizard.eu/pages/val...stom.Rules
[eluser]Dunmail[/eluser]
Hmm. Tried that but must have being doing something wrong as I couldn't get it to work :-(
|
Welcome Guest, Not a member yet? Register Sign In |