dimanche 31 décembre 2017

Display form errors (laravel validator) on bootstrap collapse

I'm using Bootstrap 3.7 and Laravel 5.5.

I have a form but it is into a bootstrap collapse. Moreover, I'm using Laravel Validator.

How it is possible to display the form (which is hide when the page is opened) when there are errors because with the Validator the page is refresh.

So I need to open the collapse to see the error. It is not very practical.

HTML:

@extends('layout.layout')

@section('content')
<div class="container">
    <h1 class="page-header">
        Administration
    </h1>

    <div class="row">
        <div class="col-lg-12">

            @if(session()->has('message'))
                <div class="alert alert-success">
                    
                </div>
            @endif

            <div class="panel-group" id="accordion">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        ...
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse">
                        ...
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">
                        ...
                    </div>
                    <div id="collapseTwo" class="panel-collapse collapse">
                        ...
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapseAjoutEmploye">Ajout d'un employé</a>
                        </h4>
                    </div>
                    <div id="collapseAjoutEmploye" class="panel-collapse collapse">
                        <form class="form-horizontal" method="POST" action="">
                            

                            <div class="form-group ">
                                <label for="ajout-employe-email" class="col-md-4 control-label">Email </label>

                                <div class="col-md-6">
                                    <input id="ajout-employe-email" type="text" class="form-control" name="email">

                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong></strong>
                                        </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        Enregistrer l'employé
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

PHP:

Route

Route::post('ajouterEmploye', 'AdministrationController@ajouterEmploye')->name('ajouterEmploye');

Controller:

public function ajouterEmploye(Request $request) {

    $this->validate($request,[
        'email' => 'required|email|exists:connexion'
    ] , [
        'email.required' => 'Ce champ est requis',
        'email.email'    => 'Ce champ doit être un email',
        'email.exists'   => 'Cet email est inconnu'
    ]);

    $user = User::getUserByEmail($request->email);

    User::where('id_utilisateur', '=', $user->id_utilisateur)
        ->update(['id_statut' => 1]);

    return redirect()->back()->with('message', "L'employé a bien été ajouté");
}

Thank's for help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire