laravel ÇëÇóÑéÖ¤¹æÔò
Ëæ×Å»¥ÁªÍøÓ¦ÓóÌÐòµÄÒ»Ö±Éú³¤£¬ÏÖÔÚÔ½À´Ô½¶àµÄÆóÒµºÍ¿ª·¢Õß×îÏÈÑ¡ÔñʹÓà laravel ¿ò¼Ü¾ÙÐпª·¢¡£ÔÚ¿ª·¢Ó¦ÓóÌÐòʱ£¬Êý¾ÝÊäÈëÑéÖ¤ÊǺÜÊÇÖ÷ÒªµÄÒ»²¿·Ö£¬Ëü¿ÉÒÔ±£»¤Ó¦ÓóÌÐòÃâÊܶñÒâ¹¥»÷ºÍÒâÍâÊäÈë´øÀ´µÄÎÊÌâ¡£ÔÚ laravel ¿ò¼ÜÖУ¬ÇëÇóÑéÖ¤¹æÔòÊÇʵÏÖÊý¾ÝÊäÈëÑéÖ¤µÄÒ»ÖÖ·½·¨¡£
Laravel ÌṩÁËÒ»ÖÖ¼òÆÓÇÒºÜÊÇÇ¿Ê¢µÄÑéÖ¤¹æÔòÓï·¨£¬¿ÉÒÔÇáËɵضÔÊäÈëÊý¾Ý¾ÙÐÐÑéÖ¤¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓà Laravel µÄÇëÇóÑéÖ¤¹æÔòÀ´ÑéÖ¤ÊäÈëÊý¾Ý¡£
»ù±¾ÑéÖ¤¹æÔò
Laravel ¿ò¼ÜÌṩÁËһЩ»ù±¾ÑéÖ¤¹æÔò£¬ÕâЩ¹æÔò¿ÉÒÔÔÚÑéÖ¤Àú³ÌÖÐÓÃÓÚÖÖÖÖÄ¿µÄ¡£ÏÂÃæÊÇһЩ³£ÓõĻù±¾ÑéÖ¤¹æÔò£º
required£º±ØÌî×ֶΡ£
email£º±ØÐèÊÇÓÐÓõĵç×ÓÓʼþµØµã¡£
numeric£º±ØÐèÊÇÊý×Ö¡£
max£º×î´óÖµ¡£
min£º×îСֵ¡£
size£º±ØÐèÓëÖ¸¶¨¾ÞϸƥÅä¡£
in£º±ØÐèÊÇÖ¸¶¨ÖµÖ®Ò»¡£
ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃÑéÖ¤¹æÔòºÜÊǼòÆÓ¡£¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪvalidateInputµÄÒªÁ죬ËüÐèÒªÑéÖ¤Ò»¸öÃûΪ¡°username¡±µÄÊäÈë×ֶΣ¬´úÂë¿ÉÒÔÊÇÕâÑùµÄ£º
$this->validate($request, [ 'username' => 'required|min:3|max:20' ]);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë»áÑéÖ¤¡°username¡±×Ö¶ÎÊÇ·ñ°üÀ¨£¬ÇÒ³¤¶ÈÔÚ3µ½20Ö®¼ä£¬ÈôÊDz»ÇкÏÕâЩ¹æÔò£¬½«Å׳öÒ»¸ö ValidationException Òì³£¡£
×Ô½ç˵ÑéÖ¤¹æÔò
³ýÁË»ù±¾ÑéÖ¤¹æÔòÍ⣬Laravel ¿ò¼Ü»¹ÔÊÐíÄú½¨Éè×Ô¼ºµÄÑéÖ¤¹æÔò¡£ÕâЩ×Ô½ç˵ÑéÖ¤¹æÔò¿ÉÒÔƾ֤ÄúµÄÏêϸÐèÒª¾ÙÐнç˵¡£¼ÙÉèÎÒÃÇÐèÒªÑéÖ¤Ò»¸öÃûΪ¡°password¡±µÄÊäÈë×ֶΣ¬¸Ã×ֶαØÐè°üÀ¨¾Þϸд×Öĸ¡¢Êý×ÖºÍÌØÊâ·ûºÅ£¬³¤¶ÈΪ8µ½20¸ö×Ö·û£¬´úÂë¿ÉÒÔÊÇÕâÑùµÄ£º
µÚÒ»²½£º½ç˵ÑéÖ¤¹æÔò
Validator::extend('my_password', function ($attribute, $value, $parameters, $validator) { return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*(_|[^w])).+$/', $value) && strlen($value) >= 8 && strlen($value) <= 20; });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ¡°my_password¡±µÄÑéÖ¤¹æÔò£¬Ê¹ÓÃÁËÕýÔò±í´ïʽºÍ×Ö·û´®³¤¶ÈÀ´ÑéÖ¤ÃÜÂë¡£ÈôÊÇÊäÈëµÄÃÜÂëÇкÏÒªÇ󣬸ÃÑéÖ¤¹æÔò½«·µ»Ø true¡£
µÚ¶þ²½£ºÊ¹ÓÃ×Ô½ç˵ÑéÖ¤¹æÔò
ʹÓÃ×Ô½ç˵ÑéÖ¤¹æÔòÓëʹÓûù±¾ÑéÖ¤¹æÔòÒ»Ñù¡£ÎÒÃÇÖ»ÐèÒªÔÚÑéÖ¤¹æÔòÊý×éÖÐÌí¼Ó×Ô½ç˵ÑéÖ¤¹æÔò¼´¿É¡£¼ÙÉèÎÒÃÇÐèÒªÔÚ֮ǰµÄÀý×ÓÖÐÌí¼Ó¡°password¡±µÄÑéÖ¤¹æÔò£¬´úÂë¿ÉÒÔÊÇÕâÑùµÄ£º
$this->validate($request, [ 'username' => 'required|min:3|max:20', 'password' => 'required|my_password' ]);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽ«¡°my_password¡±Ìí¼Óµ½ÑéÖ¤¹æÔòÖС£ÈôÊÇÃÜÂëÊäÈëÇкÏ×Ô½ç˵¹æÔò£¬Ôòͨ¹ýÑéÖ¤¡£
¹ýʧÐÂÎŶ¨ÖÆ
ÔÚ Laravel ¿ò¼ÜÖУ¬Äú¿ÉÒÔÇáËɵØΪÑéÖ¤¹ýʧÐÂÎŶ¨ÖÆÃûÌá£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
¼ÙÉèÎÒÃÇÐèÒª¶¨ÖÆÃÜÂëÊäÈë¹ýʧÐÂÎÅ£¬ÎÒÃÇ¿ÉÒÔÕâÑù×ö£º
µÚÒ»²½£º½ç˵¹ýʧÐÂÎÅ
$messages = [ 'my_password' => 'The password must contain upper and lowercase letters, numbers, and special symbols. Its length must be between 8 and 20 characters.' ];
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪ¡°my_password¡±µÄ¹ýʧÐÂÎÅ¡£
µÚ¶þ²½£ºÊ¹Óö¨ÖÆÐÂÎÅ
ʹÓùýʧÐÂÎÅÀàËÆÓÚʹÓÃ×Ô½ç˵ÑéÖ¤¹æÔò¡£Ö»ÐèÒª½«ÐèÒª¶¨ÖÆÐÂÎŵÄÑéÖ¤¹æÔòת´ï¸ø validate ÒªÁìµÄµÚ¶þ¸ö²ÎÊý¼´¿É¡£¼ÙÉèÎÒÃÇÐèҪΪ¡°password¡±×ֶζ¨ÖƹýʧÐÂÎÅ£¬´úÂë¿ÉÒÔÊÇÕâÑùµÄ£º
$this->validate($request, [ 'password' => 'required|my_password' ], $messages);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽ«¹ýʧÐÂÎÅ×÷ΪµÚÈý¸ö²ÎÊýת´ï¸øÁË validate ÒªÁì¡£ÈôÊÇÃÜÂëÊäÈë²»ÇкÏ×Ô½ç˵¹æÔò£¬Ôò Laravel ½«ÏÔʾÎÒÃǽç˵µÄ¹ýʧÐÂÎÅ¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÌÖÂÛÁË Laravel ÇëÇóÑéÖ¤¹æÔò£¬°üÀ¨ÔõÑùʹÓûù±¾ÑéÖ¤¹æÔò¼°×Ô½ç˵ÑéÖ¤¹æÔò£¬ÒÔ¼°ÔõÑù¶¨ÖƹýʧÐÂÎÅ¡£Laravel µÄÑéÖ¤¹æÔòÓï·¨ºÜÊǼòÆÓ£¬µ«È´¹¦Ð§Ç¿Ê¢¡£Ê¹Óà Laravel µÄÇëÇóÑéÖ¤¹æÔò£¬¿ÉÒÔÇáËɵضÔÊäÈëÊý¾Ý¾ÙÐÐÑéÖ¤£¬°ü¹ÜÓ¦ÓóÌÐòµÄÇå¾²¡£
ÒÔÉϾÍÊÇlaravel ÇëÇóÑéÖ¤¹æÔòµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡