Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

3bood_kr's avatar

prepareForValidation Doesn't work anymore

the database columns are email, phone and the input names are admission_email, admission_phone whenever i submit the form they show as null in the database for some reason. I used to use this method to do this type of thing but it doesn't work anymore am i missing something??

<?php

namespace App\Http\Requests\Pages;
use Illuminate\Support\Facades\Auth;


use Illuminate\Foundation\Http\FormRequest;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'admission_email' => 'required|email|unique:admissions,email',
            'admission_phone' => 'required|numeric|digits_between:10,14',
            'address' => 'required|string|max:255',
            'student.*.student_name' => 'required|string|max:255',
            'student.*.admission_category_id' => 'required|numeric|exists:admission_categories,id',
            'student.*.class_room_id' => 'required|numeric|exists:class_rooms,id',
            'student.*.gender' => 'required|string|max:255|in:male,female',
            'student.*.old_grade' => 'required|numeric|min:0|max:100',
        ];
    }

    public function prepareForValidation(): void
    {
        $this->merge([
            'email' => $this->admission_email,
            'phone' => $this->admission_phone,
        ]);
    }

}

0 likes
6 replies
Snapey's avatar

should work fine, and there are other reasons to write null to the database such as forgetting to make the columns fillable

Simplest check is to dump $request->all() in your controller and check these values are present

3bood_kr's avatar

@Snapey i did a dd and the admission email and phone were present it's just that 'email' and 'phone' are not getting merged to the array using that function. I ended up using this:

public function validated($key = null, $default = null)
    {
        $validated =  parent::validated($key, $default);
        return array_merge($validated, [
            'email' => $this->admission_email,
            'phone' => $this->admission_phone,
        ]);
    }

i just thought it would be cleaner to use prepareForValidation but it doesn't work anymore for me even on the other projects i'm working on. Thanks for the help anyway.

martinbean's avatar

@3bood_kr That phone number validation rule is going to fail if a user types anything other than a number, which humans tend to do, i.e. including country codes (+44), spaces to separate groups, brackets to wrap number groups ((01234) 123456), dashes to separate groups (555-1234), and so on.

Making it numeric also means you’re going to be potentially storing incorrect numbers. For example, all phone numbers in the UK begin with a zero. If you’re just treating that as a number and casting it to an integer, then you’re going to lose any preceding zeroes and end up storing an incorrect value, i.e. 7900123456 instead of 07900123456.

Make the validation rule accept strings instead, and then use a service like Twilio to check the submitted value can be converted to an actual phone number. Then store the E.164-formatted version in your database.

1 like
joseprr87's avatar

Is prepareForValidation method executed before authorize method?

If it's correct, it is not a problem?

MrK1-Dev's avatar

Hello, I know it's been a long time, and you've definitely solved this problem in the meantime and even know more comprehensive solutions.

The prepareForValidation method does not work on the original (input) data and only validates the fields defined in the rules.

This means that if you have defined a key as

$the->merge([
'key' => 'something'
]) 

and this 'key' does not exist in the rules method, then you cannot access it in the controller in this way

$request->validated()

but you can access it in this way

$request->all()

Another solution I can suggest is this Let's say this is my prepareForValidation method

protected function prepareForValidation() {
     $this->marge([
          'key_dosent_exist_in_rules' => 'something'
      ])
}

Now this key of mine is not present in the rules method but I want to access the 'key_dosent_exist_in_rules' key when I use the

$request->validated()

command

So I do this inside the rules method

public function rules(){
     return [
          'key_dosent_exist_in_rules' => 'nullable'
     ]
}

Now I can access it in the controller like this

$request->validated()

As you know, there are many ways to solve problems in programming, and this is just one of them.

Please or to participate in this conversation.