尊龙凯时人生就是搏

laravel×¢²á¹ýʧ´¦Àí

×î½üÔÚʹÓà laravel ¿ò¼Ü¾ÙÐпª·¢Ê±£¬Óöµ½ÁËһЩ¹ØÓÚ×¢²á±íµ¥µÄ¹ýʧ´¦ÀíÎÊÌâ¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«·ÖÏíÎÒÔõÑùʹÓà laravel µÄ±íµ¥ÑéÖ¤ºÍ¹ýʧ´¦Àí¹¦Ð§À´½â¾öÕâЩ¹ýʧ£¬ÒÔ×ÊÖúÆäËû¿ª·¢Õ߸üºÃµØ´¦Àí×¢²á±íµ¥µÄ¹ýʧ¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶ Laravel ÖÐµÄ±íµ¥ÑéÖ¤ºÍ¹ýʧ´¦Àí»úÖÆ¡£ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Validator ÀàÀ´ÑéÖ¤±íµ¥Êý¾Ý¡£ÑéÖ¤Æ÷¿ÉÒÔÑéÖ¤ÊäÈëÊý¾ÝÊÇ·ñÖª×ã»®¶¨µÄÌõ¼þ£¬Èç±ØÌî×ֶΡ¢ÓÊÏäÃûÌá¢ÃÜÂ볤¶ÈµÈµÈ¡£ÈôÊÇÊäÈëÊý¾Ý²»Öª×ã»®¶¨µÄÌõ¼þ£¬ÔòÑéÖ¤Æ÷»á·µ»ØÒ»¸ö¹ýʧÊý×é¡£ÎÒÃÇ¿ÉÒÔʹÓà withErrors() ÒªÁ콫¹ýʧÐÅÏ¢´æ´¢µ½ Session ÖУ¬È»ºóÔÚÊÓͼÖÐÏÔʾ¹ýʧÐÅÏ¢¡£½ÓÏÂÀ´£¬ÈÃÎÒÃÇ¿´¿´ÔõÑùÔÚ Laravel ÖÐʵÏÖÕâ¸ö¹¦Ð§¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ×¢²á¿ØÖÆÆ÷Öн¨ÉèÒ»¸öÑéÖ¤Æ÷¡£ÎÒÃÇ¿ÉÒÔʹÓà Artisan ÏÂÁî make:validator À´½¨ÉèÑéÖ¤Æ÷¡£ÀýÈ磺

php artisan make:validator RegisterValidator

µÇ¼ºó¸´ÖÆ

Õâ»áÔÚ app/Validators Ŀ¼Ï½¨ÉèÒ»¸öÃûΪ RegisterValidator µÄÑéÖ¤Æ÷¡£È»ºóÎÒÃÇÐèÒªÉèÖÃÑéÖ¤¹æÔòºÍ¹ýʧÐÅÏ¢¡£·­¿ª RegisterValidator À࣬Ìí¼ÓÒÔÏ´úÂ룺

<?php

namespace AppValidators;

use IlluminateValidationValidator;

class RegisterValidator extends Validator
{
    protected $messages = [
        'name.required' => 'ÇëÊäÈëÓû§Ãû¡£',
        'name.max' => 'Óû§Ãû²»¿ÉÁè¼Ý255¸ö×Ö·û¡£',
        'email.required' => 'ÇëÊäÈëÓÊÏäµØµã¡£',
        'email.email' => 'ÇëÊäÈëÓÐÓõÄÓÊÏäµØµã¡£',
        'email.unique' => '¸ÃÓÊÏäÒѾ­±»×¢²á¡£',
        'password.required' => 'ÇëÊäÈëÃÜÂë¡£',
        'password.min' => 'ÃÜÂ볤¶È²»¿ÉСÓÚ6¸ö×Ö·û¡£',
        'password_confirmation.required' => 'ÇëÊäÈëÈ·ÈÏÃÜÂë¡£',
        'password_confirmation.same' => 'Á½´ÎÊäÈëµÄÃÜÂë·×ÆçÖ¡£',
    ];

    public function validateConfirmPassword($attribute, $value, $parameters)
    {
        $other = $this->getValue($parameters[0]);
        return isset($other) && strcmp($value, $other) === 0;
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁËһЩ³£¼ûµÄÑéÖ¤¹æÔòºÍ¹ýʧÐÅÏ¢¡£ÈôÊÇÓû§Ã»ÓÐÊäÈë±ØÌîÏî»òÕßÊäÈëÃûÌò»×¼È·£¬½«»áÏÔʾÏìÓ¦µÄ¹ýʧÐÅÏ¢¡£±ðµÄ£¬ÎÒÃÇ»¹½ç˵ÁËÒ»¸ö×Ô½ç˵ÑéÖ¤¹æÔò validateConfirmPassword À´ÑéÖ¤Á½´ÎÊäÈëµÄÃÜÂëÊÇ·ñÒ»Ö¡£½ÓÏÂÀ´£¬ÔÚ×¢²á¿ØÖÆÆ÷ÖÐÌí¼ÓÒÔÏ´úÂ룺

<?php

namespace AppHttpControllers;

use AppUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
use AppValidatorsRegisterValidator;

class RegisterController extends Controller
{
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        // ÑéÖ¤±íµ¥Êý¾Ý
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);

        // ʹÓÃ×Ô½ç˵µÄ validator ÀàµÄ¹æÔòÑéÖ¤Êý¾Ý
        $validator->setValidator(new RegisterValidator($validator->getTranslator()));

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        // ½¨ÉèÓû§
        $user = User::create([
            'name' => $request->input('name'),
            'email' => $request->input('email'),
            'password' => bcrypt($request->input('password')),
        ]);

        // µÇÈÎÃü»§
        auth()->login($user);

        // Ìøתµ½Ê×Ò³
        return redirect()->intended('/');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓà Laravel ÄÚÖÃµÄ Validator ÀàÀ´ÑéÖ¤±íµ¥Êý¾Ý¡£È»ºóÎÒÃÇŲÓà setValidator() ÒªÁìÀ´Ó¦ÓÃÎÒÃǽ¨ÉèµÄ×Ô½ç˵ validator ÀàµÄ¹æÔòÑéÖ¤Êý¾Ý¡£ÈôÊÇÓйýʧ£¬ÎÒÃǽ«¹ýʧÐÅÏ¢´æ´¢µ½ Session Öв¢½«Óû§Öض¨Ïò»Ø±íµ¥Ò³Ãæ¡£ÈôÊÇÑé֤ͨ¹ý£¬ÎÒÃǽ¨ÉèÓû§²¢½«ÆäµÇ¼¡£×îºó£¬ÎÒÃǽ«Óû§Öض¨Ïòµ½Ö÷Ò³¡£

ÔÚÄ£°åÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÏÔʾ¹ýʧÐÅÏ¢£º

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂ뽫±éÀú $errors ±äÁ¿ÖеÄËùÓйýʧÐÅÏ¢£¬²¢½«Ã¿¸ö¹ýʧÐÅÏ¢×÷Ϊһ¸öÁбíÏî¾ÙÐÐÏÔʾ¡£

ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇѧϰÁËÔõÑùÔÚ Laravel ÖÐʹÓÃ±íµ¥ÑéÖ¤ºÍ¹ýʧ´¦Àí¹¦Ð§À´´¦Àí×¢²á±íµ¥µÄ¹ýʧ¡£Í¨¹ýʹÓÃ×Ô½ç˵ validator ÀàÀ´À©Õ¹ Laravel ÄÚÖÃµÄ validator ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔÇáËɵØ×Ô½ç˵¹æÔòºÍ¹ýʧÐÅÏ¢£¬Ìá¸ß´úÂëµÄ¿ÉÖØÓÃÐԺͿÉά»¤ÐÔ¡£Ï£ÍûÕâƪÎÄÕÂÄܹ»×ÊÖúµ½Äã¸üºÃµØ´¦Àí±íµ¥ÑéÖ¤ºÍ¹ýʧ´¦Àí¡£

ÒÔÉϾÍÊÇlaravel×¢²á¹ýʧ´¦ÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

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