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

rileyshannon's avatar

Terms Checkbox FormRequest issues

Hello all!

I have been trying to have a form that has a checkbox for accepting TOS.

I have a Controller/FormRequest for this that is as follows:

"app/Http/Controllers/ApplicationController"

public function store(StoreApplicationRequest $request)
    {
        Application::create($request->validated());

        return to_route('apply.success');
    }

"app/Http/Requests/StoreApplicationRequest"

...
public function rules(): array
    {
        return [
            'name' => ['required'],
            'email' => ['required', 'email'],
            'phone' => ['required'],
            'career_aspirations' => ['required'],
            'additional_information' => ['required'],
            'referral' => ['required', 'string'],
            'is_second_career' => ['required', 'boolean'],
            'is_over_18' => ['required', 'boolean'],
            'terms' => ['accepted'],
        ];
    }
...

This request is validating correctly and working as expected. The issue I run into is when trying to persist to the DB.

The issue is that the value of the checkbox when submitted is "on" and trying to save this field as a boolean. (SQL Error 1366 (Incorrect integer value))

Here are things I have tried to fix this issue:

// In FormRequest
protected function passedValidation()
    {
        $this->replace([ // and $this->merge()
            'terms' => true, // Attempt One
			'terms' => $this->input('terms') == 'on' ? true : false, // Attempt Two
		    'terms' => $this->input('terms') == 'on' ? "1" : "0", // Attempt Three
			'terms' => "1",  // Attempt Four
        ]);
    }

The replace method appears to do nothing as dd always displays "on" as the value for terms.

The only way I have gotten this to work was by moving the validation logic into the controller and manually reassigning the value of the terms array that is returned:

// Simplified since I don't have this coded currently
public function store($request) {
		$validated = $request->validate([/** same rules as above **/]);

		$validated['terms'] = $validated['terms'] == 'on' ? true : false;

		Application::create($validated);

        // redirect
}

While this approach works, I prefer removing the validation logic into its own Form Request. Has anyone found a way to successfully implement something like this without having to then replace the value in the controller, or should I just reassign the value on the $validated array and call it quits?

Thanks in advance for any help, if you need any more information, please do not hesitate to ask.

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're on the right track with your attempts to manipulate the request data within the passedValidation method of your StoreApplicationRequest FormRequest class. However, the replace method should be used to replace the entire request data array, not just a single value. Instead, you should modify the specific value within the request data and then set it back using the merge method.

Here's how you can modify the terms checkbox value within the passedValidation method:

// In FormRequest
protected function passedValidation()
{
    $termsValue = $this->input('terms') == 'on' ? true : false;
    $this->merge(['terms' => $termsValue]);
}

This code snippet checks if the terms input is 'on' and sets it to true or false accordingly. Then it merges this value back into the request data.

Make sure that your terms column in the database is of type BOOLEAN or a compatible type that can store true/false values.

With this change, your store method in the ApplicationController should work as expected without any further modification:

public function store(StoreApplicationRequest $request)
{
    Application::create($request->validated());

    return to_route('apply.success');
}

This approach keeps your validation logic within the FormRequest and avoids the need to handle it in the controller.

rileyshannon's avatar

I know the above is a bot, however, that method still returns "on" as the value form "terms": I also had already tried this approach as well. :)

protected function passedValidation()
    {
        $termsValue = $this->input('terms') == 'on' ? true : false;

        $this->merge([
            'terms' => $termsValue,
        ]);
    }
// Data returned from DD in controller using $request->validated()
array:9 [▼ // app/Http/Controllers/ApplicationController.php:17
  "name" => "David Williamson"
  "email" => "[email protected]"
  "phone" => "+1 (447) 642-7768"
  "career_aspirations" => "Sequi ad iste iusto"
  "additional_information" => "Qui accusamus eum de"
  "referral" => "social_media"
  "is_second_career" => "0"
  "is_over_18" => "1"
  "terms" => "on"
]
Snapey's avatar

once validation passes, it does not matter what the value is. To be honest, i'm not even sure why you save it.


protected function passedValidation()
{
    $this->replace(['terms' => true]);
}

What version is this?

rileyshannon's avatar

@Snapey I suppose thats true. If the request gets validated i can assume that it was checked. Would it be better to remove it from the request instead?

Please or to participate in this conversation.