开发者

custom validator in symfony

开发者 https://www.devze.com 2023-03-20 19:16 出处:网络
I would like create custom validator for Symfony 1.4, for example check length name. I know that it exist, but i would like own.

I would like create custom validator for Symfony 1.4, for example check length name. I know that it exist, but i would like own.

I create /myapp/lib/validator/sfValidatorName.class.php

Must be there:

class sfValidatorName extends sfValidatorBase
{

  protected function configure($options = array(),
                                $messages = array()) {

      $this->addMessage('invalid', 'Invalid name!');
  }

  protected function doClean($value) {

  }
}

and how can i add for this my function, for example:

    if (count($letters) < 3) {
        return 'too small';
   开发者_高级运维 } else if (count($letters) > 43) {
        return 'too long'; 
    } 


  1. Open /lib/validator/sfValidatorString.class.php
  2. Model your validator after that one.


Since your example is exactly what sfValidatorString does, why don't you go look at it's source? Basically you just throw a validation error with the relevant error code (eg. invalid, min_length, max_length, ...).

By default any validator has the errors 'invalid' and 'required', but you can add your own with addMessage().

For this specific example, a much smarter choice is to configure or extend sfValidatorString.

0

精彩评论

暂无评论...
验证码 换一张
取 消