I am working on a web app and I needed to validate a form. The form has two numeric fields and I need to make sure one is greater than or equal to the other.
So I used these rules.
[
'adults' => ['required', 'numeric'],
'people' => ['required', 'numeric', 'gte:adults'],
]
Everything else is working except in one case. Say adults is missing or null but people is not. Then I would expect adults is required message. But instead I get
InvalidArgumentException: The values under comparison must be of the same type.
So it seems gte rules is comparing types of the two fields even when one is null with a required rule. How can I get around this?
I think it is a problem with the order the rules are evaluated.
If adults is evaluated first, It would fail with adults is required error. However, in case people is evaluated first, it tries to compare it to adults, throwing the error. You could solve this by conditionally adding the last rule:
[
'adults' => ['required', 'numeric'],
'people' => ['required', 'numeric', $request->has('adults') ? 'gte:adults' : ''],
]
Related
Here is a table named stages and below are the fields
I don't want to allow to add the same from and to points example, if points from 1 to 10 is already added means not allow them to add the same range, another one condition is don't allow to add in between points example in-between 1to 10 like 5 to 7 are also not allowed.
Tried laravel query
$isexist = Stage::whereBetween('from', [$request->from, $request->to])
->orWhereBetweenColumn('to','from', [$request->from, $request->to])->exists();
but it not satisfying all conditions.
Any one please help, thanks in advance.
you can validate the data like this
$validator = Validator::make($inputData, [
'label' => 'required|unique:tableName,label'
]);
if($validator->fails()){
//Throw validation errors
}
Or you can use
Model::query()->updateOrCreate(['label' => $request->label], [
'from' => $request->from,
'to' => $request->to
]);
Read more on unique validation reference: https://laravel.com/docs/9.x/validation#quick-writing-the-validation-logic
I want to validate the form data if it is just numeric/integer (as in just numbers). Based on Laravel's documentation there are two specific validators for that. But the problem I'm facing is that both the validators accept non-numeric characters such as "+" or "-".
numeric
The field under validation must have a numeric value.
integer
The field under validation must be numeric.
How can I make the validation to only accept numbers and not other non-numeric characters?
'main_telephone' => 'numeric',
'main_fax' => 'integer',
'direct_telephone' => 'integer',
'mobile' => 'integer',
Below is the screenshot
if anyone has come across this problem the best possible solution is to use regex. I do not know why I didn't think of this before.
'main_telephone' => 'integer|regex:/^[0-9]*$/',
'main_fax' => 'integer|regex:/^[0-9]*$/',
'direct_telephone' => 'integer|regex:/^[0-9]*$/',
'mobile' => 'integer|regex:/^[0-9]*$/',
I would like to create a validation rule which checks if the combination of two values is unique.
There is a field for the street (id) and the house number. Both fields are required. Further, no new entry should be created if a certain combination of street and house number already exists.
How can I achieve this with Laravel?
What I have so far is only this:
protected $rules = [
'street_id' => 'required',
'tree_number' => 'required',
];
I guess this would be possible by using Rule Objects. Then I would query the DB if a certain combination is already stored. But can this be done in a simpler way as well?
Something like below
'street_id' => ['required', 'unique:table,street_id,'.$request->input('street_id').',NULL,id,tree_number,'.$request->input('tree_number')]
I have 3 parameters that I send through request body.
At least one of them is necessary, others are optional, but it could be all 3 that would be send.
How should I validate this data? sometimes rule helps with optional parameters but how do I define that at least 1 is required?
You can use required_without as
$request->validate([
'first' => 'required_without:second,third',
'second' => 'required_without:first,third',
'third' => 'required_without:first,second',
]);
In this way you can validate at least one from three fields among (first,second and third)
Explanation of
'first' => 'required_without:second,third',
first must be present in absence or if empty of both second and third
i need to create a validation to my table "candidate_knowledges", basically in this table it accepts to columns (candidate_id, software_id), i cannot let create user_id and software more then one. but i think my validation is wrong or im not doing it right.
What im trying to say in validation is that can only exist one software_id and one candidate_id on the table, this way the candidate dont have duplicate entries.
Ex: 'software_id' => 'required|integer|unique:candidate_knowledges,candidate_id,'.$candidate->id,
Here is a way to allow only one software for each candidate:
$rules = [
'software_id' =>
'required',
'integer',
'min:1',
Rule::unique('candidate_knowledges')->where('candidate_id', $candidate->id),
],
];
In general I would suggest using fluent validation syntax (it's match easier to use and update), but if you can't (laravel < 5.3 or any other reasons):
'software_id' => 'required|integer|unique:candidate_knowledges,NULL,NULL,candidate_id,' . $candidate->id,
Hope it helps.