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?