php - Laravel 5.0 how to add a custom check before registration -
i working laravel project (laravel 5.0) , came across roadblock. trying make people email domain (let's @gmail.com example) register. have looked through of files registration , can't seem find add check.
the code simple:
if (substr($email,-10,10)!="@gmail.com") { echo "we have issue here"; }
now, preferably, wouldn't echo error, rather include in list of errors such when leave name field blank.
if know how this, please post code , file put said code in.
thanks again!
p.s. using of default authentication built laravel 5.0 registration , login.
first thing need create custom validator there several ways make custom validator in laravel prefer doing so:
firstly create custom validator class, serve place can register custom validators in future, mine located in
app\customs
- createdcustoms
directory doesn't exists.<?php namespace app\customs; use illuminate\validation\validator; class validators extends validator { public function validategoodemail($attribute, $value, $parameters) { return (substr($value,-10,10)!="@gmail.com"); //return boolen - true or false } }
nb: notice keyword in method namevalidate
example validategoodemail, validatesex etc.
updated: next up, create validatorserviceprovider.php in
app\providers
, similar this:<?php namespace app\providers; use illuminate\support\serviceprovider; use validator; use app\customs\validators; class validatorserviceprovider extends serviceprovider { public function boot() { validator::resolver(function($translator, $data, $rules, $messages){ return new validators($translator, $data, $rules, $messages); }); } public function register() { } }
register newly created service provider in
config/app.php
under providers'app\providers\validatorserviceprovider',
to use validator in default laravel registration process, pop open
app\services\registrar.php
, add this
nb: in laravel 5.2 checkout app\http\controllers\auth\authcontroller.php
use app\user; use validator; use illuminate\contracts\auth\registrar registrarcontract; use mail; class registrar implements registrarcontract { public function validator(array $data) { return validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|good_email|email|max:255|unique:users', 'password' => 'required|confirmed|min:6', ]); } }
updated: for quick fix, can skip step 1 - 4 , modify app\services\registrar.php
so:
public function validator(array $data) { \validator::extend('good_email', function($attribute, $value, $parameters){ return (substr($value,-10,10)!="@gmail.com"); }); return \validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|good_email|email|max:255|unique:users', 'password' => 'required|confirmed|min:6', ]); }
one last thing add
good_email
inresources\lang\en\validation.php
'unique' => 'the :attribute has been tak 'url' => 'the :attribute format invalid.', ...... 'good_email' => 'sorry :attribute must end "@gmail.com"', ......
Comments
Post a Comment