douci6541 2014-10-27 14:44
浏览 39
已采纳

在Laravel中验证之前运行任务

Okay, so I have a form where it asks the user for:-

  • The day on which they were born (0-31, etc.)
  • The month in which they were born (January-December)
  • The year in which they were born

I then validate this with:

$rules = array(
    '???'   => 'required:date'
);
$validator = Validator::make($data, $rules);

But I have no idea where/what I can do to put the date, month, year into something that I can put into my validation, whether it be on the view or in the validation process.

Thanks, Tom

  • 写回答

1条回答 默认 最新

  • doumi7861 2014-10-29 16:56
    关注

    If you want to use the linked validation rule your date needs to come in as a single field in a format that Laravel's date parser will be able to recognise (dd-mm-yyyy, dd/mm/yy, yyyy-mm-dd, etc.) - so your form should really contain a single text field called 'date' in this case.

    If it comes in as three separate fields you need to turn them into a single field before they get near the validator. Something like this maybe:

    $rules = [
        'name'  => ['required'],
        'email' => ['required', 'email'],
        'date'  => ['required', 'date'],
    ];
    
    // transform date from three separate fields to yyyy-mm-dd
    // TODO: ensure the things are zero-padded maybe?
    $date = Input::get('year').'-'.Input::get('month').'-'.Input::get('day');
    
    $v = Validator::make(
        Input::only('name', 'email') + ['date' => $date],
        $rules
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!