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
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误