Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

php - how to make an input date field greater than or equal to another date field using validation in laravel

Hi iam developing an application using laravel is there any way to make an input date field greater than or equal to another date field using validation.

I know that i can achieve this through jquery and i have already got that working, but i want to know whether is this achievable through laravel validation since laravel has got some predefined validations.

For Example

protected $validationRules = array
    (   
      'a' => 'date',
      'b' => 'date|(some validation so that the b value is greater than or equal to that of 'a')'
    );

EDIT

If there is any other approach to solve the problem using laravel concept please tell me

I tried

Validator::extend('val_date', function ($attribute,$value,$parameters) {
    return preg_match("between [start date] and DateAdd("d", 1, [end date])",$value);
});

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem. before and after does not work when dates could be the same. Here is my short solution:

NOTE: Laravel 5.3.25 and later have new built in rules: before_or_equal and after_or_equal


// 5.1 or newer
Validator::extend('before_or_equal', function($attribute, $value, $parameters, $validator) {
    return strtotime($validator->getData()[$parameters[0]]) >= strtotime($value);
});

// 5.0 & 4.2
Validator::extend('before_or_equal', function($attribute, $value, $parameters) {
    return strtotime(Input::get($parameters[0])) >= strtotime($value);
});

$rules = array(
    'start'=>'required|date|before_or_equal:stop',
    'stop'=>'required|date',
);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...