×ðÁú¿­Ê±

ThinkPHP6ÔõÑù¾ÙÐÐ±íµ¥ÑéÖ¤²Ù×÷ £¿

thinkphp6ÊÇÒ»¿î»ùÓÚphpµÄmvc¿ò¼Ü £¬¼«´óµØ¼ò»¯ÁËwebÓ¦ÓóÌÐòµÄ¿ª·¢¡£ÆäÖÐ±íµ¥ÑéÖ¤ÊÇÒ»¸ö·Ç³£»ù´¡ºÍÖ÷ÒªµÄ¹¦Ð§¡£ÔÚÕâƪÎÄÕÂÖÐ £¬ÎÒÃǽ«ÏÈÈÝthinkphp6ÖÐÔõÑù¾ÙÐÐ±íµ¥ÑéÖ¤²Ù×÷¡£

Ò»¡¢ÑéÖ¤¹æÔò½ç˵

ÔÚThinkPHP6ÖÐ £¬ÑéÖ¤¹æÔò¶¼ÐèÒª½ç˵ÔÚ¿ØÖÆÆ÷ÖÐ £¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚ¿ØÖÆÆ÷Öнç˵һ¸ö$validateÊôÐÔÀ´ÊµÏÖ¹æÔòµÄ½ç˵ £¬ÈçÏÂËùʾ£º

use thinkValidate;
class UserController extends Controller {
    protected $validate;
    public function __construct(Validate $validate) {
        $this->validate = $validate;
    }

    // ½ç˵ÑéÖ¤¹æÔò
    protected $rule = [
        'name' => 'require|max:25',
        'email' => 'email',
        'age' => 'number|between:1,120',
    ];
}

µÇ¼ºó¸´ÖÆ

¶þ¡¢±íµ¥ÑéÖ¤

µ±ÎÒÃǽç˵ÁËÑéÖ¤¹æÔòºó £¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐʹÓÃ$validateµÄcheck()ÒªÁìÖ´ÐÐÎÒÃǽç˵µÄÑéÖ¤¹æÔò¡£Í¨¹ýÔÚ¿ØÖÆÆ÷ÖеÄ$request¹¤¾ßÉÏŲÓÃvalidate()ÒªÁì²¢´«ÈëÑéÖ¤¹æÔò £¬¼´¿ÉʵÏÖÑéÖ¤²Ù×÷¡£

public function add(Request $request) {
    $data = $request->param();
    $result = $this->validate($data, $this->rule);
    if ($result !== true) {
        return ['code' => 1, 'msg' => $result];
    }
    // Ñé֤ͨ¹ý£¬Ö´ÐÐÌí¼Ó²Ù×÷
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖÐ £¬$request->param()ÒªÁì·µ»ØµÄÊÇÒ»¸öÊý×é £¬ÆäÖаüÀ¨ÁË±íµ¥ÖÐËùÓеIJÎÊýÖµ¡£ÎÒÃǽ«Æä´«Èëvalidate()ÒªÁìÖоÙÐÐÑéÖ¤ £¬ÈôÊÇÑé֤ʧ°ÜÔò»á·µ»Ø¹ýʧÐÅÏ¢ £¬ÈôÊÇÑé֤ͨ¹ýÔò»áÖ±½ÓÖ´ÐÐÌí¼Ó²Ù×÷¡£

Èý¡¢×Ô½ç˵¹ýʧÐÂÎÅ

ÔÚÏÖʵ¿ª·¢ÖÐ £¬ÎÒÃÇ¿ÉÄÜÐèÒª¶ÔһЩ¹æÔò¾ÙÐÐ×Ô½ç˵¹ýʧÐÂÎŵÄÉèÖà £¬Õâ¿ÉÒÔͨ¹ýÔÚÑéÖ¤¹æÔòÖÐʹÓãºmessage()ÒªÁìÀ´ÊµÏÖ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

protected $rule = [
    'name' => 'require|max:25',
    'email' => 'email',
    'age' => 'number|between:1,120',
];

protected $message = [
    'name.require' => 'Óû§Ãû±ØÌî',
    'name.max' => 'Óû§Ãû×î¶à²»¿ÉÁè¼Ý25¸ö×Ö·û',
    'email.email' => 'ÓÊÏäÃûÌùýʧ',
    'age.number' => 'ÄêËê±ØÐèÊÇÊý×Ö',
    'age.between' => 'ÄêËê±ØÐèÔÚ1~120Ö®¼ä',
];

µÇ¼ºó¸´ÖÆ

ͨ¹ýÔÚ¿ØÖÆÆ÷ÖÐʹÓÃ$validateµÄmessage()ÒªÁì £¬ÎÒÃǾͿÉÒÔ¶Ô¹æÔòµÄ¹ýʧÐÅÏ¢¾ÙÐÐ×Ô½ç˵ÉèÖá£ÀýÈ磺

$result = $this->validate($data, $this->rule, $this->message);

µÇ¼ºó¸´ÖÆ

ËÄ¡¢ÅúÁ¿ÑéÖ¤

µ±ÎÒÃÇÐèÒªÑéÖ¤¶à¸ö±íµ¥Ê± £¬¿ÉÒÔʹÓÃ$validateµÄbatch()ÒªÁìÀ´¾ÙÐÐÅúÁ¿ÑéÖ¤¡£ÀýÈ磺

public function verify(Request $request) {
    $data = $request->param();
    $rule = [
        'name' => 'require|max:25',
        'email' => 'email',
        'age' => 'number|between:1,120',
    ];
    $message = [
        'name.require' => 'Óû§Ãû±ØÌî',
        'name.max' => 'Óû§Ãû×î¶à²»¿ÉÁè¼Ý25¸ö×Ö·û',
        'email.email' => 'ÓÊÏäÃûÌùýʧ',
        'age.number' => 'ÄêËê±ØÐèÊÇÊý×Ö',
        'age.between' => 'ÄêËê±ØÐèÔÚ1~120Ö®¼ä',
    ];
    $result = $this->validate($data, $rule, $message, true);
    if ($result !== true) {
        return ['code' => 1, 'msg' => $result];
    }
    // Ñé֤ͨ¹ý£¬Ö´ÐÐÏà¹Ø²Ù×÷
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖÐ £¬$validateÒªÁìµÄµÚËĸö²ÎÊýΪtrue £¬ÌåÏÖ¿ªÆôÅúÁ¿ÑéÖ¤¡ £¿ªÆôÅúÁ¿ÑéÖ¤ºó £¬¿ÉÒÔÉèÖöà¸ö±íµ¥ÑéÖ¤¹æÔò £¬ÔÚÑé֤ʧ°Üʱ·µ»ØËùÓйýʧÐÅÏ¢¡£

×ܽ᣺

ÔÚThinkPHP6ÖÐ £¬Í¨¹ýÔÚ¿ØÖÆÆ÷Öнç˵$validateÊôÐÔÀ´½ç˵ÑéÖ¤¹æÔò £¬Í¨¹ýÔÚ$request¹¤¾ßÉÏŲÓÃvalidate()ÒªÁì¾ÙÐÐ±íµ¥ÑéÖ¤ £¬²¢¿ÉÒÔͨ¹ýmessage()ÒªÁì¾ÙÐÐ×Ô½ç˵¹ýʧÐÂÎŵÄÉèÖá£Í¬Ê± £¬Í¨¹ýbatch()ÒªÁì¿ÉÒÔʵÏÖÅúÁ¿ÑéÖ¤¡£ÕâЩ²Ù×÷¶¼ÊǷdz£»ù´¡ºÍ³£ÓõÄ £¬ÎÒÃÇÔÚÏÖʵµÄ¿ª·¢ÖÐÒ²ÒªÊìÁ·ÕÆÎÕ¡£

ÒÔÉϾÍÊÇThinkPHP6ÔõÑù¾ÙÐÐ±íµ¥ÑéÖ¤²Ù×÷ £¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿