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

Sinres's avatar

Save data form without validate by save button

Hello Guys,

I created a two-step form where if the validation is correct, I can go to the second stage using the next button but I would also like to create a save button which will skip the entire validation and save the data to the database no matter what is written in the fields input.

What will be the good practice of creating such a record?

0 likes
6 replies
Cronix's avatar
if ($request->has('noValidation')) {
    $this->validate($request, [$rules]);
}
<button type="submit">Save</button
<button type="submit" name="noValidation">Save without Validation</button>

If you're using a FormRequest class for separate validation, it would basically be the same thing. Check if the request has noValidation, if it's present, return the array of $rules, if not present, return an empty array.

Sinres's avatar

I tried to use your solution but somthink is wrong. Look this is my function for post form data with validation. Maybe you have any idea form me?

/**
     * Post Request to store first step info in session
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postFirstStep(Request $request)
    {

        if ($request->company_or_person == 'company') {
            $company_or_person = 'required|string|min:6|max:7';
            $company_name = 'required|min:3';
            $first_name = '';
            $last_name = '';
        } elseif ($request->company_or_person == 'person') {
            $company_or_person = 'required|string|min:6|max:7';
            $company_name = '';
            $first_name = 'required|min:3';
            $last_name = 'required|min:3';
        } else {
            $company_or_person = 'required|string|min:6|max:7';
            $company_name = 'required|min:3';
            $first_name = 'required|min:3';
            $last_name = 'required|min:3';
        };

        $validatedData = $request->validate([
            'company_or_person' => $company_or_person,
            'company_name' => $company_name,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'street_name' => 'required|min:3',
            'street_number' => 'required|numeric',
            'city' => 'required|min:3',
            'zip_code' => 'required|max:10|string',
        ]);

        if (empty($request->session()->get('data_form'))) {
            $data_form = new ContactRequest();
            $data_form->fill($validatedData);
            $request->session()->put('data_form', $data_form);
        } else {
            $data_form = $request->session()->get('data_form');
            $data_form->fill($validatedData);
            $request->session()->put('data_form', $data_form);
        }
        // dd($data_form);

        if ($request->has('noValidation')) {
            $this->validate($request, []);
            dd($request);
            return redirect('/contact-form/first-step')->with('message', 'Form save.');
        }
        
        return redirect('/contact-form/second-step');

    }

munazzil's avatar

Sorry for interrupt @cronix missed some symbols '>',@sinres try now like below.

     <button type="submit">Save</button>
     <button type="submit" name="noValidation">Save without Validation</button>
Sinres's avatar

I tried to change but it is still the same effect :-(

<button type="submit" name="noValidation" class="btn btn-success float-right mr-10 ml-2 mt-4">Save without Validation</button>
Cronix's avatar
Cronix
Best Answer
Level 67

Of course it has no effect. You're running validation earlier.

ff ($request->has('noValidation')) {
            $this->validate($request, []);
            dd($request);
            return redirect('/contact-form/first-step')->with('message', 'Form save.');
        }

that at the end will do nothing, because you've already run validation here

$validatedData = $request->validate([
            'company_or_person' => $company_or_person,
            'company_name' => $company_name,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'street_name' => 'required|min:3',
            'street_number' => 'required|numeric',
            'city' => 'required|min:3',
            'zip_code' => 'required|max:10|string',
        ]);

You want those reversed so it won't run the validation if the input is present. Put your no validate code redirect as very first thing in the method. It doesn't make sense to run all that code and validate things, just to get to the bottom and check for no validate. Check that first.

if (no validate) {
    return redirect()
}

// all other code.
Sinres's avatar

@cronix thank you :-)

Look this is my code for novalidate. What do you think about it? Maybe you have some idea how I can secure that one user can not do a lot of records. I thought to use the token for this but I do not know if it's a good idea.

if ($request->has('noValidation')) {
            $data_form = new ContactRequest($request->all());
            $request->session()->put('data_form', $data_form);
            $data_form->correctly_completed = 0;
            $data_form->save();

            return redirect('/contact-form/first-step')->with('message', 'Data form save');

}

Please or to participate in this conversation.