0

I have an application, which for each method it does a field validation before proceeding with code execution.

public function authenticateSeller(Request $request)
    {
         $fields = $request->all();

        $rules = config("validation-rules.loads.access");
        $validator = Validator::make($fields, $rules);

        if($validator->fails()) {
            throw new CustomFieldValidation($validator);                
        }
    }

I needed to pass the object to the CustomFieldValidation class to pass to the Laravel Handler class to handle the errors and return in the JSON form. How to do this?

class CustomFieldValidation extends \Exception
{
    public function __construct($validator,$message= NULL, $code = NULL, Exception $previous = NULL)
    {
        parent::__construct($message, $code, $previous);
    }

}

I was hoping to manipulate the message handler's rendering method.

public function render($request, Exception $exception)
    {

        if($exception instanceof CustomFieldValidation) {
            foreach($validator->errors()->all() as $message) {
                $errors[] = [
                    'message' => $message,
                ];
            }

            return response()->json($errors, 400, ['Content-type' => 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);
        }

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

Can someone help me?

2
  • 1
    Why throw an error at all if you're just going to immediately handle it? Just return your json. Commented Apr 27, 2017 at 20:21
  • Thank you very much! Actually, my idea is to create a function that I can always call at the beginning of each method, for example checkFields ($ fields, $ rules) and this function in case of validation failure throws an exception that will always be treated in the same way, And I just need to do the code once. Commented Apr 28, 2017 at 13:22

1 Answer 1

1

To answer your question: just add a $validator attribute to your exception class.

class CustomFieldValidation extends \Exception
{
    public $validator;

    public function __construct($validator,$message= NULL, $code = NULL, Exception $previous = NULL)
    {
        parent::__construct($message, $code, $previous);
        $this->validator = $validator;
    }

}

public function render($request, Exception $exception)
{

    if($exception instanceof CustomFieldValidation) {
        foreach($exception->validator->errors()->all() as $message) {
            $errors[] = [
                'message' => $message,
            ];
        }

        return response()->json($errors, 400, ['Content-type' => 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);
    }

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

@Samsquanch is right, the use case example you're showing is simple and it'd be better to just return JSON from the controller. However if you're throwing the exception from some other class, this would make sense.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Actually, my idea is to create a function that I can always call at the beginning of each method, for example checkFields ($ fields, $ rules) and this function in case of validation failure throws an exception that will always be treated in the same way, And I just need to do the code once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.