Requests


Writing the validation logic

Now we are ready to fill in our store method with the logic to validate the new product post. To do this, we will use the validator method provided by shadow. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests. To get a better understanding of the validate method, let's jump back into the store method:

public function store() {
    $validate = validator()->check(input()->all(), [
        'title' => ['required' => true, 'min' => 2, 'max' => 100], 
        'price' => ['required' => true],
        'description' => ['required' => true], 
        'image' => ['required' => true]
    ]);

    if($validate->passed()) {
        // validation passed
    } else {
        $errors = $validate->errors();
        dump($errors);
    }
}