Laravel 4:验证之前和之后验证开始和结束日期 [英] Laravel 4: Validate start and end date with before and after validation

查看:119
本文介绍了Laravel 4:验证之前和之后验证开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证两个日期字段的形式是from_date和end_date。需要检查from_date是否小于end_date。

  $ rules = array('from_date'=>数组(' 'date_format:Ymd','before:'。Input :: get('to_date')),
'to_date'=>数组('有时','date_format: :'。Input :: get('from_date')));

这是我试过的。但这不行。如果我将to_date作为空值,它将通过错误。

解决方案

Laravel 5:



<这是遵循现代原则的更广泛的方法,更像Laravel一样。这是一个更复杂一点,但仍然容易遵循,最终的结果是更清洁。



让我们开始改变一些事情。让我们减少这个具体的问题,使用更新的数组语法和应用格式。

  $ rules = [
'from_date '=> [
'before:'。Input :: get('to_date')//这是我们将要学会做的
],
'to_date'=> [
'after:'。Input :: get('from_date')//自己做这个一个
]
];

现在让我们用创建一个新的请求php artisan make:request StoreWhateverRequest 。这将创建 App / HTTP / Request / StoreWhateverRequest.php 文件。打开它并将您的规则放在 rules()函数的返回数组中。

  return [
'from_date'=> 'date',
'to_date'=> 'date | after_field:from_date'
];

这不会工作,因为 after_field isn不可用。让我们创造一下我们需要一个扩展验证器的新类。您可以将其放在 app / Services 中。我们需要类似的东西:

 <?php命名空间App \Services; 

使用Illuminate\Validation\Validator;
使用Carbon\\Carbon;

class AfterFieldValidator extends Validator {
public function validateAfterField($ attribute,$ value,$ parameters)
{
返回Carbon :: parse($ value)> Carbon :: parse($ this-> data [$ parameters [0]]);
}
}

在上面我们有: $ attribute 这是我们正在检查的字段的名称(to_date), $ value 是我们正在检查的字段的值, code> $ parameters 是我们传递给验证器(from_date)的参数,见于'to_date'=> 'date | afterField:from_date'。我们还需要传递给验证器的其他数据字段,我们可以使用 $ this-> data 获取这些数据。那么我们只需要适当地提出逻辑。你真的不需要碳这里,但一定要解析字符串,所以我们不做字符串比较。



现在我们需要加载到应用程序。为此,将以下代码放在 app / Providers / AppServiceProviders.php 中的 boot()函数内。 p>

  Validator :: resolver(function($ translator,$ data,$ rules,$ messages)
{
return new afterFieldValidator($ translator,$ data,$ rules,$ messages);
});

最后一步是最简单的。只需将 StoreWhateverRequest 的实例注入我们的控制器。

  ... 
public function store(StoreWhateverRequest $ request)
{
...

全部完成我觉得这是一个非常实用的解决问题的方法。


I want to validate two date fields in a form which is from_date and end_date. Need to check from_date is less than end_date.

$rules = array('from_date' => array('sometimes','date_format:"Y-m-d"', 'before:'.Input::get('to_date') ),
                        'to_date' => array('sometimes','date_format:"Y-m-d"', 'after:'.Input::get('from_date') ) );

This is what i tried. But that does not work. If i give the to_date as empty value it will through the error.

解决方案

Laravel 5:

Here is more extensive approach that follows modern principles and is more Laravel-like. This is a little more complex but still easy to follow and the end results is much cleaner.

Let's start by changing a few things. Let's reduce this to the specific problem, use newer array syntax and apply formatting.

$rules = [
  'from_date' => [
    'before:'.Input::get('to_date') // This is what we will learn to do
  ],
  'to_date' => [ 
    'after:'.Input::get('from_date') // Do this one on your own
  ]
];

Now let's create a new Request with php artisan make:request StoreWhateverRequest. This will create the App/HTTP/Request/StoreWhateverRequest.php file. Open that and place your rules in the return array of the rules() function.

return [
   'from_date'       => 'date',
   'to_date'         => 'date|after_field:from_date'
 ];

This will not work yet because after_field isn't available to use yet. Let's create that. We need a new class that extends validator. You can place it in app/Services. We need something similar to:

<?php namespace App\Services;

use Illuminate\Validation\Validator;
use Carbon\Carbon;

class AfterFieldValidator extends Validator {
  public function validateAfterField($attribute, $value, $parameters)
  {
    return Carbon::parse($value) > Carbon::parse($this->data[$parameters[0]]);
  }
}

In the above we have: $attribute which is the name of the field we are checking (to_date), $value is the value of the field we are checking and $parameters is the parameters we passed to the Validator(from_date) seen in 'to_date' => 'date|afterField:from_date'. We also need the other data fields passed to the Validator, we can get these with $this->data. Then we just have to preform the logic appropriately. You really don't even need Carbon here but be sure to parse the string so we don't do string comparison.

Now we need to load this into the application. To do this put the below code inside the boot() function in app/Providers/AppServiceProviders.php.

Validator::resolver(function($translator, $data, $rules, $messages)
{
  return new afterFieldValidator($translator, $data, $rules, $messages);
});

The final step is easiest. Just inject and instance of StoreWhateverRequest into our Controller.

...
public function store(StoreWhateverRequest $request)
{
...

All done. I feel this is a pretty SOLID way to solve the problem.

这篇关于Laravel 4:验证之前和之后验证开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆