Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically switching the error message on validation?

With the new validator object - is it possible to replace the validation error inside the validation rule triggered? to not only return the static error message but maybe some dynamically genereted one?

public function validateLength($data) {
    ...
    $length = mb_strlen($data['name']);
    $this->validator()->getField('name')->setRule('validateLength', array('message' => $length . 'chars')); 
    ...
}

does not work, of course (too late I guess)

I want to actually return the lenght of the string (you used 111 chars from 100 allowed) for example - but for this I would need to be able to replace the message from inside the (custom) validation method

$this->validate['name']['validateLength']['message'] = $length . 'chars';

also never worked so far. it would always return the previous (static) error message from the $validate array

like image 221
mark Avatar asked Jul 15 '12 19:07

mark


1 Answers

public function customValidator($data) {
    ....
    if ($validates) {
        return true;
    } else { 
        return 'my new error message';
    }
}
like image 89
ADmad Avatar answered Sep 22 '22 05:09

ADmad