Laravel validation date field before:another_date_field plus some period - laravel

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;

Related

Laravel Validation: how to validate n days after date field 1?

Is there a way to validate directly such that date field 2 is after n days of date field 1?
For example:
'date_1' => 'required|date_format:"Y-m-d"',
'date_2' => 'required|date_format:"Y-m-d"|after:date_1 +5 day'
I tried something like this but it doesnt work
The after validation rule accepts date strings and they get converted using strtotime() php function. you can use carbon to play around with the date.
Assuming you are going to use this inside a class that extends FormRequest
$new_date = Carbon::parse($this->date_1)->addDays(5);
$new_date->toDateString();
and then simply concat the value to the rule
'date_2' => 'required|date_format:"Y-m-d"|after:'.$new_date
you can also create your own validation rules.
laravel docs have a pretty decent explanation on that.

unique between 2 columns

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.

Laravel customize rules by another fields

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",
]

Laravel validator: complex rule for exists rule

I have a table that looks like this
vote_id
voter_email
target_id
vote_date
The voter can vote once per day for each target_id. I am trying to validate votes and this is what I've got so far
['email' => "exists:participants_votes,voter_email,target_id,$targetId,vote_date,$date"];
How can I check the date for a range? Is there a way to do it without writing custom code?
There is the after:date validator but it has to be in the same scope as exists:
Basically something like this: (this sql could be totally wrong though)
Select MAX(vote_date) FROM parcitipants_votes WHERE voter_email = ?
then I would check if this date is > now - 24 hours.
I'm trying to do it the "right" way by using validator but maybe this is not meant for it...
Note: I cannot just have 1 entry per email with a vote_count, because I need to be storing information such as the vote_date for each vote, even if its from the same person.
I decided not to use the validator for this complex scenario and to just query the database, it was simple and effective.
$voteDate = \DB::table('participants_votes')
->where('voter_email', '=', $user->email)
->where('contest_id', '=', $contestId)
->where('target_user_id', '=', $targetId)
->where('created_at', '>', new \DateTime('now -24 hours'))
->pluck('created_at');
voteDate would be null if the email has voted in the past 24 hours.

Laravel 4: making a combination of values/columns unique

I'm importing a bunch of csv entries in my database with Laravel 4.
I can't really point at one column that has to be unique, it's a combination of 5 columns that makes it unique. However: how does one define this in Laravel?
Option 1: schema builder
You can use the $table->unique('email') method, but that only seems to allow one column, not a combination of columns.
Option 2: Validation
Less preferable, but I could validate the model before inserting it. However, again, using 'unique:[table]' validation rules, it will return an error when just one of the column values isn't unique, not a combination of them.
Can anyone tell me how I should go about this?
I'm sure I'm missing something, but I could use a push in the right direction :-)
Thanks,
Dieter
You can combine:
$table->unique( array('email','name') );
And pretty much everything in Laravel will accept arrays to do whatever you need to with 'more than one'.
Use Schema Builder's unique() method to define your data model, as Antonio mentioned.
Additionally, if you want to use validation on your model, consider my custom Validator rule for multiple UNIQUE indexes: https://github.com/felixkiss/uniquewith-validator
You can also do this;
$table->unique(["column1", "column2"], 'uq_columns');
Which means that you will have a unique column combination of all the columns i.e. column1 and column2
I know this question is for Laravel 4, but I just came across this on searches and found a solution for Laravel >= 5.3
Here it is:
Of course, the migration may look something like
$table->unique( array('email','name') );
Then to validate this, you do not need to use custom rules, just advanced rules:
'email' => Rule::unique('users')->where(function ($query) use ($request) {
return $query->where('name', $request->name);
}),
Of course, you may want to validate name before of this. The name should be required so that you may finish with something like this:
'name' => 'required|max:255',
'email' => Rule::unique('users')->where(function ($query) use ($request) {
return $query->where('name', $request->name);
}),
I hope it helps.
You can try this
$table->string("name");
$table->string("email")->unique("name")

Resources