October 24, 2024
Chicago 12, Melborne City, USA
PHP

Validation excludes specific fields from request data


I’m working on a Laravel project where I’m handling property creation, and I’m facing an issue where specific fields (facilities and additional_facilities) are missing from the validated data even though they are present in the request.

In my controller’s store() method, I use PropertyStoreRequest for validation. The request contains fields such as facilities, additional_facilities, amenities, etc.

After validation, I expect these fields to be present in the $validatedData, but for some reason, only the amenities are included, while facilities and additional_facilities are missing.

Here’s the relevant code:

Request Validation (PropertyStoreRequest):

class PropertyStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        return auth()->user()->isHost();
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'location' => 'required|string|max:255',
            'price_per_night' => 'required|numeric|min:0',
            'max_guests' => 'required|integer|min:1',
            'photos' => 'required|array|min:3',
            'photos.*' => 'image|mimes:jpg,jpeg,png|max:2048',
            'facilities' => 'required|array',
            'facilities.*' => 'integer|exists:facilities,id',
            'additional_facilities' => 'nullable|array',
            'additional_facilities.*' => 'integer|exists:additional_facilities,id',
            'amenities' => 'required|array',
            'amenities.*' => 'integer|exists:amenities,id',
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        Log::error('Validation failed:', $validator->errors()->all());
        throw new HttpResponseException(response()->json([
            'errors' => $validator->errors(),
        ], 422));
    }
} 

Controller (store() method)

public function store(PropertyStoreRequest $request)
{
    Log::info('Property coming data:', $request->all());

    $validatedData = $request->validated(); // Facilities and additional_facilities missing

    // Create property
    $property = Property::create([
        'title' => $validatedData['title'],
        'description' => $validatedData['description'],
        'location' => $validatedData['location'],
        'price_per_night' => $validatedData['price_per_night'],
        'max_guests' => $validatedData['max_guests'],
        'photos' => json_encode($this->handleFileUpload($request, 'photos', 'property_photos')),
        'user_id' => auth()->id(),
        'is_available' => $validatedData['is_available'] ?? true,
    ]);

    // Syncing relations
    $this->syncRelations($property, $validatedData, $request);

    return response()->json($property, 201);

Logging output shows that facilities and additional_facilities are present in the raw request but not in $validatedData:

[2024-10-24 13:45:01] local.INFO: Property coming data: {…}

[2024-10-24 13:45:01] local.INFO: validated data from request : {… "amenities":["1","2"], "photos":[…]}

[2024-10-24 13:45:01] local.INFO: Facilities to sync: []

[2024-10-24 13:45:01] local.INFO: Additional Facilities to sync: []

[2024-10-24 13:45:01] local.INFO: Amenities to sync: [1,2]

What I’ve tried:

  • Checked database existence: Both facilities and additional_facilities tables contain valid IDs that match the request.

  • Manually overriding data: I’ve manually set the facilities and additional_facilities in the controller after validation, and the relationships sync properly, which means there’s no issue with syncing—only with validation.

Logged the validation failure: No validation errors are logged, which suggests that validation for these fields is passing, but they still don’t appear in $validatedData.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video