I have laravel validation request form, like this:
public function rules()
{
$id = $this->input('wlId');
return [
'id' => ['sometimes ', 'integer'],
'domain_name' => ['required', 'string', 'unique:white_label,domain_name' . $id],
'disabled' => ['required', 'boolean']
];
}
I set id for ignore my entry during the unique check, but if id is't an integer but for example a string then I will get sql error from validator.
How can I stop validation if the id field fails validation?
You are going to ignore a certain value while using unique validation rule.
Consider the following snippet;
use Illuminate\Validation\Rule;
// ...
public function rules() {
$id = $this->input('wlId');
return [
'id' => ['sometimes ', 'integer'],
'domain_name' => [
'required',
'string',
Rule::unique('white_label', 'domain_name')->ignore($id)
],
'disabled' => ['required', 'boolean']
];
}
For more details, see this link; https://laravel.com/docs/8.x/validation#rule-unique
As mentioned in the documentation you can use bail rule
Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute
$request->validate([
'title' => 'bail|required|unique:posts|max:255',
'body' => 'required',
]);
In this example, if the unique rule on the title attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned.
Related
I want to make a field mandatory if another field contains the "{" character.
I've tried several combinations but it looks like Laravel doesn't apply the rule at all.
public function rules()
{
$rules = [
'title' => 'required',
'url' => 'required',
'model_name' => 'required_if:url,url:regex:/{/',
// The following doesn't work either:
// 'model_name' => 'required_if:url,url:not_regex:/{/',
// 'model_name' => 'required_if:url,regex:/{/',
// 'model_name' => 'required_if:url,not_regex:/{/',
];
return $rules;
}
Any idea ?
If you would like to construct a more complex condition for the required_if rule, you may use the Rule::requiredIf method. This method accepts a boolean or a closure. When passed a closure, the closure should return true or false to indicate if the field under validation is required:
use Illuminate\Validation\Rule;
use Illuminate\Support\Str;
public function rules()
{
$rules = [
'title' => 'required',
'url' => 'required',
'model_name' => Rule::requiredIf(Str::contains($this->url, '{')),
];
return $rules;
}
I hope this workaround help you
I have the email and password fields in which I only want them to be required if the add_user field value is 1. This is my validation:
public function rules() {
return [
'add_user' => ['required'],
'password' => ['required_if:add_user,1', 'min:8'],
'email' => ['required_if:add_user,1', 'email', 'max:255'],
];
}
This one is not working, it always errors even if the value is 0. I fill out the add_user value using jquery. I have a drop down that if the user clicked from the options, add_user value is 0, but if thru input, value is 1. Values are correct but the validation isn't working. Can someone help me figure this out?
Okay. Got it.
I added a nullable validation property and now it works.
return [
'add_user' => ['required'],
'password' => ['required_if:add_user,1', 'min:8', 'nullable'],
'email' => ['required_if:add_user,1', 'email', 'max:255', 'nullable'],
];
Hope this will help someone, someday.
We are checking two fields that are not dependent to each other means if user_id!=1 there will be some other validation on password and email so we need to check it by this way
$rules = [
'type' => 'required'
];
$v = Validator::make(Input::all(),$rules);
$v->sometimes('password', 'required|min:8', function($input) {
return $input->type == '1';
});
Here we make an instance of Validator where we checking password is required if user_id ==1 else we no need to validate it.
In Model you can do this.
public function rules(){
return [
'user_id ' => 'required'
];
}
protected function getValidatorInstance() {
$validator = parent::getValidatorInstance();
$validator->sometimes('password', 'required|min:8',
function($input) {
return $input->user_id== 1;
});
return $validator;
}
I have a table states and fields are id,country_id,state_code,state_name.Now I want to validate that same country doesn't have a same state_code and same state_name in a table.
I tried but it's not working.
In my Controller :
$validator = State::validator($request->all(), $id);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator->getMessageBag())
->withInput($request->all());
}
Here is my validation function in model :
protected function validator(array $data, $id)
{
return Validator::make($data, [
'country_id' => 'required',
'state_code' => 'required',
'state_name' => 'required',
]);
}
How can I solved this without custom validation ?
First of all, your validation rules are only checking if these fields are present in the request. There is no validation happening for a value exists in database. First of all, you might wanna go through the documentation for the rule_exists. Secondly, you may have to update the query for this rule as per the documentation
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::exists('staff')->where(function ($query) {
$query->where('account_id', 1);
}),
],
]);
Here you can pass additional query parameters.
You do not need a custom validation for that one, laravel has a unique column validation
$this->validate($request, [
'country_id' => 'required',
'state_code' => 'required|unique:states,state_name',
'state_name' => 'required|unique:states,state_code',
]);
This way you know for sure that column values in state_code will be unique compare to state_name for more check the docs
I have created rule validation as follow :
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1'],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);
here i need to validate that each ordered_qty should be greater than qty I have tried the below way but it doesn't work for me
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1','max:items.*.ordered_qty'],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
$this->validate($request, $rules);
i tried custom validation also but i could not able to getting the value of other field
$rules = [
'items.*.qty' => ['required', 'integer', 'min:1',function ($attribute, $value, $fail) {
if ($value > ordered_qty) {
$fail(':attribute qty is invalid!');
}
}],
'items.*.ordered_qty' => ['required','numeric','min:1']
]
It validate wrong. How can i do the comparision on array of objects among their attributes
You are using the max on qty but max suspect a value while you are trying to validate it with the input of another field. I think you need to use lte, see: https://laravel.com/docs/5.8/validation#rule-lte
This my request class rules.
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'unique:event_cals,eventDate,NULL,id,venue,$request->venue,time,$request->time'
];
I want to validate a rule like below.
'eventDate' && 'venue' && 'time' => 'unique'
There I need to check if there any row without same eventDate, venue and time altogether. Anyone knows how to declare such a rule?
This is the snapshot of db table.
Here is the possible solution:
<?php
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'event_date' => "unique:event_cals,event_date,NULL,id,venue,{$request->venue},time,{$request->time}",
];
I again want to highlight that; if you want that validator to work, you should make sure that the event_date and time should be correctly formatted.
An example unique check with additional wheres from our running project's update requests:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$id = $this->route('money');
return $rules = [
'name' => "required|string|min:3|max:255|unique:moneys,name,{$id},id,deleted_at,NULL,access,public",
];
}