Search code examples
phpsymfony1symfony-1.4

customize errors "required" Symfony 1.4 (dynamic label)


In "ProjectConfiguration" I can customize the output errors, such

sfValidatorBase::setDefaultMessage('min_length', 'Min% min_length% sign');
sfValidatorBase::setDefaultMessage('required', 'Label required');

But for the field "required", I can not substitute the dynamic field name, like this:

sfValidatorBase::setDefaultMessage('required', 'Label %label% required');

How can I fix it?


Solution

  • You can't. Or you will have to hack into the global sfValidatorBase.


    If you take the example of min_length error, it is throwing like this (in sfValidatorString):

    throw new sfValidatorError($this, 'min_length', array('value' => $value, 'min_length' => $this->getOption('min_length')));
    

    As you can see, it takes parameters for value and min_length since the message is defined like this :

    $this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).');
    

    So it will automatically replace each variable with %....%.

    For required error, it's throwing from the sfValidatorBase (the one which is extended by all validator):

    throw new sfValidatorError($this, 'required');
    

    As you can see, nothing is given as third parameter, so you can't configure extra parameter. If you want to do so, you should change:

    throw new sfValidatorError($this, 'required', array('label' => $this->getOption('label')));
    

    But this change will be inside the Symfony repository. And this sfValidatorBase is extended on every validator, I don't know how you can apply this modification. At least, you know where do make changes.