【问题标题】:Laravel, Carbon, and ValidationsLaravel、Carbon 和验证
【发布时间】:2017-08-01 05:55:21
【问题描述】:

我在 Laravel 4 和 Carbon 中遇到了一个奇怪的验证问题。

我有一个表单,顶部有一些必需的常规字段(几个下拉菜单和一个文本字段),然后是一系列不需要的日期和时间字段。

我有一个验证器设置为需要前 3 个字段,并且日期/时间字段设置为 'date_format:Y-m-d H:i'(秒在这里并不重要)。

下面的“airline_open”、“airline_close”、“hotel_open”、“hotel_close”字段是导致问题的原因。

只要填写了必填字段,一切正常。如果未填写必填字段,并且没有填写日期/时间字段,那也很好(我会收到适当的消息)。但是,如果未填写任何必填字段,并且使用预期格式(无秒)填写任何日期/时间字段,我会收到 Carbon 引发的异常。 (有趣的是,如果我包括时间的秒数,它不会抛出异常,但会给出验证错误。)

这个异常好像和这个问题类似:Laravel Carbon Data Missing

但是,只要填写了必填字段,一切都会按设计进行,感觉我应该能够完成这项工作,而不必弄乱 Carbon 的日期格式。

有什么想法吗?

添加一些代码:

验证器:

        'direction' => 'required',
        'type' => 'required',
        'notes' => 'required',
        'callback_time' => 'date_format:H:i',
        'callback_time2' => 'required_if:callback_type, 4|date_format:H:i',
        'airline_open' => 'date_format:Y-m-d H:i',
        'airline_close' => 'date_format:Y-m-d H:i',
        'hotel_open' => 'date_format:Y-m-d H:i',
        'hotel_close' => 'date_format:Y-m-d H:i'
    );

来自表单(airline_open 和airline_close 字段)。 “现在”按钮调用一些 js 以所需格式使用日期和时间填充字段:

        <tr class="airline-reservation @if ($data->airline_reservation == 0) _hide @endif">
            <td width="25%">{{ trans('nationalbank.call.form.open') }}:</td>
            <td width="25%">
            <input type="text" class="form-control" name="airline_open" id="airline_open" @if ($data->airline_open > 0)) value="{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$data->airline_open)->format('Y-m-d H:i') }}" @endif size="30">
            <a href="javascript:;" class="btn btn-info btn-xs nowbtn" id="now-airline_open">now</a>
            </td>
            <td width="25%">{{ trans('nationalbank.call.form.close') }}:</td>
            <td width="25%">
            <input type="text" class="form-control" name="airline_close" id="airline_close" @if ($data->airline_close > 0)) value="{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$data->airline_close)->format('Y-m-d H:i') }}" @endif size="30">
            <a href="javascript:;" class="btn btn-info btn-xs nowbtn" id="now-airline_close">now</a>
            </td>
        </tr>

Screenshot of error:

【问题讨论】:

  • 显示一些代码。
  • 此外,如果我更改字段验证和格式以包含秒数,但从字段中的时间中删除秒数,我仍然会抛出异常。如果我输入垃圾(非日期/时间文本),我会按预期收到验证错误,但“不完整”的日期/时间(例如,删除时间并只留下日期)会引发异常。
  • 请发布您遇到的确切错误。该错误可能与保存日期而不是验证日期有关。
  • 我附上了上面错误的截图。消息是“数据丢失(查看:/var/www/novus-calltracker-2/app/modules/nationalbank/views/call/form.blade.php)”

标签: php validation laravel-4 php-carbon


【解决方案1】:

验证失败,因为您无法将此类日期保存在需要解析的数据库中。

我建议首先将模型中的日期字段放在 $dates 变量下

protected $dates = ['your_date'];

然后做一个setter和getter:

// save the date in UTC format in DB table
public function setYourDateAttribute($date)
{
    $this->attributes['your_date'] = Carbon::parse($date);
}

// convert the UTC format to my format
public function getYourDateAttribute()
{
    return Carbon::parse($this->attributes['your_date'])->format('d.m.Y H:i');
}

【讨论】:

  • 但是如果我填充了必填字段,它确实可以很好地保存在我的数据库中(秒总是设置为 00)。请参阅上面我在键入此代码时添加的代码,了解我如何处理日期的显示。
  • 问题与验证无关。我已经发布了一个合适的解决方案。
【解决方案2】:

该错误与您视图中的代码form.blade.php有关。

您在哪里调用以下内容:

Carbon\Carbon::createFromFormat('Y-m-d H:i:s',$data->airline_open)->format('Y-m-d H:i')

将其更改为:

(new Carbon\Carbon($data->airline_open))->format('Y-m-d H:i')

对使用 Carbon 的两条线都这样做,它应该可以解决问题。

【讨论】:

  • 谢谢!这似乎做到了!
猜你喜欢
  • 2019-05-04
  • 2018-06-05
  • 2018-03-12
  • 2020-11-24
  • 2015-10-01
  • 1970-01-01
  • 2020-11-06
  • 2017-02-18
  • 2020-10-29
相关资源
最近更新 更多