I want to add validate a field depending on another field like:
pay_time => "required",
"months" => "rules:pay_time"
When pay_time is monthly, months is 1,2,3,4,.... but if pay_time is annual, months should be 12,24,36,...
How could I pass the pay_time value to extending validating function?
I think the better way to add the yearly values with another field with other name, then you can use required_if rule to validate it.
[
pay_time => "required|in:monthly,yearly",
months => "required_if:pay_time,monthly|in:1,2,3,4,5,6,7,8,9,10,11",
years => "required_if:pay_time,yearly|in:12,24,36,48",
]
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
Laravel Help with validation
Hi everyone I want to validate an array of a foreign key to make sure it exists I am doing
'activities' => 'required|array',
'activities.*' => 'exists:admin_activities,id',
but it's skipping this validation
any solution for this
You can validate each element of an array in Laravel with "dot notation" to For example, to validate that each id in a given array input field is exists in admin_activities , you may do the following:
'activities.*.id' => 'required|exists:admin_activities,id',
I have Laravel 5.5 project. In my_table I have 2 date fields: "valid_since" and "valid_to". I need "valid_to" field was after ("valid_to" + 5 year).
I have tried:
'valid_to' => 'required|date|after:valid_since',
Also I have tried:
'valid_to' => 'required|date|after:+5 year',
This two cases was working, but not the way I want. I need something like:
'valid_to' => 'required|date|after:valid_since +5 year',
But third case doesn't work. How can I do this?
'valid_to' => ['required','date','after:'.Carbon::createFromFormat('Y-m-d', \Request::input('valid_since'))->addYear(5)]
The above approach will do what you want.
Basically in your validation for after the specific date, you will use the requested field valid_since (like you thought of) but you are going to use Carbon to actually add 5 years to it.
Try the following
'valid_to' => 'required|date|after:'.Carbon::create($request->valid_since)->add(5, 'year')->toDateString(),
Don't forge to import carbon in the controller
use Carbon\Carbon;
Suppose, in the feedback form we want to ask at least one of two contacts: email and/or telephone number. So, in the validation rules will be required_without for each other:
$this->validate($request, [
'email' => 'required_without:tel|email',
'tel' => 'required_without:email|regex:/(01)[0-9]{9}/'
], $messages);
We need to validate the contact data format only when it has been inputted, but in the code above the format of both fields will be checked. Because we need at least one contact, sometimes rule is no use.
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.