3

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'; 
    } 

2 Answers 2

2
  1. Open /lib/validator/sfValidatorString.class.php
  2. Model your validator after that one.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

i would like to learn create own validator for this simple example
Then why don't you follow my advice and have a look at sfValidatorString.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.