Save Letters, Use Short Validator Names!
Something’s been bugging me lately. Be it customer projects or code people post on the channel, I often find code that spells out long validator names, over and over again:
<?xml version="1.0" encoding="UTF-8"?>
<ae:configurations
xmlns="http://agavi.org/agavi/config/parts/validators/1.0"
xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0"
parent="%core.module_dir%/Account/config/validators.xml"
>
<ae:configuration>
<validators>
<validator class="FooBarValidatorWithVeryLongClassname">
<arguments>
<argument>foo</argument>
</arguments>
</validator>
<validator class="FooBarValidatorWithVeryLongClassname">
<arguments>
<argument>bar</argument>
</arguments>
</validator>
<validator class="string">
<arguments>
<argument>baz</argument>
</arguments>
</validator>
</validators>
</ae:configuration>
</ae:configurations>
Granted, this is one of the less documented features in Agavi, but does nobody ever wonder whether there’s a better way? There’s obviously a way to create shortcuts or the validator named string couldn’t be resolved - the full classname is AgaviStringValidator.
Actually, it’s quite simple. At any point in a validation file you can define shortnames and use like this:
<?xml version="1.0" encoding="utf-8"?>
<ae:configurations xmlns="http://agavi.org/agavi/config/parts/validators/1.0"
xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0">
<ae:configuration>
<validator_definitions>
<validator_definition name="foo" class="FooBarValidatorWithVeryLongClassname" />
</validator_definitions>
<validators>
<validator class="foo">
<arguments>
<argument>foo</argument>
</arguments>
</validator>
<validator class="foo">
<arguments>
<argument>bar</argument>
</arguments>
</validator>
<validator class="string">
<arguments>
<argument>baz</argument>
</arguments>
</validator>
</validators>
</ae:configuration>
</ae:configurations>
Validatordefinitions can include default parameters and multiple definitions can reference the same class:
<?xml version="1.0" encoding="utf-8"?>
<ae:configurations xmlns="http://agavi.org/agavi/config/parts/validators/1.0"
xmlns:ae="http://agavi.org/agavi/config/global/envelope/1.0">
<ae:configuration>
<validator_definitions>
<validator_definition name="foo" class="FooBarValidatorWithVeryLongClassname" />
<validator_definition name="foobar" class="FooBarValidatorWithVeryLongClassname">
<ae:parameters>
<ae:parameter name="bar">baz</ae:parameter>
<ae:parameters>
</validator_definition>
</validator_definitions>
</ae:configuration>
</ae:configurations>
The default parameters can be overwritten by the actual validator definition referencing that shortname. This is how all the shortnames we provide are implemented - the curious among you can have a peek in the config/defaults/validators.xml file.
Projects created with Agavi > 1.0.0 will have a cascade of validator files that are loaded: The actual validaton file for the action which references a validator config in the module’s config directory which in turn references a validator config file in the app’s config directory which in turn references the agavi default file. Older projects do not have this structure but I do recommend to create at least a global config file to shorten often used names.

