1) new project custom validator (we will use it like value filter here):
/lib/validator/MyProjectStringLowerCase.class.php
<?php
classMyProjectStringLowerCaseextends sfValidatorBase
{/**
* @see sfValidatorBase
*/protectedfunction doClean($value){return strtolower($value);}}
2) bound it to field:
$this->setWidget('my_field_name',new sfWidgetFormInputText());
$this->validatorSchema['my_field_name']=newMyProjectStringLowerCase();
If you have some validator on that field already, you can merge them into combined validators this way:
$this->validatorSchema['my_field_name']=new sfValidatorAnd(array(
$this->validatorSchema['my_field_name'],// the original field validatornewMyProjectStringLowerCase(),));
The combined validators order influent how value will flow trough them, so if you want to have value filtrated in second validation, set MyProjectStringLowerCase as the first one.
There are 2 differences between this approach and using post processing (like doSave() for instance):
- the value here will be filtered after each send (and will show filtered in displaying of form errors)
- You can reuse it very cleanly and easily in other fields or forms in your project