Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel ValidationÑéÖ¤±íµ¥ÇëÇó£¿
laravelÊÇÒ»¸öÊ¢ÐеÄphp web¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§À´¼ÓËÙ¿ª·¢ÕßµÄÊÂÇé¡£ÆäÖУ¬laravel validationÊÇÒ»ÖÖºÜÊÇÊÊÓõĹ¦Ð§£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÑéÖ¤±íµ¥ÇëÇóºÍÓû§ÊäÈëµÄÊý¾Ý¡£±¾Îľͽ«ÏÈÈÝÔõÑùʹÓÃlaravel validationÑéÖ¤±íµ¥ÇëÇó¡£
ʲôÊÇLaravel Validation
Laravel ValidationÊÇLaravel¿ò¼ÜÄÚÖõÄÒ»ÖÖ±íµ¥ÑéÖ¤·½·¨¡£ËüÌṩÁËÒ»ÖÖ¾«Á·¹æ·¶¡¢Ò×ÓÚʹÓõķ½·¨À´ÑéÖ¤±íµ¥ÇëÇóºÍÓû§ÊäÈëÊý¾Ý¡£Äã¿ÉÒÔʹÓÃÖÖÖÖ¹æÔòÑéÖ¤ÖÖÖÖÊý¾ÝÀàÐÍ£¬ïÔÌÁË´úÂëÈßÓ࣬¼ò»¯ÁË´úÂ뿪·¢µÄÁ÷³Ì¡£
ÔõÑùʹÓÃLaravel Validation
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ¿ØÖÆÆ÷ÄÚuse LaravelµÄValidationÀࣺ
use IlluminateSupportFacadesValidator;
µÇ¼ºó¸´ÖÆ
µ±ÎÒÃÇÐèÒªÑéÖ¤Ò»¸öÇëÇóʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃValidator::make()ÒªÁ죺
public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|min:3|max:255', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8|confirmed', ]); if ($validator->fails()) { return redirect('register') ->withErrors($validator) ->withInput(); } // ÔÚ´Ë´¦Ð´ÈëÊý¾Ýµ½Êý¾Ý¿âµÄÂß¼ }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËValidator::make()ÒªÁìÀ´½ç˵ÑéÖ¤¹æÔò¡£×ª´ï¸ømakeÒªÁìµÄµÚÒ»¸ö²ÎÊýÊÇÐèÒªÑéÖ¤µÄÇëÇóÊý¾Ý¡£µÚ¶þ¸ö²ÎÊýÊǽç˵ÑéÖ¤¹æÔòµÄÊý×飬Êý×éÄڵļüÊÇÐèÒªÑéÖ¤µÄ×Ö¶ÎÃû³Æ£¬ÖµÊÇ×Ö·û´®ÐÎʽµÄÑéÖ¤¹æÔò¡£
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÈý¸ö×ֶΣº
name£º±ØÐè¡¢×îС×Ö·ûÊýΪ3¡¢×î´ó×Ö·ûÊýΪ255
email£º±ØÐè¡¢ÓÊÏäÃûÌá¢Î¨Ò»ÐÔÑéÖ¤
password£º±ØÐè¡¢×îС×Ö·ûÊýΪ8¡¢ºÍpassword_confirmation×Ö¶ÎÆ¥Åä
ÈôÊÇÓÐÈκÎÒ»¸ö¹æÔòÑé֤ʧ°Ü£¬ÎÒÃǻὫ¹ýʧÐÅÏ¢·µ»Ø¸ø×¢ÊéÒ³Ã棬²¢Ê¹ÓÃwithInput()ÒªÁì·µ»ØÓû§Ö®Ìõ¼þ½»µÄÊý¾Ý¡£
³£ÓõÄÑéÖ¤¹æÔò
ʹÓÃLaravel Validationʱ£¬ÓÐÐí¶à¹æÔò¿É¹©Ê¹Óá£ÏÂÃæÁгöÁËһЩ³£ÓõĹæÔò£º
required£º¸Ã×ֶαØÐèÌîд
email£º¸Ã×ֶαØÐèÇкϵç×ÓÓʼþµØµã¹æ·¶
unique:table_name,field_name£º¸Ã×ֶεÄÖµÔÚÖ¸¶¨µÄÊý¾Ý±íÄÚ±ØÐèΨһ
min:num£º¸Ã×Ö¶Î×îСµÄ³¤¶È±ØÐèΪnum¸ö×Ö·û
max:num£º¸Ã×Ö¶Î×î´óµÄ³¤¶È±ØÐèΪnum¸ö×Ö·û
numeric£º¸Ã×ֶαØÐèΪÊý×Ö
integer£º¸Ã×ֶαØÐèΪÕûÊý
alpha£º¸Ã×ֶαØÐèΪµ¥´Ê×Ö·û£¨a-z¡¢A-Z£©
alpha_num£º¸Ã×ֶαØÐèΪ×Öĸ¡¢Êý×Ö×Ö·û
confirmed£º¸Ã×ֶαØÐèÓëÃûΪfield_name_confirmationµÄ×Ö¶ÎֵƥÅä
ÕâЩ¹æÔòÖ»ÊÇÑéÖ¤¹æÔòµÄÒ»²¿·Ö£¬Ä㻹¿ÉÒÔʹÓøü¶àµÄ¹æÔòÀ´ÑéÖ¤Êý¾Ý¡£Äã¿ÉÒÔÔÚLaravelµÄÎĵµÖÐÕÒµ½¸üÏêϸµÄÑéÖ¤¹æÔòÁÐ±í¡£
×Ô½ç˵ÑéÖ¤¹æÔò
ÓÐʱ¼ä£¬ÎÒÃÇÐèÒª×Ô½ç˵¹æÔòÀ´ÑéÖ¤Êý¾Ý¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃValidator::extend()ÒªÁìʵÏÖ×Ô½ç˵ÑéÖ¤¹æÔò¡£ÏÂÃæÊÇÒ»¸ö×Ô½ç˵¹æÔòµÄÀý×Ó£º
Validator::extend('phone_number', function ($attribute, $value, $parameters, $validator) { return preg_match('/^1[3-9][0-9]{9}$/', $value); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö×Ô½ç˵¹æÔò¡°phone_number¡±£¬Ëü»áÑéÖ¤$formData[‘phone_number’]ÊÇ·ñÇкÏÄã×Ô¼º½ç˵µÄ¹æ·¶¡£ÈôÊÇÑéÖ¤Àֳɣ¬·µ»Øtrue£¬²»È»·µ»Øfalse¡£
×Ô½ç˵¹ýʧÐÅÏ¢
ÎÒÃÇ»¹¿ÉÒÔ×Ô½ç˵¹ýʧÐÅÏ¢£¬À´Ê¹¹ýʧÐÅÏ¢Ô½·¢ÈËÐÔ»¯¡£ÏÂÃæÊÇÒ»¸ö×Ô½ç˵¹ýʧÐÅÏ¢µÄÀý×Ó£º
$validator = Validator::make($request->all(), [ 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8|confirmed', ]); // ×Ô½ç˵¹ýʧÐÅÏ¢ $validator->setAttributeNames([ 'email' => 'ÓÊÏä', 'password' => 'ÃÜÂë', ]); // ×Ô½ç˵¹ýʧÐÅϢģ°å $validator->setCustomMessages([ 'required' => ':attribute×ֶβ»¿ÉΪ¿Õ', 'email' => 'ÇëÊäÈë׼ȷµÄÓÊÏäµØµã', 'unique' => ':attributeÒѾ±»Õ¼ÓÃÁË', 'min' => ':attribute³¤¶È²»¿ÉСÓÚ:min¸ö×Ö·û', 'confirmed' => 'ÃÜÂëºÍÈ·ÈÏÃÜÂ벻ƥÅä', ]);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËsetAttributeNames()ÒªÁìÀ´¸ü¸Ä×Ö¶ÎÃû³Æ£¬ÒÔʹ¹ýʧÐÅÏ¢¸ü¾ßÓÐÈËÐÔ»¯¡£ÎÒÃÇ»¹Ê¹ÓÃÁËsetCustomMessages()ÒªÁìÀ´½ç˵¹ýʧÐÅϢģ°å£¬ÒÔ±ã¸ü¼òÆÓ¿ìËٵظü¸Ä¹ýʧÐÅÏ¢£¬²¢Ê¹ÆäÔ½·¢ÈËÐÔ»¯¡£
×ܽá
ͨ¹ýÉÏÊöµÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃLaravel ValidationÑéÖ¤±íµ¥ÇëÇó¡£ÕâÑù¿ÉÒÔ×èÖ¹ÓÉÓÚÓû§µÄÊäÈ벻׼ȷÔì³ÉµÄ¹ýʧ£¬²¢ÇÒ¿ÉÒÔ¸üÀû±ãµØ´¦Öóͷ£¹ýʧÐÅÏ¢£¬½ÚÔ¼¿ª·¢Ê±¼ä¡£Laravel ValidationÄܹ»¸üºÃµØ°ü¹ÜÓ¦ÓóÌÐòµÄ³°ôÐÔºÍÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel ValidationÑéÖ¤±íµ¥ÇëÇ󣿵ÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡