3

I`m working on a project with Nova dashboard. For some reason I can`t see validation error messages.

If there is an validation error, I can see that as an exception in browser console. But not in Nova UI.

I can see success message, if all fields of form are input correctly.

I`m new to Nova, can anyone help me to debug this issue? I mean I don`t know where to look for to figure out this issue

Error trace from browser:

{
    "errors":"Sorry, something went wrong.",
    "exception":"Illuminate\\Validation\\ValidationException",
    "message":"The given data was invalid.",
    "trace":[{
        "file":"\/home\/ausvacs\/public_html\/nova\/src\/PerformsValidation.php",
        "line":18,
        "function":"validate",
        "class":"Illuminate\\Validation\\Validator",
        "type":"->",
        "args":[]
    }]
}

Fields method of Agency nova model (Table name:agency):

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Text::make('Name')
            ->sortable()
            ->rules('required', 'string'),

    ];
}

Error on browser console:

enter image description here

Exception on browser network tab:

enter image description here

Vineeth Vijayan
  • 1,215
  • 1
  • 21
  • 33

1 Answers1

0

The issue was with in the Handler (app/Exceptions/Handler.php). Not sure if the previous developer updated this function. Anyway, the issues in this function are:

  • Status code returning in the event of validation exception is 400, thats needs to 422. Then only Vue components will display validation messages.
  • Also, in here the errors are pushed to "validation" index of "response" array. But Vue component is checking for "errors" index of "response" array.

    public function render($request, Exception $exception)
    {    
        if ($request->ajax() || $request->wantsJson()) {

            $response = ['errors' => 'Sorry, something went wrong.'];

            $status = 400;

            if ($this->isHttpException($exception)) {
                $status = $exception->getStatusCode();
            }

            if (get_class($exception) == 'Illuminate\Validation\ValidationException') {
                $response['validation'] = $exception->validator->errors();
            }

            return response()->json($response, $status);
        }

        return parent::render($request, $exception);
    }

The issue got fixed when I updated code to return error code as 422 in the event of validation exception and pushed errors into "errors" index of response instead of "validation" index.

Vineeth Vijayan
  • 1,215
  • 1
  • 21
  • 33