Hacker Rhino
Hacker Rhino

Reputation: 15

How to display en error message when validation fails in PHP Laravel

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?

Upvotes: 0

Views: 1350

Answers (3)

sajjad
sajjad

Reputation: 487

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

Upvotes: 0

Hanan Alhasan
Hanan Alhasan

Reputation: 300

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');
   }

Upvotes: 2

Behzad
Behzad

Reputation: 572

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');
        }
    }
}

Upvotes: 0

Related Questions