I've created a FormRequest to validate some fields, and I would like that one of those fields only accept the options that a I give it
Searching I found something like this
"rule" => 'required|in:Option1,Option2,Option3',
This accept only the predifined options, but in the error message only shows
"The rule type is invalid."
And I would like that too shows the valid options predefined in the rules.
How could i do this?
In the end I modified my rule to this:
"rule" => ['required', 'in:Option1,Option2,Option3']
and I customized the error message with the following
public function messages()
{
return [
'rule.in' => 'The rule field must be Option1, Option2 or Option3.'
];
}
Related
Here is to validate form request in laravel, request contains filter and field name in the filter has period(dot) present.
Sample Request url
...?filter[entity.abc][]='value'
Here entity.abc is actually a string, but laravel considers it to be array of object, when rule is given for 'filter.entity.abc'
filter:[
[entity]: [ {abc:'value'}]
]
which is actually
filter:[
[entity.abc]:['value']
]
So we need to make regex for second dot, which equivalents to:
public function rules()
{
return [
'filter.entity\.abc' => ['bail', 'sometimes', 'array'],
'filter.entity\.abc' => ['uuid']
];
}
Above always retuns true,even when invalid uuid is present
why not modify your request like this?
...?filter[entity][abc][]='value'
Edit:
You can use custom validation in laravel where you can deconstruct the parameters and check the values manually
Laravel Custom Validation
https://laravel.com/docs/8.x/validation
So I'm building a form and I need specific fields to be empty.
They return an empty string and from other similar questions, I looked for
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class
in Kernel.php which is commented out by default, I believe.
I don't want to change its behavior since it's a global middleware.
I have tried making them nullable, string|sometimes, present|max:0 yet none of these give me the desired result. I want the validation to pass only if the fields are empty.
Any help will be deeply appreciated.
So as I understood, you want for specific field to be required, if the other field in form is empty? To achieve that, you can use required_without property in Request validation like this:
public function rules()
{
return [
'filed_name' => 'required_without:other_field_name',
];
}
public function messages()
{
return [
'filed_name.required_without' => 'filed_name is required.',
];
}
More on validation on official documentation.
I have my route set as
Route::any('/{brand?}/{type?}/{city?}', 'SearchController#index')->name('search');
I want to send from my controller query strings (Form GET params)
After searching I ended up with this but it does not work properly
return redirect()->route('search', [$brand->name, $type->name, 'search_model_from' => $request->search_model_from, 'search_model_to' => $request->search_model_to]);
which returns back
localhost:8000/toyota/avalon/2018?search_model_to=2019
I want to return
localhost:8000/toyota/avalon/?search_model_from=2018&search_model_to=2019
What I am trying to achieve in general is SEO friendly search functionality
Maybe you should try to assign city as null like that :
return redirect()->route('search', [
'brand' => $brand->name, 'type' => $type->name,
'city' => '', 'search_model_from' => $request->search_model_from,
'search_model_to' => $request->search_model_to
]);
I'm not sure but this could happen because you have defined 3 optional parameters in the route and as you are sending just two of them, this might takes the next (in this case 'search_model_from') as the third parameter for url.
Maybe if you cast and set a default value to the optional parameters in your Controller, you won't have that trouble, like this:
public function index(string $brand='', string $type='', string $city='' , $other_parameters)
In Laravel you can make a custom messages for validators. But I found that the prepared messages are little bit wrong. With before and after validation rules, the parameter is converted with strtotime like that said in the documentation.
So if I set rule 'expires' => 'before:+1 year' the rule is working and correct. But if the user inputs a wrong value, Laravel prints the message like that:
The expires must be a date before +1 year
This message is very unclear for the average client. I was expected that the message will be converted with the strtotime also.
There is a clean way to print more clear error message?
You can override the validation messages with a custom ones if you want to.
In the Form Request class, add the following method and change the messages like so:
public function messages()
{
return [
// 'fieldname.rulename' => 'Custom message goes here.'
'email.required' => 'Er, you forgot your email address!',
'email.unique' => 'Email already taken m8',
];
}
Update:
If you want to change the global messages, you can open the validation.php file inside resources/lang/en/validation.php and edit the message there.
You can user AppServiceProvider.php
and you can write your new validation rule there and set your error message in validation.php file
/app/Providers/AppServiceProvider.php
/resources/lang/en/validation.php
Ex:
Validator::extend('before_equal', function ($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
and the message is
'before_equal' => 'The :attribute field is only accept date format before or equal to end date',
Please try this and you can alter this to based on your require.
How can I create a model rule that's only required when a certain value from the Database is 1?
I tried using a 'required', 'when' rule but that doesn't seem to update the client-side JavaScript.
I also tried a custom inline validator but that doesn't seem to post an empty field.
Scenario's aren't an option I think as I have 6 fields and can have any combination of required/not required.
EDIT
At the moment I just never add the required rules, instead of directly returning the rules I store them in a variable. $rules = []
Then before I return the variable I add the required options to the array.
if($x->x_required)
$rules[] = ['your-field', 'required', 'on' => 'your-scenario'];
This is a quickfix and I don't really like it, but it works. I'm not sure if there is a better way of doing this.
You need to use combination required with when, but for client side validation you need additionally specify whenClient property.
Example (add this to your rules()):
[
'attributeName',
'required',
'when' => function ($model) {
return $model->country == Country::USA;
},
'whenClient' => function (attribute, value) {
return $('#country').value == 'USA';
},
],
Official docs:
RequiredValidator
Validator $when
Validator $whenClient