xtremer360
3 months ago
155
0
Laravel

Validated Except for user creation

Posted 3 months ago by xtremer360

I have a situation where I am attempting to create a model-based off request data that gets validated however I have an after validated hook in my request class that merges a new field and removes two existing fields from the request.

The problem I'm facing is that I created a validatedExcept method that will pass in all the validated data except for any specific fields. When I do this for the model creation below its still using the validated fields which makes sense. For example its still trying to send through feet and inches instead of height, since height is what is the name of the property for the patient. I just don't know what to do here.

Is there any other suggestion of handling the code below from my model creation other than having to write out $request->only(['fields-to-send-through'])

// Uses StoreReqeust from Patients namespace
$user = User::create($request->validatedExcept('started_at'));
<?php

namespace App\Http\Requests\Patients;

use App\Models\Patient;
use Illuminate\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class StoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return $this->user()->can('create', Patient::class);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => [
                'required',
                'string',
                'min:3'
            ],
            'feet' => [
                'required',
                'integer',
                'min:5',
                'max:7'
            ],
            'inches' => [
                'required',
                'integer',
                'max:11'
            ],
            'weight' => [
                'required',
                'integer'
            ],
            'started_at' => [
                'nullable',
                'string',
                'date_format:Y-m-d H:i:s'
            ],
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'started_at' => 'date started',
        ];
    }

    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator(Validator $validator): void
    {
        $validator->after(function (Validator $validator) {
            if ($validator->errors()->isEmpty()) {
                $this->merge(['height' => ($this->input('feet') * 12) + $this->input('inches')]);
                $this->offsetUnset('feet');
                $this->offsetUnset('inches');
            }
        });
    }
}

Please sign in or create an account to participate in this conversation.