【问题标题】:Laravel 4 Validation is BrokenLaravel 4 验证被破坏
【发布时间】:2023-03-27 00:00:01
【问题描述】:

我只需要一个简单的日期格式 mm/dd/yyyy 验证...

$rules = array(
    'renewal_date' =>  array('required', 'date_format:?')
    );

我将日期格式设置为什么? Laravel 文档可能会更好。

【问题讨论】:

  • 你能把调用验证器的代码贴出来吗——有可能是这个错误。

标签: php validation laravel laravel-4


【解决方案1】:

文档对我来说很清楚,你应该使用

日期格式:格式

“验证中的字段必须匹配根据 date_parse_from_format PHP 函数定义的格式。”

看着它:http://php.net/manual/en/function.date-parse-from-format.php,我看到你可以这样做:

$rules = array(
    'renewal_date' =>  array('required', 'date_format:"m/d/Y"')
    );

这是对它的纯 PHP 测试:

print_r(date_parse_from_format("m/d/Y", "04/01/2013"));

你也可以在 Laravel 中手动进行测试:

$v = Validator::make(['date' => '09/26/13'], ['date' => 'date_format:"m/d/Y"']);
var_dump( $v->passes() );

对我来说它正在打印

布尔真

【讨论】:

  • 'renewal_date' => array('required', 'date_format:"m/d/Y"') 不起作用
  • 添加了一个独立的测试,以便您可以查看它是 Laravel 还是您的代码中的其他任何东西。
  • 确实有问题,有些警告没有处理。刚刚在github上发布了一个问题:github.com/laravel/laravel/issues/2317.
  • 解决方法:'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9] {2}\/[0-9]{4}/')
  • @AntonioCarlosRibeiro - 你如何解释这个? sandbox.onlinephpfunctions.com/code/…
【解决方案2】:

我遇到了类似的问题,但使用的是 d/m/Y 日期格式。 就我而言,问题在于我为同一字段定义了“日期”和“日期格式”规则:

public static $rules = array(
    'birthday' => 'required|date|date_format:"d/m/Y"',
    ...

解决方案是删除“日期”验证器:您不能同时使用两者。像这样:

public static $rules = array(
    'birthday' => 'required|date_format:"d/m/Y"',
    ...

之后,一切都很好。

【讨论】:

    【解决方案3】:

    解决方法:

    'renewal_date' => array('required', 'date_format:m/d/Y', 'regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/')
    

    【讨论】:

      【解决方案4】:

      你应该和double quote一起使用,比如"Y-m-d H:i:s"

      $rules = array(
          'renewal_date' =>  array('required', 'date_format:"m/d/Y"')
                                                            ^     ^ this ones
      );
      

      在 GitHub 上讨论这个问题: https://github.com/laravel/laravel/pull/1192

      【讨论】:

      • 'renewal_date' => array('required', 'date_format:"m/d/Y"') 仍然不起作用。
      • @suncoastkid 我猜语法是错误的。尝试不使用required,例如$rules = array('renewal_date' => 'date_format:"m/d/Y"');
      • 谢谢,但这仍然不起作用......这太令人沮丧了。这应该很简单。
      【解决方案5】:

      date_format 对我不起作用,所以我做了这个自定义验证

      Validator::extend('customdate', function($attribute, $value, $parameters) {
        $parsed_date = date_parse_from_format ( "Y-m-d" , $value);
        $year = $parsed_date['year'];
        $month = $parsed_date['month'];
        $month = $month <= 9 ? "0" . $month : $month;
        $day = $parsed_date['day'];
        $day = $day <= 9 ? "0" . $day : $day;
        return checkdate($month, $day, $year);
      });
      
      $validation = Validator::make(
        array('date' => $num),
        array('date' => 'customdate')
      );
      

      【讨论】:

        【解决方案6】:

        使用 PHP date_parse_from_format (Laravel 4):

        'birthday' => 'date_format:m/d/Y'
        

        您的验证消息还将使用普通用户无法理解的"m/d/Y"。 生日与 m/d/Y 格式不匹配

        建议针对此无效响应自定义您的消息。 生日与 mm/dd/yyyy 格式不匹配

        【讨论】:

          【解决方案7】:

          来源:点击Here

          你可以用like

          $rules = [
              'start_date'        => 'date_format:d/m/Y|after:tomorrow',
              'end_date'          => 'date_format:d/m/Y|after:start_date',
          ];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-07-02
            • 2012-08-22
            • 2013-05-07
            相关资源
            最近更新 更多