doulu1914 2017-03-10 19:08
浏览 109

Laravel,Carbon和Validations

I'm running into an odd validation problem in Laravel 4 & Carbon.

I have a form that has some required general fields at the top (a couple of drop-downs and a text field), then a series of date & time fields that are not required.

I have a validator set up to have the first 3 fields required, and the date/time fields set to 'date_format:Y-m-d H:i' (seconds are not important here).

The "airline_open", "airline_close", "hotel_open", "hotel_close" fields below are the ones causing the problems.

As long as the required fields are filled in, everything works great. If the required fields are not filled in, and there are no date/time fields filled in, also great (I get appropriate messaging). However, if any of the required fields are not filled in, and any of the date/time fields are filled in with the expected format (no seconds), I get an exception thrown by Carbon. (Interestingly, if I include the seconds for the time, it doesn't thrown the exception, but gives the validation error.)

This exception seems to be similar to this problem: Laravel Carbon Data Missing

However, given that everything works as designed as long as the required fields are filled in, it feels like I should be able to make this work without having to mess around with Carbon's date format.

Any ideas?

Adding some code:

Validator:

        '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'
    );

From the form (the airline_open & airline_close fields). The "now" button calls some js to populate the field with the date & time in the desired format:

        <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:

  • 写回答

2条回答 默认 最新

  • dptt66700 2017-03-10 19:13
    关注

    The validation is failing because you can't save such date in DB you need to parse it.

    I recommend first putting the date fields in your model under $dates variable

    protected $dates = ['your_date'];
    

    and then make a setter and 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');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号