I have a login page http://localhost/register the register method displays the register page. I am posting the data and validating it in this way
$rules = array(
'telephone' => 'required',
'theemail' => 'required',
'fullnames' => 'required',
'profilepicture' => 'required',
'password' => 'required',
'confirm-password' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::back()->withErrors($validator);
}
I used the method above to validate a form but when i try the above code, it seems not to validate. My last app was simplistic and the form names were also the column names in the database.
In my case, the array
$rules = array(
'telephone' => 'required',
'theemail' => 'required',
'fullnames' => 'required',
'profilepicture' => 'required',
'password' => 'required',
'confirm-password' => 'required'
);
does not contain names of columns. Must the rules contain the column names of the table i am writing to?.
Try something like this:
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'telephone' => 'required',
'theemail' => 'required',
'fullnames' => 'required',
'profilepicture' => 'required',
'password' => 'required',
'confirm-password' => 'required'
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
}
Be sure to match the column name in the $rule array with the other in the DB table, and if you mean the validation error doesn't appear, then you have to put this in the blade file which you want to show in.
#if(!empty($errors))
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
of course you have to add the needed classes
Related
controller validator in laravel :
$validationController = $this->validate(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and we can make validator with :
$validationNormaly = Validator::make(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and i can't use $validationController->fails(). how can i use it?
The first validation that you mentioned will fail automatically as designed by the laravel code base.
The Validator::make() i typically use when doing ajax requests and such to the controller method. You will need to run the validation fails method to invoke the failed response like so:
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
You Can Use :
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
take advantage of the automatic redirection with error messages
I defined my route but it is not showing that Route [dealer] not defined.
Route::resource('/dealer', DealerController::class);
This is my controller where there is index, create and store method is in same page.
public function index()
{
$users = User::all();
return view('dealer', compact('users'));
}
public function create()
{
$dealers = Dealer::all();
return view('dealer', compact('dealers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'name_of_firm' => 'required',
'address' => 'required',
'number' => 'required',
]);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'name_of_firm' => $request->input('name_of_firm'),
'address' => $request->input('address'),
'number' => $request->input('number'),
]);
return redirect()->route('dealer')->withSuccess('done');
}
https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller
Look at the example from the documentation. There exists no such route in your ressource controller.
Depending on what you want you either have to use dealer.index, dealer.show or dealer.edit
Normally you would also use the plural form and not the singular form of the word.
I have a strange Laravel behaviour. If I validate my form with less than about 10 fields, everything works perfectly fine, including showing error messages (e.g. "field1 is required"):
public function myFctName(Request $request)
{
$validator = Validator::make($request->all(), [
'year' => 'required',
'field1' => 'required',
'field2' => 'required'
]);
if ($validator->fails()) {
return back()->withErrors($validator->errors())->withInput();
}
return view('companiesView');
}
My form has 23 fields. As soon as I add around 10 fields, everythings works fine if there are no validation errors. Here is the second examplecode:
public function myFctName(Request $request)
{
$validator = Validator::make($request->all(), [
'year' => 'required',
'field1' => 'required',
'field2' => 'required',
'field3' => 'required',
'field4' => 'required',
'field5' => 'required',
'field6' => 'required',
'field7' => 'required',
'field8' => 'required',
'field9' => 'required',
'field10' => 'required',
'field11' => 'required',
'field12' => 'required',
'field13' => 'required',
'field14' => 'required'
]);
if ($validator->fails()) {
return back()->withErrors($validator->errors())->withInput();
}
return view('companiesView');
}
If there is an validation error, the redirect (back()) still works. However, there is no error message displayed.
If I change the line return back()->withErrors($validator->errors())->withInput(); to return back()->withErrors($validator->errors());, the error messages are displayed. So the problem must be with the withInput() function.
Moreover, with the withInput() part in place, there is a warning in the Chrome console (not happening in Firefox though): Set Cookie header is ignored in response from url: ... Cookie length should be less than or equal to 4096 characters. I am not actively doing anything with Cookies at this point.
Does anyone know what the problem could be?
withInput() is using cookie/session under the hood to store the old values. Maybe those are flooding.
An alternate could be
$request->validate([
'year' => 'required',
]);
<input type="text" name="year" value="{{old('year')}}">
for displaying the errors
#if($errors->any())
#foreach($errors->all() as $error)
{{$error}}
#endforeach
#endif
The problem is with the session driver. It is probably set to cookie which can only store 4096 characters. Set it to file, database, or redis.
In .env
SESSION_DRIVER=cookie
to
SESSION_DRIVER=database
In the given scenario
request()->validate([
'type' => 'required',
'category' => 'required'
]);
and Again
request()->validate([
'name' => 'required',
'gender' => 'required
]);
Is it possible to get some sort of centralized or complied error that encompasses both the validations?
Then you should use Validator facade to handle this kind of cases.
for ex.
$validator = Validator::make($request->only('type', 'category), [
'type' => 'required',
'category' => 'required'
]);
$validator2 = Validator::make($request->only('name', 'gender'), [
'name' => 'required',
'gender' => 'required'
]);
if ($validator->fails() || $validator2->fails()) {
// return merge $validator->errors() and $validator2->errors();
}
I'm trying to submit data from one form into two different tables, but it is not submitting, my form contains on two parts one is model, i.e., when modal form submit then its data saves into one table and rest of other data into another table.
Controller
public function store(Request $request)
{
$request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
Input::get('patient_fname'),
Input::get('patient_lname'),
Input::get('patient_email'),
Input::get('patient_dob'),
Input::get('patient_guardian'),
Input::get('patient_gender')
]);
$form = Form::create([
Input::get('patient_name'),
Input::get('patient_insurance'),
Input::get('patient_insurance_id'),
Input::get('patient_reason'),
Input::get('patient_new'),
Input::get('patient_contact_no'),
Input::get('patient_message')
]);
return back();
}
form.php
protected $guarded = [];
protected $table = "forms";
protected $fillable=['patient_name',
'patient_insurance','patient_insurance_id', 'patient_reason',
'patient_new', 'patient_message'];
public function userdata()
{
return $this->belongsTo('App\Userdata');
}
userdata.php
protected $fillable = ['patient_fname', 'patient_lname',
'patient_email', 'patient_dob', 'patient_guardian',
'patient_gender','user_id'];
public function form()
{
return $this->hasMany('App\Form');
}
I would imagine this is because you're not supplying a key for the values in your create method.
Also, validate will return an array of the validated properties so you can just use that instead of using Input::get():
public function store(Request $request)
{
$data = $request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
'patient_fname' => $data['patient_fname'],
'patient_lname' => $data['patient_lname'],
'patient_email' => $data['patient_email'],
'patient_dob' => $data['patient_dob'],
'patient_guardian' => $data['patient_guardian'],
'patient_gender' => $data['patient_gender'],
]);
$form = Form::create([
'patient_name' => $data['patient_name'],
'patient_insurance' => $data['patient_insurance'],
'patient_insurance_id' => $data['patient_insurance_id'],
'patient_reason' => $data['patient_reason'],
'patient_new' => $data['patient_new'],
'patient_contact_no' => $data['patient_contact_no'],
'patient_message' => $data['patient_message'],
]);
return back();
}