这是我的请求类。
public function rules()
{
$input = Input::all();
return [
'title' => 'required|unique:event_cals,title',
'eventDate' => 'required|date|after:yesterday',
'endDate' => 'date|before:event_cals,' . date( 'Y-m-d', strtotime( $input['eventDate'] . ' +1 day' ) ),
'venue' => 'required',
'time' => 'required',
'type' => 'required',
'photo' => 'mimes:jpeg,bmp,png,jpg|max:1000'
];
}
public function messages(){
return [
'title.required' => 'Title is required.',
'title.unique' => 'Title is already there.',
'eventDate.after' => 'Event Date is passed.',
'eventDate.required' => 'Event Date is required.',
'venue.required' => 'Venue is required.',
'time.required' => 'Time is required.',
'type.required' => 'Type is required.',
'endDate.date' => 'Due Date should be after starting date',
];
}
我想验证endDate是否在eventDate之后。除非endDate类似于eventDate,否则它正常工作。但我想确保必须能够添加类似于eventDate的endDate。例如,如果eventDate是03/04/2016,则可以将endDate添加为03/04/2016或任何即将到来的日期。但是从上面的规则03/04/2016作为endDate是无效的。之前或之前有关键字吗?请任何人帮我弄清楚这个烂摊子吗?
答案 0 :(得分:0)
假设您在建立$rules
之前收到了输入,您可以在验证前尝试将一天添加到eventDate
:
$rules = [
'eventDate' => 'required|date|after:yesterday',
'endDate' => 'date|before:event_cals,' . date( 'Y-m-d', strtotime( $input['eventDate'] . ' +1 day' ) ),
];
或者您可以扩展Laravel以创建一个新的验证规则来为您处理此问题(并使其更容易在其他控制器中重用)。