Laravel 5.6出生日期验证

时间:2018-05-28 15:53:28

标签: php laravel

我在我的会员模型中创建了验证规则,不需要出生日期。然而,当有人填写他们的出生日期时,我希望它是 1.格式正确 2.日期不能是将来的日期。

但是,当我填写表格并将出生日期留空时,我仍然会收到以下错误:

enter image description here

这是我的基本Member.php模型:

use Esensi\Model\Model;

class Member extends Model
{
    protected $rules = [
        'name' => 'required|alpha|min:2|max:255',
        'surname' => 'required|alpha|min:2|max:255',
        'id_number' => 'required|unique:members,id_number|digits:13',
        'mobile_number' => 'required|digits:10',
        'email' => 'required|email',
        'date_of_birth' => 'date_format:Y-M-D|before:today',
    ];
}

这是我的MemberController.php存储功能:

public function store(Request $request) 
{
    $member = new Member;
    $member->name = $request->name;
    $member->surname = $request->surname;
    $member->id_number = $request->id_number;
    $member->mobile_number = $request->mobile_number;
    $member->email = $request->email;
    $member->date_of_birth = $request->date_of_birth;

    if(!$member->save()){
        $errors = $member->getErrors();

         return redirect()
            ->action('MemberController@create')
            ->with('errors', $errors)
            ->withInput();
}
//successful creation
    return redirect()
        ->action('MemberController@index')
        ->with('message', '<div class="alert alert-success">Member Created Successfully!</div>');

}

如何修复它,以便在实际填写字段但格式不正确时,出生日期只显示错误?

3 个答案:

答案 0 :(得分:2)

在这里,您可以找到针对不同条件的<section class="content"> <?php $events = TicketData::getEvents(); // $status->status_id; foreach($events as $event){ $thejson[] = array("title"=>$event->title,"url"=>"./?view=editticket&id=".$event->id,"start"=>$event->date_at."T".$event->time_at,); $thejsonColor[] = array($event->status_id); } // print_r(json_encode($thejson)); ?> <script> $(document).ready(function() { $('#calendar').fullCalendar({ header: { left: 'prev,next, today', center: 'title', right: 'month,agendaWeek,agendaDay' }, defaultDate: jQuery.now(), editable: false, eventLimit: true, // allow "more" link when too many events events: <?php echo json_encode($thejson); ?>, if ($thejsonColor=1){ eventColor: 'fb8c00' }else if ($thejsonColor=2){ eventColor: 'ff0' } else if ($thejsonColor=3){ eventColor: '43a047' } else { eventColor: '00acc1' } }); }); </script> 规则:

date

对于您的情况,您应该尝试:

$v = Validator::make(
  ['start_date' => date('30-05-2028')], // input
  [
    'date_of_birth' => ['before:5 years ago'],
    'start_date'    => 'required|date_format:d-m-Y|after:8 years',
    'end_date'      => 'date_format:d-m-Y|after:start_date', 
    ]
);

if ($v->passes())
 dd('Your Date format is correct.');
else
 dd($v->errors());

小心:您必须使用此'date_of_birth' => 'date_format:Y-m-d|before:today|nullable' 格式。不是这个Y-m-d。 Month和Date参数必须为小写。希望它能解决你的问题。

答案 1 :(得分:1)

Callable n_max

答案 2 :(得分:0)

只是多想一想......

我在date_of_birth中添加了nullable,然后得到一个SQL错误,指出date_of_birth不允许为null。我忘了将列设置为允许我的架构中的空值

谢谢,这些建议有帮助