̽ÌÖÔõÑù×°ÖÃLaravel validator
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp webÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü¡£Ëü¾ßÓÐÇ¿Ê¢µÄÌØÕ÷ºÍ¹¤¾ß£¬¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¿ìËٵع¹½¨ÏîÄ¿¡£ÆäÖÐÒ»¸öÖµµÃÒ»ÌáµÄÌØÕ÷ÊÇvalidator¡£ËüÊÇÒ»¸öÓÃÓÚÑéÖ¤ÊäÈëÊý¾ÝµÄ¹¤¾ß£¬¿ÉÒÔÇáËɵØÔÚlaravelÏîÄ¿ÖÐʹÓá£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖÔõÑù×°ÖÃLaravel validator£¬²¢Õ¹Ê¾ÔõÑùÔÚÏîÄ¿ÖÐʹÓÃËü¡£
×°ÖÃLaravel
Ê×ÏÈ£¬ÄúÐèҪʹÓÃComposer×°ÖÃLaravel¡£ÈôÊÇÄúûÓÐ×°ÖÃComposer£¬Çëƾ֤ÏÂÃæµÄ°ì·¨×°Öá£
ÔÚÖն˻òÏÂÁîÌáÐÑ·ûÖÐÔËÐÐÒÔÏÂÏÂÁ
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');"
µÇ¼ºó¸´ÖÆ
ÕâЩÏÂÁÏÂÔØComposer×°ÖóÌÐò²¢½«ÆäÉúÑÄÔÚÄ¿½ñĿ¼ÖС£ÒªÈ«¾Ö×°ÖÃComposer£¬Ç뽫ÏÂÔصÄÎļþÒƶ¯µ½ÄúÏ£Íû×°ÖÃËüµÄλÖã¬È»ºóͨ¹ýÔËÐÐÒÔÏÂÏÂÁîʹÆäÈ«¾Ö¿ÉÓãº
mv composer.phar /usr/local/bin/composer
µÇ¼ºó¸´ÖÆ
´ËÏÂÁ°ÑComposerÒƶ¯µ½/usr/local/binĿ¼ÖС£
ÏÖÔÚ£¬Äú¿ÉÒÔʹÓÃComposer×°ÖÃLaravel¡£ÔÚÖն˻òÏÂÁîÌáÐÑ·ûÖÐÔËÐÐÒÔÏÂÏÂÁ
composer create-project --prefer-dist laravel/laravel blog
µÇ¼ºó¸´ÖÆ
´ËÏÂÁ½¨ÉèÒ»¸öÃûΪ”blog”µÄÐÂLaravelÏîÄ¿£¬²¢×°ÖÃËùÓбØÐèµÄÒÀÀµÏî¡£
×°ÖÃLaravel validator
×°ÖÃLaravel validatorºÜÊǼòÆÓ¡£Ö»ÐèÔÚÖն˻òÏÂÁîÌáÐÑ·ûÖÐÔËÐÐÒÔÏÂÏÂÁ
composer require illuminate/validation
µÇ¼ºó¸´ÖÆ
´ËÏÂÁʹComposerÏÂÔز¢×°ÖÃLaravel validator¿â¼°ÆäÏà¹ØÒÀÀµÏî¡£
ʹÓÃLaravel validator
ÏÖÔÚ£¬ÄúÒѾÀÖ³É×°ÖÃÁËLaravel validator¡£ÏÂÃæÈÃÎÒÃÇ¿´Ò»ÏÂÔõÑùÔÚLaravelÏîÄ¿ÖÐʹÓÃËü¡£
ÔÚLaravelÏîÄ¿ÖУ¬Äú¿ÉÒÔʹÓÃvalidatorÀàÀ´ÑéÖ¤ÊäÈëÊý¾Ý¡£validatorÀàÌṩÁËÐí¶àÄÚÖÃÑéÖ¤¹æÔò£¬ÀýÈçrequired£¬emailµÈ£¬²¢ÇÒÄú¿ÉÒÔÇáËɵؽ¨Éè×Ô½ç˵¹æÔò¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£¬Õ¹Ê¾ÁËÔõÑùÔÚLaravel¿ØÖÆÆ÷ÖÐʹÓÃvalidatorÀ´ÑéÖ¤Ìá½»µÄ±íµ¥Êý¾Ý£º
public function store(Request $request) { // ÑéÖ¤ÇëÇóÊý¾Ý $validatedData = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); // ÔÚ´ËËæ´¦ÀíÑé֤ͨʺóµÄÂß¼ }
µÇ¼ºó¸´ÖÆ
´ÓÉÏÃæµÄ´úÂë¶Î¿ÉÒÔ¿´³ö£¬validateÒªÁìÎüÊÕ¹æÔòÊý×é×÷ΪÆä²ÎÊý£¬²¢ÇÒÈôÊÇÑé֤ʧ°Ü£¬ÔòÅ׳öÒ»¸öValidationExceptionÒì³£¡£ÈôÊÇÑé֤ͨ¹ý£¬Ëü½«·µ»ØÒ»¸ö°üÀ¨ÒÑÑéÖ¤Êý¾ÝµÄ¹ØÁªÊý×é¡£
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÑéÖ¤ÁËÿ¸ö±íµ¥×ֶεı£´æÐÔºÍ׼ȷÃûÌ㬲¢ÇÒʹÓÃunique¹æÔòÈ·±£emailµØµãÔÚÊý¾Ý¿âÖÐûÓÐÖظ´¡£
½áÂÛ
Laravel validatorÊÇÒ»¸öºÜÊÇÇ¿Ê¢ºÍÓÐÓõŤ¾ß£¬Ëü¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±ÑéÖ¤ÊäÈëÊý¾Ý¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÒѾÕÆÎÕÁËÔõÑù×°ÖÃLaravel validator²¢ÔÚLaravelÏîÄ¿ÖÐʹÓÃËüµÄ»ù´¡ÖªÊ¶¡£ÈôÊÇÄú»¹Ã»ÓÐʵÑé¹ýʹÓÃLaravel validator£¬ÎÒÃãÀøÄúÔÚÏÂÒ»¸öÏîÄ¿ÖÐʹÓÃËü£¬Ëü½«Ê¹ÄúµÄ¿ª·¢ÊÂÇéÔ½·¢ÈÝÒ׺͸ßЧ¡£
ÒÔÉϾÍÊÇ̽ÌÖÔõÑù×°ÖÃLaravel validatorµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡