mercredi 31 octobre 2018

Laravel FormRequest Validation: if non-required field fails validation, change the value to null and continue

I'm using Laravel 5.5 with FormRequest Validation. My current code is below. This is being used to validate the data form the request coming in.

If a nullable field fails validation in the request, I want the request to continue and make that field's value to NULL. So ,for example, if count is sent as a string instead of an integer.. I want to make the value of count NULL and continue with the request.

Is this possible using this FormRequest and if so, how?

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Response;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class FieldsCheck extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'userid' => 'required|integer',
            'custom' => 'nullable|string|max:99',
            'count' => 'nullable|integer'

        ];
    }


    protected function failedValidation(Validator $validator)
    {
        // if it fails validation, is there a way to change the failing value to null here and continue with the request?
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire