Do you know why the named bag messages are not appearing? - php

I have a page that has a form with a select menu and if there are some validation errors in that form the validation errors are shown using "#include('includes.errors')". But in this same page I have a button that when the user clicks in it it shows a modal where the user can introduce a subject, a message to send an email. In this modal I also have "#include('includes.errors')".
Issue: So the issue is that if there are validation errors in the form in the modal because the subject or messare were not filled by the user that errors appear on the modal but also on the same page above the form that has the select menu. Also if there are some validation errors in the form that has the select menu and the user opens the modal that validation erros also appear in the modal.
To fix this issue using named bags is not working. For example in the storeQuantities() there is:
public function storeQuantities(Request $request, $id, $slug = null)
{
$validator = $request->validate([
'rtypes' => ['required', 'array', new RegistrationTypeQuantity],
]);
// dd('test'); dont shows
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'quantitiesError');
}
...
}
In the contactOrganizer:
public function contactOrganizer($id, Request $request)
$validator = $this->validate($request, $rules, $customMessages);
// dd('test'); dont shows
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'contactErrors');
}
}
And then use:
#include('includes.errors', ['errors' => $errors->quantitiesError])
And in the modal:
#include('includes.errors', ['errors' => $errors->contactErrors])
But its not working it appears always that the bag is empty in both cases with "{{dump($errors->contactErrors)}}" and "{{dump($errors->quantitiesError)}}" like:
"MessageBag {#336 ▼
#messages: []
#format: ":message"
}"
It seems that the issue is because there is some error in "$validator = $this->validate($request, $rules, $customMessages);", any code after this line like "dd('test);" dont appears.

Laravel will automatically redirect the user back to their previous location when $this->validate is used
You need to use Validator::make instead Manually Creating Validators
$validator = Validator::make($request->all(), [
'rtypes' => ['required', 'array', new RegistrationTypeQuantity],
]);
if ($validator->fails())
// redirect with errors
}
And don't forger to include use Validator;

Related

The smart way of set custom validations message in laravel without making a request class

I found difficulties to set custom validation message without making a
request class. That's why I am explaining it for a better
understanding.
Default validation of laravel:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|unique:categories',
]);
$input = $request->all();
Category::create($input);
Session::flash('create_category','Category created successfully');
return redirect('admin/categories');
}
It will show the default message of laravel. In this question-answer section I will show how easily I solved this problem with the help of laravel documentation.
you can find other ways of doing this here in laravel documentation.
You have to simply pass the three values to the validate parameter.
Your input as $request
Your rules as $rules
Your custom message as $message
public function store(Request $request)
{
$rules = ['name'=>'required|unique:categories'];
$message = [
'name.required' => 'The category name is required',
'name.unique' => 'Category name should be unique'
];
$this->validate($request, $rules, $message);
$input = $request->all();
Category::create($input);
Session::flash('create_category','Category created successfully');
return redirect('admin/categories');
}
I found that this is the smartest way of doing custom validation without making a request class. If your input field is a few and you want to your validation in the controller then you can do your validation in this way.
Thank's for reading.

How to change error message Title is required in Laravel [duplicate]

This question already has answers here:
How to return custom error message from controller method validation
(5 answers)
Closed 4 years ago.
Good day to all, I want to change default error message as "Title is required" to "Please enter title" The code I use:
Controller
$this->validate($request, [
'Title'=>'required',
]);
Also, how can I ensure that a user cannot save the same data into database, for example, if there is already a Title as Movie 43 we do not have to let user save that Title again in the database.
The signature of the validate function is:
public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
You can pass in custom messages as the third parameter. The key of the custom message can be either field_name for all errors relating to that field, or you can be more specific and use field_name.rule. In this case you should use:
$this->validate(
$request,
['Title' => 'required'],
['Title.required' => 'Please enter title']
);
use Validator;
if you have much more validations this could be better
$validator = Validator::make($request->all(), $rules, $messages);
Try this
$rules = [
'Title'=>'required|unique'
];
$messages = [
'Title.required' => 'Please Enter Title',
'Title.unique' => 'Please Enter Unique Title'
];
$validator = Validator::make(Input::all(), $rules, $messages);
And above declaration of controller class
use Validator;
use Illuminate\Support\Facades\Input;
Hope it helps you!

Laravel Post Request

I have a table called Customer and with a Get Request I can already get all the Data (which I created with phpMyAdmin) on a HTML Template.
Now I want to create a new Customer with a Post Request.
This is the way I thought it would work:
In the Controller:
public function addNewCustomer(Request $request)
{
return \app\model\Customer::create($request->all());
}
The route:
Route::post('posttest', 'CustomerController#addNewCustomer');
How can I create a validation for it?
You can add validation of form like below in addNewCustomer,
public function addNewCustomer(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'body' => 'required',
]);
return \app\model\Customer::create($request->all());
}
For more information about input validation in Laravel Please readout Laravel Validation Documentation

Laravel validation with Bootstrap Modal

I've included this Github script to make a validation form with Bootstrap Modal everything is set up correctly but when I included this script the form don't submit because there are some errors which is good but it doesn't show them in the view
Controller
$this->validate($request, [
'nom' => 'required|unique:filieres'
]);
View
#if($errors->get('nom'))
#foreach($errors->get('nom') as $error)
<li class="has-error">{{$error}}</li>
#endforeach
#endif
can you just type this code in your view file
#include('includes.form_error')
second type this in your controller
public function store(UsersRequest $request)
{
}
and you create UserRequest file in Requests folder and type this
public function rules()
{
return [
'name' => 'required',
];
}
after you got proper error

Laravel 5 make a custom register function

Laravel 5 has it's default register function which is in
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
I know that I can copy this code and paste it in my AuthController but there's a change that I need to make which I don't know where to start and find. What I want is change the code for the insertion of data in my users table. I want to change this because I add another column in my users table which is company_name and I have a table which is named companies so basically when the user enter a company_name for registration it will check the companies table if it is existing then return error message if it is. So think there is something like:
$rules = array(
'company_name' => 'unqiue:companies',
);
But i don't know where to put this thing in my registration code. Thanks
You can use a custom validation, in this case. Make sure, you a are calling $this->validate(), and not $this->validator. This validate will automatically redirect back with errors if it fails, so you can skip the check statement.
public function postRegister(Request $request)
{
$this->validate($request->all(), [
'company_name' => 'unique:companies',
// And the other rules, like email unqiue, etc..
]);
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}

Categories