Sunday, May 21, 2017

Laravel not responding with validator errors

I validate a model

$validator = $c->validate($collection);

This is the validate function

public function validate($data){
    return Validator::make($data, $this->rules());;
}

These are the rules

public function rules() {

    return  array([
        'name' => [
            'required', 'You need to choose a name for your collection.',
            'unique:collections,table_name', 'A collection or collection table with this name already exists'
        ],
           ...
        ]);
}

I'm trying to send back a JSON response with the validator's errors, as such:

return response()->json($validator->errors(), 200);     

I'm currently testing validation for the 'name' rule, and the validator is failing, as expected.

However, I'm expecting it to return that rule's message ("A collection or collection table with this name already exists")

Instead, I'm getting this returned:

enter image description here

My goal is to have laravel send back the error that I need, thank you in advance for any help.



via Barry D.

Advertisement