thinkphp5Ä£°åÔõôÅжÏÊÇ·ñÊÇÕýÊý
ÔÚthinkphp5¿ò¼ÜÖУ¬ÅжÏÒ»¸öÊýÊÇ·ñÊÇÕýÊý¿ÉÒÔͨ¹ýʹÓÃtpÄÚÖõÄÑéÖ¤Æ÷À´¾ÙÐÐÅжϡ£ÑéÖ¤Æ÷¿ÉÒÔͨ¹ýÀàµÄ·½·¨ÊµÏÖ£¬Ò²¿ÉÒÔͨ¹ýÄÚÖõĺ¯ÊýÑéÖ¤À´ÊµÏÖ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃTPÄÚÖú¯ÊýÑéÖ¤ÒªÁìÀ´ÑéÖ¤ÊýÖµÊÇ·ñΪÕýÊý¡£
ÔõÑù½ç˵ÑéÖ¤¹æÔò£¿
TP5ÌṩÁËÄÚÖú¯ÊýÑéÖ¤µÄÒªÁìÀ´ÑéÖ¤Êý¾Ý£¬°üÀ¨ÕûÊý¡¢¸¡µãÊý¡¢ÈÕÆڵȵȣ¬¶¼¿ÉÒÔͨ¹ýÄÚÖú¯ÊýÀ´ÑéÖ¤¡£ÔÚ½ç˵ÑéÖ¤¹æÔòʱ£¬ÐèҪʹÓõ½ÑéÖ¤Æ÷ÀàÖеÄrule()ÒªÁì¡£ÎÒÃǽç˵ÑéÖ¤¹æÔòµÄ´úÂëÀàËÆÓÚÏÂÃæÕâÑù£º
$validate = new \think\Validate([ 'num' => 'require|number|gt:0', ]);
µÇ¼ºó¸´ÖÆ
´úÂëÖÐÎÒÃǽç˵ÁËnumÕâ¸ö×ֶεÄÑéÖ¤¹æÔò£¬¸Ã×Ö¶ÎÐèÒªÖª×ãÒÔÏÂÈý¸öÏÞÖÆ£º
±ØÌrequire£©
Êý×ÖÀàÐÍ£¨number£©
´óÓÚ0£¨gt:0£©
Õâ¸öÀý×ÓÑÝʾÁËÔõÑù½ç˵ÑéÖ¤¹æÔòÀ´°ü¹ÜÎÒÃÇÊäÈëµÄÊýÖµÊÇÒ»¸öÕýÊý¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔõôÑéÖ¤ÊýÖµÊÇ·ñΪÕýÊý£¿
ÑéÖ¤ÊýÖµÊÇ·ñΪÕýÊýµÄÒªÁìÊÇʹÓÃvalidate()º¯Êý¡£ÔÚÕâ¸öº¯ÊýÖд«ÈëÒªÑéÖ¤µÄÊýÖµºÍÑéÖ¤¹æÔò¼´¿ÉÍê³ÉÑéÖ¤£¬validateº¯Êý½«·µ»ØÒ»¸ö²¼¶ûÖµ£¬Åú×¢ÑéÖ¤ÊÇ·ñͨ¹ý¡£
Ç°ÃæÒѾ½ç˵Á˹æÔò£¬ÏÖÔÚÎÒÃÇÀ´ÑéÖ¤ÊýÖµÊÇ·ñΪÕýÊý°É£º
$result = $validate->check([ 'num' => 10.2 ]);
µÇ¼ºó¸´ÖÆ
ÑéÖ¤º¯Êý·µ»Øtrue£¬µ±ÑéÖ¤º¯Êý·µ»ØֵΪtrueʱÌåÏÖÑé֤ͨ¹ý£¬²»È»ÌåÏÖÑé֤Ƿºà¹ý¡£
ʹÓÃvalidate()º¯ÊýÀ´ÑéÖ¤Ö¸¶¨µÄ±íµ¥ÖеÄnum×ֶεÄÊýÖµÊÇ·ñΪÕýÊý¡£
ÔõÑù°ÑÕâ¸öº¯Êý·â×°ÆðÀ´ÒÔ±ã¶à´ÎʹÓã¿
ͨ¹ýº¯Êý·â×°£¬ÎÒÃÇ¿ÉÒÔÔ½·¢Àû±ãµÄʹÓÃÒ»¸ö¹²ÓõÄÑéÖ¤¹æÔò£¬Ò²×èÖ¹ÁËÔÚ²î±ðµÄµØ·½ÖØ»ØÐÅдÏàͬµÄÑéÖ¤´úÂë¡£ÏÖÔÚÎÒÃÇ·â×°Ò»¸öº¯Êý£¬ÃüÃûΪ¡°isPositiveNumber¡±£¬ÓÃÀ´ÑéÖ¤ÊäÈëµÄÊýÖµÊÇ·ñΪÕýÊý¡£
/** * ÑéÖ¤ÊýÖµÊÇ·ñΪÕýÊý * @param float $value ÐèÒªÑéÖ¤µÄÖµ * @return bool Ñé֤Ч¹û */ function isPositiveNumber($value): bool{ //½ç˵ÑéÖ¤¹æÔò $validate = new \think\Validate([ 'num' => 'require|number|gt:0', ]); //Ö´ÐÐÑéÖ¤ return $validate->check([ 'num' => $value ]); }
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÖÐÎÒÃǽç˵Á˺¯ÊýisPositiveNumber£¬¸Ãº¯ÊýÎüÊÕÒ»¸öÊýÖµ×÷Ϊ²ÎÊý£¬½«¸ÃÊýÖµ×÷Ϊnum×Ö¶Îת´ï¸øÑéÖ¤Æ÷¾ÙÐÐÑéÖ¤£¬×îºó·µ»ØÑé֤Ч¹û¡£
ͨ¹ýÕâ¶Î·â×°µÄ´úÂ룬ÎÒÃÇ¿ÉÒÔÔÚÐèÒªÑéÖ¤ÕýÊýµÄµØ·½ÒýÓÃÕâ¸öº¯Êý£¬À´ÅжÏÊäÈëµÄÊýÖµÊÇ·ñΪÕýÊý¡£
½áÂÛ
ͨ¹ýÉÏÊöµÄÏÈÈÝ£¬ÎÒÃÇѧ»áÁËÔõÑùÔÚThinkPHP5ÖÐʹÓÃÄÚÖÃÑéÖ¤Æ÷µÄº¯ÊýÑéÖ¤ÒªÁìÀ´ÑéÖ¤ÊäÈëµÄÊýÖµÊÇ·ñΪÕýÊý¡£ÈôÊÇÐèÒªÔÚÏîÄ¿ÖÐʹÓöà´ÎÑéÖ¤ÕýÊýµÄ¹¦Ð§£¬¿ÉÒÔͨ¹ýº¯Êý·â×°µÄ·½·¨À´ÊµÏÖ£¬Ìá¸ß´úÂëµÄÖØÓÃÐÔºÍά»¤ÐÔ¡£
ÒÔÉϾÍÊÇthinkphp5Ä£°åÔõôÅжÏÊÇ·ñÊÇÕýÊýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡