Respect / Validation

The most awesome validation engine ever created for PHP
https://respect-validation.readthedocs.io
MIT License
5.76k stars 774 forks source link

define different error messages #1383

Closed Bruce-chai closed 1 year ago

Bruce-chai commented 2 years ago

How to define different error messages for the same rule for different fields

illumws commented 1 year ago

translate fuction

function translate($message) // message = {{name}} must not be empty
{
    // your translation code
    return str_replace($message, 'O campo {{name}} não pode ser vazio.', $message);
}

binding translate function to factory

Factory::setDefaultInstance(
    (new Factory())->withTranslator('translate')
);

validate and get message

try {
    $value = [];
    Validator::notEmpty()->setName('cidades')->check($value);    // cidades = cities
} catch (Throwable $e){
    echo $e->getMessage(); // result - O campo cidades não pode ser vazio.
}
nickl- commented 1 year ago

Closed: asked and answered