laravelÖÐÑéÖ¤ÒªÁì
laravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ò¼Ü£¬¾ßÓмòÆÓÒ×ÓÃÐÔºÍÇ¿Ê¢µÄ¹¦Ð§¡£ÆäÖÐÒ»¸öÖ÷ÒªµÄ¹¦Ð§ÊÇÑéÖ¤£¬laravelÌṩÁ˶à¸öÑéÖ¤ÒªÁ죬¿ÉÒÔÇáËɵØÑéÖ¤±íµ¥Ìá½»µÄÊý¾Ý£¬°ü¹ÜÓ¦ÓóÌÐòÊý¾ÝµÄ׼ȷÐÔºÍÇå¾²ÐÔ¡£±¾ÎĽ«ÏÈÈÝlaravelÖг£ÓõÄÑéÖ¤ÒªÁì¡£
±íµ¥ÑéÖ¤
±íµ¥ÑéÖ¤ÊÇLaravel×î³£ÓõÄÑéÖ¤ÒªÁ죬ËüÓÃÓÚÑéÖ¤±íµ¥Ìá½»µÄÊý¾ÝÊÇ·ñÇкÏÖ¸¶¨µÄ¹æÔò¡£Ã¿¸öÑéÖ¤¹æÔò¶¼¿ÉÒÔ½ç˵һ¸ö¹ýʧÐÅÏ¢£¬ÔÚÑé֤ʧ°ÜʱÊä³ö¡£ÒÔÏÂÊÇÒ»¸ö±íµ¥ÑéÖ¤µÄʾÀý£º
public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); // Êý¾Ýͨ¹ýÑéÖ¤£¬½«Êý¾Ý´æ´¢µ½Êý¾Ý¿âÖÐ }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬$request->validate([])ÒªÁìÓÃÓÚÑéÖ¤ÇëÇóÖеÄÊý¾Ý¡£¸ÃÒªÁì½ÓÊÜÒ»¸ö°üÀ¨ÑéÖ¤¹æÔòµÄÊý×飬Êý×éµÄ¼üÃûÌåÏÖÒªÑéÖ¤µÄ×Ö¶ÎÃû³Æ£¬¶ø¼üÖµÌåÏÖ¸Ã×ֶεÄÑéÖ¤¹æÔò¡£
ÿ¸öÑéÖ¤¹æÔò¶¼ÓÉÒ»¸ö¹ÜµÀ·û|ÍÑÀ룬ÆäÖаüÀ¨Á˸ùæÔòµÄÑéÖ¤²ÎÊý¡£ÀýÈ磬required|string|max:255ÌåÏÖ×Ö¶ÎΪ±ØÌîÏî¡¢×Ö·û´®ÀàÐÍ£¬²¢ÇÒ×î´ó³¤¶ÈΪ255¸ö×Ö·û¡£unique:usersÌåÏÖÑéÖ¤¸Ã×Ö¶ÎÔÚusers±í¸ñÖеÄΨһÐÔ¡£
ÈôÊÇÑé֤ʧ°Ü£¬½«»á×Ô¶¯·µ»ØÒ»¸ö¹ýʧÐÅÏ¢¡£ÈôÊÇÑéÖ¤Àֳɣ¬½«»á¼ÌÐøÖ´ÐÐÏÂÃæµÄ´úÂëÂß¼¡£
JSONÑéÖ¤
Laravel»¹ÌṩÁËJSONÊý¾ÝÑéÖ¤ÒªÁì¡£JSONÑé֤ͨ³£ÓÃÓÚAPI½Ó¿Ú¿ª·¢£¬ÓÃÓÚÑéÖ¤·¢Ë͵½Ð§ÀÍÆ÷µÄJSONÊý¾ÝÊÇ·ñÇкÏÖ¸¶¨¹æÔò¡£Óë±íµ¥ÑéÖ¤ÏàËÆ£¬JSONÑéÖ¤Ò²¿ÉÒÔ½ç˵ÑéÖ¤¹æÔòºÍ¹ýʧÐÅÏ¢¡£
ÒÔÏÂÊÇÒ»¸öJSONÑéÖ¤µÄʾÀý£º
public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 422); } // Êý¾Ýͨ¹ýÑéÖ¤£¬½«Êý¾Ý´æ´¢µ½Êý¾Ý¿âÖÐ }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬Ê¹ÓÃValidator::make()ÒªÁ콨ÉèÒ»¸öÑé֤ʵÀý¡£Óë±íµ¥ÑéÖ¤²î±ð£¬make()ÒªÁìÐèҪת´ïÁ½¸ö²ÎÊý£ºÑéÖ¤µÄÊý¾ÝºÍÑéÖ¤¹æÔò¡£ÈôÊÇÑé֤ʧ°Ü£¬ÔòÐèÒª·µ»ØÒ»¸ö°üÀ¨¹ýʧÐÅÏ¢µÄJSONÏìÓ¦¡£ÈôÊÇÑéÖ¤Àֳɣ¬½«»á¼ÌÐøÖ´ÐÐÏÂÃæµÄ´úÂëÂß¼¡£
×Ô½ç˵ÑéÖ¤¹æÔò
³ýÁËLaravelÄÚÖõÄÑéÖ¤¹æÔòÍ⣬¿ª·¢Ö°Ô±»¹¿ÉÒÔ×Ô½ç˵ÑéÖ¤¹æÔò¡£×Ô½ç˵¹æÔò¿ÉÒÔÖª×ãÌØÊâµÄÑéÖ¤ÐèÇó£¬ÀýÈçÑé֤ʱ¼ä¡¢ÃÜÂëÇ¿¶ÈµÈ¡£
ÒÔÏÂÊÇÒ»¸ö×Ô½ç˵ÑéÖ¤¹æÔòµÄʾÀý£º
Validator::extend('strong_password', function ($attribute, $value, $parameters, $validator) { return preg_match('/^(?=.*[a-zA-Z])(?=.*d)(?=.*(_|[^w])).+$/', $value); }); $validator = Validator::make($request->all(), [ 'password' => 'required|string|min:8|strong_password', ]);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬Ê¹ÓÃValidator::extend()ÒªÁ콨Éè×Ô½ç˵ÑéÖ¤¹æÔòstrong_password¡£¸ÃÒªÁì½ÓÊÜÁ½¸ö²ÎÊý£º¹æÔòÃû³ÆºÍÒ»¸ö±Õ°üº¯Êý£¬¸Ãº¯Êý½ÓÊÜËĸö²ÎÊý£ºÑéÖ¤×Ö¶ÎÃû³Æ¡¢ÑéÖ¤×Ö¶ÎÖµ¡¢ÑéÖ¤²ÎÊý¡¢ÑéÖ¤Æ÷¹¤¾ß¡£
ÔÚ½ç˵×Ô½ç˵¹æÔòºó£¬ÐèÒªÔÚÑéÖ¤Æ÷ʵÀýÖÐʹÓÃ×Ô½ç˵¹æÔò¡£ÈçÉÏÊö´úÂëËùʾ£¬min:8|strong_passwordÌåÏÖÃÜÂ볤¶ÈÖÁÉÙΪ8¸ö×Ö·û£¬²¢ÇÒ±ØÐè°üÀ¨ÖÁÉÙÒ»¸ö¾Þϸд×Öĸ¡¢Ò»¸öÊý×ÖºÍÒ»¸öÌØÊâ×Ö·û£¨Ï»®Ïß_³ýÍ⣩¡£
¹ýʧÐÅÏ¢×Ô½ç˵
ÔÚ¿ª·¢Ó¦ÓóÌÐòʱ£¬Í¨³£ÐèÒª×Ô½ç˵ÑéÖ¤¹ýʧÐÅÏ¢¡£ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃmessage()ÒªÁì×Ô½ç˵¹ýʧÐÅÏ¢¡£
ÒÔÏÂÊÇÒ»¸ö×Ô½ç˵ÑéÖ¤¹ýʧÐÅÏ¢µÄʾÀý£º
$validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); $validator->message('email.required', 'ÓÊÏä²»¿ÉΪ¿Õ'); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 422); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬Ê¹ÓÃmessage()ÒªÁì×Ô½ç˵¹ýʧÐÅÏ¢¡£¸ÃÒªÁì½ÓÊÜÁ½¸ö²ÎÊý£ºÑéÖ¤¹æÔòÃû³ÆºÍ×Ô½ç˵µÄ¹ýʧÐÅÏ¢¡£
ÒÔÉÏÊÇLaravelÖг£ÓõÄÑéÖ¤ÒªÁì¡£ÔÚ±àдӦÓóÌÐòʱ£¬×¼È·Ê¹ÓÃÑéÖ¤ÒªÁì¿ÉÒÔÓÐÓðü¹ÜÊý¾ÝµÄ׼ȷÐÔºÍÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇlaravelÖÐÑéÖ¤ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡