Laravel 5如何在验证前从Controller中的Boostrap datepicker更改日期格式

时间:2017-06-16 09:36:38

标签: php validation date laravel-5

我想在进行验证之前更改控制器中的日期格式。 以下是我的观点

<input type='text' class="form-control" id='datePicker' value="{{old('dateRegister')}}" name="dateRegister"/>

<script type="text/javascript">
    $('#datePicker').datepicker({
    format: 'DD - dd MM yyyy',       
    })
</script>

我想在选择日期选择器时向客户显示的格式为DD - dd MM yyyy

现在在控制器存储功能中:

public function store(Request $request)
{      
    $rules = array(
        'dateRegister' => 'date_format:Y-m-d|required|unique:events,dateRegister,NULL,id,users_id,'.\Auth::id(),
    );
    $messages = array(
        'dateRegister.unique' => 'You have already register this event in this date'
    );

    $validator = Validator::make(Input::all(), $rules, $messages);
    if($validator->fails()){
        return back()->withInput()->withErrors($validator);
    } 
    elseif ($validator->passes()){
        $input = $request->all();
        // MODIFIED DATE
        $date = str_replace("-", "", $request->dateRegister);
        $input['dateRegister'] = Carbon::parse($date)->format('Y-m-d');
        Auth()->user()->events()->create($input);
    }
    return back();
}

在$ rule中,我需要检查每个用户是否只能拥有唯一的日期寄存器。 由于在数据库中存储日期的格式是Y-m-d,我可以修改日期格式并存储在数据库中。

但我不知道如何在验证之前修改日期格式。我应该在这部分做什么?我不断收到此验证错误:dateRegister与格式Y-m-d不匹配。

1 个答案:

答案 0 :(得分:1)

首先,更好地使用#formRightSide #formSwitch{position:relative; text-align:center; text-transform:uppercase; font-size:.6em; line-height:1.1em;} #formSwitch li{position:relative; display:inline-block; background:red ; color:#999; -webkit-transform: translate3d(0,0,0);float:left;margin:0;padding:0;} #formSwitch li#switchIngr{border-top-left-radius:11px; border-bottom-left-radius:11px;} #formSwitch li#switchHour{border-top-right-radius:11px; border-bottom-right-radius:11px;} #formSwitch li a{display:inline-block; overflow: hidden; line-height: 1; color: #999; text-decoration:none;} #formSwitch li#switchIngr a{padding:.6em .6em .6em 1em; border-top-left-radius:11px; border-bottom-left-radius:11px;} #formSwitch li#switchHour a{padding:.6em 1em .6em .6em; border-top-right-radius:11px; border-bottom-right-radius:11px;} #formSwitch li a::after{content:''; position:absolute; height:100%; width:0%; top:0; z-index:-1; background:#58bb9c; -webkit-transition: width 3s cubic-bezier(0.7, 0.3, 0, 1); transition: width 3s cubic-bezier(0.7, 0.3, 0, 1);} #formSwitch li#switchIngr a::after{right:0; border-top-left-radius:11px; border-bottom-left-radius:11px;} #formSwitch li#switchHour a::after{left:0; border-top-right-radius:11px; border-bottom-right-radius:11px;} #formSwitch li a.selected::after{width: 100%;} #formSwitch li a span {position:relative; display:block;} #formSwitch li a.selected span::before{width:100%;} #formSwitch li a span::before{ content:attr(data-letters); position:absolute; color:#fff; overflow:hidden; white-space:nowrap; width:0%; -webkit-transition: all 3s cubic-bezier(0.7, 0.3, 0, 1); transition: all 3s cubic-bezier(0.7, 0.3, 0, 1); } #formSwitch li a.selected span::before{ display:none; } #formSwitch li a span{ color:#999; -moz-transition:all linear 3s; -webkit-transition:all linear 3s; -ms-transition:all linear 3s; -o-transition:all linear 3s; transition:all linear 3s; } #formSwitch li a.selected span{ color:#fff; -moz-transition:all linear 3s; -webkit-transition:all linear 3s; -ms-transition:all linear 3s; -o-transition:all linear 3s; transition:all linear 3s; } #formSwitch li:last-child a span::before{ left:0; } #formSwitch li:first-child a span::before{ right:0; } 代替$request

要在验证前修改数据,请尝试以下操作:

Input