0

I'm stuck in displaying a message in form validation.

Here is my code:

class InputController extends Controller
{
    public static function save(Request $request)
    {
        //Get input values to variables respectively
        $First_Name = $request->input('First_Name');
        $Last_Name = $request->input('Last_Name');
        $Email = $request->input('Email');
        $Credit_Card = $request->input('Credit_Card');
        $Password = $request->input('Password');

        //Rules for each field
        $rules = [
            'First_Name' => ['required', 'string'],
            'Last_Name' => ['required', 'string'],
            'Email' => ['required', 'string'],
            'Credit_Card' => ['required', 'string'],
            'Password' => ['required', 'string']
        ];

        //Validator variable
        $valid = Validator::make($request->all(),$rules);

        //Insert information into SQL table
        $customer = new CustomersTable;
        $customer->First_Name = $First_Name;
        $customer->Last_Name = $Last_Name;
        $customer->Email = $Email;
        $customer->Credit_Card = $Credit_Card;
        $customer->Password = Hash::make($Password);

        $customer->save();

        //If validation fails, pop out a message or else view localhost/submit
        if ($valid -> failed())
        {
            dd("Error! Some of the required information are empty");
        }
        else
        {
            return view('submit');
        }
    }
}

In the below:

if ($valid -> failed())
{
    dd("Error! Some of the required information are empty");
}
else
{
    return view('submit');
}

,when I let every field (First_Name, Last_Name,...) in the form empty and click the button to post, the validation should be fail and the page should display

"Error! Some of the required information are empty"

But instead, it pops out the error of SQL:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'First_Name' cannot be null

, which means it jumped into "else" statement. Can anyone explain it for me?

6
  • Would the documentation on Displaying The Validation Errors help? Commented Aug 22, 2021 at 10:23
  • read this link stackoverflow.com/questions/59806074/… Commented Aug 22, 2021 at 10:25
  • @brombeer Is there any other way? Commented Aug 22, 2021 at 10:26
  • @brombeer see link Commented Aug 22, 2021 at 10:27
  • @sajjad Still didn't work for me. Commented Aug 22, 2021 at 10:37

3 Answers 3

2

you have to put this code before you insert the data

if ($valid -> failed())
{
    dd("Error! Some of the required information are empty");
}

your code should be

//Validator variable
    $valid = Validator::make($request->all(),$rules);
    if ($valid -> failed())
    {
        dd("Error! Some of the required information are empty");
    }
    else
    {
        //Get input values to variables respectively
        $First_Name = $request->input('First_Name');
        $Last_Name = $request->input('Last_Name');
        $Email = $request->input('Email');
        $Credit_Card = $request->input('Credit_Card');
        $Password = $request->input('Password');

        //Insert information into SQL table
        $customer = new CustomersTable;
        $customer->First_Name = $First_Name;
        $customer->Last_Name = $Last_Name;
        $customer->Email = $Email;
        $customer->Credit_Card = $Credit_Card;
        $customer->Password = Hash::make($Password);

        $customer->save();
        return view('submit');
   }
Sign up to request clarification or add additional context in comments.

Comments

0

I think this is because of you are assigning request values to variables before validation. Try this:


class InputController extends Controller
{
    public static function save(Request $request)
    {
       
        $request->validate([
            'First_Name' => ['required', 'string'],
            'Last_Name' => ['required', 'string'],
            'Email' => ['required', 'string'],
            'Credit_Card' => ['required', 'string'],
            'Password' => ['required', 'string']
        ]);


        //Get input values to variables respectively
        $First_Name = $request->input('First_Name');
        $Last_Name = $request->input('Last_Name');
        $Email = $request->input('Email');
        $Credit_Card = $request->input('Credit_Card');
        $Password = $request->input('Password');

        //Insert information into SQL table
        $customer = new CustomersTable;
        $customer->First_Name = $First_Name;
        $customer->Last_Name = $Last_Name;
        $customer->Email = $Email;
        $customer->Credit_Card = $Credit_Card;
        $customer->Password = Hash::make($Password);

        $customer->save();

        //If validation fails, pop out a message or else view localhost/submit
        if ($valid -> failed())
        {
            dd("Error! Some of the required information are empty");
        }
        else
        {
            return view('submit');
        }
    }
}

2 Comments

if you wanna show the error messages in form you can do this by blade @error component, so if validation fails user redirects to submit view with errors shown.
I thought there is a 'require' property in $rules so when letting all fields empty, the validation should always fail?
0
if ($valid->fails())
{
   return redirect()->back()->withErrors($valid)->withInput();
}

and in blade template show errors with below code

@if ($errors->has('First_Name'))
    <span class="invalid-feedback">
        <strong>{{ $errors->first('First_Name') }}</strong>
    </span>
@endif

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.