loading
FullScreen Fecha y Hora: 28-Apr-2024 17:01 IP Pública: 3.145.105.105 Usuario: Público
16/Mar/2014 Filtering form input within Symfony form class strtolower before validate

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):

  1. the value here will be filtered after each send (and will show filtered in displaying of form errors)
  2. You can reuse it very cleanly and easily in other fields or forms in your project
 
 

Ip Pública 3.145.105.105
Navegador Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
País United States
Ciudad