<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\Validator;
use Throwable;

class DatatableEditorValidationException extends Exception{

    protected $validator ;
    protected $code = 422;

    public function __construct( $validator){
        $this->validator = $validator;
    }

    public function render(){
        $fieldErrors = [];
        foreach ($this->validator->errors()->messages() as $key => $error){
            $fieldErrors[] = [
                "name" => $key,
                "status" => $error[0]
            ];
        }
        return response()->json(["fieldErrors" => $fieldErrors],$this->code);
    }
}
