lundi 27 février 2017

How can I perform this custom input form validation (if the username\e-mail yet exists in the DB) in Laravel?

I am pretty new in Laravel and I have the following doubt about how to implement a custom form input validator.

I explain my situation:

I have this view that contains a registration form:

@extends('layouts.app')

@section('content')

    <div class="row">

        <div class="col-md-12">

            <!--<h1 class="page-header"><i class="glyphicon glyphicon-file"></i> Aggiungi un utente albergatore</h1>-->
            <h1 class="page-header"><i class="fa fa-bed" aria-hidden="true" style="margin-right: 2%"></i>Aggiungi un utente albergatore</h1>

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <strong>Whoops!</strong> Sono stati riscontrati errori nel tuo input.<br /><br />
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li></li>
                        @endforeach
                    </ul>
                </div>
            @endif


            <form method="post" action="/registration">

                <div class="form-group">
                    <label>Nome</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-user"></i></div>
                        <input type="text" name="name" class="form-control" placeholder="Inserisci il tuo nome">
                    </div>
                </div>

                <div class="form-group">
                    <label>Cognome</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-user"></i></div>
                        <input type="text" name="surname" class="form-control" placeholder="Inserisci il tuo cognome">
                    </div>
                </div>

                <div class="form-group">
                    <label>Username</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-user"></i></div>
                        <input type="text" name="login" class="form-control" placeholder="Inserisci il tuo username">
                    </div>
                </div>

                <div class="form-group">
                    <label>E-mail</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-envelope"></i></div>
                        <input type="email" name="email" class="form-control" placeholder="Inserisci il tuo indirizzo e-mail">
                    </div>
                </div>

                <div class="form-group">
                    <label>Conferma e-mail</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-envelope"></i></div>
                        <input type="email" name="email_confirmation" class="form-control" placeholder="Inserisci il tuo indirizzo e-mail">
                    </div>
                </div>

                <div class="form-group">
                    <label>Password</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-lock"></i></div>
                        <input type="password" name="pass" class="form-control" placeholder="Inserisci la tua password">
                    </div>
                </div>

                <div class="form-group">
                    <label>Conferma password</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="fa fa-lock"></i></div>
                        <input type="password" name="pass_confirmation" class="form-control" placeholder="Inserisci la tua password">
                    </div>
                </div>


                <div class="form-group">
                    <label>Captcha</label>
                    <div class="input-group">
                        {!! app('captcha')->display(); !!}
                    </div>
                </div>

                

                <button type="submit" class="btn btn-default">Submit</button>

            </form>

        </div>

    </div>


@endsection

As you can see in the previous code this view contains this section that iterates on the possible "syntax" errors and show it in this page if the input is not validated:

        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <strong>Whoops!</strong> Sono stati riscontrati errori nel tuo input.<br /><br />
                <ul>
                    @foreach ($errors->all() as $error)
                        <li></li>
                    @endforeach
                </ul>
            </div>
        @endif

This is the controller method that handle the form submission:

public function store(Request $request) {

    Log::info('store() START');

    $data = Input::all();

    Log::info('INSERTED DATA: '.implode("|", $data));

    // Regole di validazione sintattica del contenuto del form di registrazione:
    $rules = array(
        'name' => 'required',
        'surname' => 'required',
        'login' => 'required',
        'email' => 'required|email|confirmed',
        //'email_confirmation' => 'required|email|confirmed',
        'pass' => 'required|required|min:6',
        //'passConfirm' => 'required',
        'g-recaptcha-response' => 'required|captcha',

    );

    // Validazione sintattica del form di registrazione:
    $validator = Validator::make($data, $rules);

    /*
     * Se il form di registrazione contiene dati sintatticamente errati, ritorna alla pagina di registrazione
     * passando la lista dei messaggi di errore da visualizzare
     */
    if ($validator->fails()){
        return Redirect::to('/registration')->withInput()->withErrors($validator);
    }

    // Altrimenti se i dati inseriti sono sintatticamente corretti:
    else {

        // Controlla se esiste un utente con la stessa e-mail:
        $resultCheckEmail = DB::select('select * from pm_user where email = ?', [$data['email']]);
        Log::info('blablabla');

        if (empty($resultCheckEmail)) {

        }

        // Controlla se esiste un utente con lo stesso username:
        $resultCheckUsername = DB::select('select * from pm_user where email = ?', [$data['login']]);
        Log::info('blablabla');

        if (empty($resultCheckUsername)) {

        }
    }
}

As you can see in this code I define a $rules array that defines the validation rules of the data submitted and I check if these data ara valid by this line:

$validator = Validator::make($data, $rules);

If the validation fails it come back to the registration form view passing the errors by this line:

return Redirect::to('/registration')->withInput()->withErrors($validator);

If the input validation is correct it enters into this block of code:

    else {

        // Check if yet exists an user with the same e-mail in the database:
        $resultCheckEmail = DB::select('select * from pm_user where email = ?', [$data['email']]);
        Log::info('blablabla');

        if (empty($resultCheckEmail)) {
            // RETURN TO THE REGISTRATION FORM WITH A SPECIFIC ERROR MESSAGE
        }

        // Check if yet exists an user with the same username in the database:
        $resultCheckUsername = DB::select('select * from pm_user where email = ?', [$data['login']]);
        Log::info('blablabla');

        if (empty($resultCheckUsername)) {
            // RETURN TO THE REGISTRATION FORM WITH A SPECIFIC ERROR MESSAGE
        }
    }

Here I have also to check if yet exists an user having the same e-mail and\or the same username (because I can't register 2 users having the same e-mail and/or the same username).

So if exists a user having the same e-mail and/or the same username I have to come back to the registration form page with a specific error that have to be shown in the same place of the previous error message, here:

I think that I have to put in some way these messages into the $errors array.

How can I do it? What is the smarter way to handle this situation?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mworWm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire