lundi 2 mai 2016

Laravel redirect withErrors not working

SO i've trying to make simple laravel validation, but kinda stuck because i cannot return validation error message.

Controller:

//for "GET" method
public function courseAdminCreate()
{
    return view('course/adminCreate');
}

//for "POST" method
public function doCourseAdminCreate()
{
    $rules = array(
        'name'  => 'required',
        'contact_name'  => 'required',
        'contact_number'  => 'required|numeric',
        'account_number'  => 'required|numeric',
        'address'  => 'required',
        'latitude'  => 'required',
        'longitude'  => 'required'
    );

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails()) {

        //get error message
        $messages = $validator->messages();
        //die($messages); //if i using DIE command, error message appear

        return redirect("course/admin/create")->withErrors($validator);

    } else {
        //Save to DB
    }
}

Routes.php:

//other code

Route::get('course/admin/create', ['as' => 'courseAdminCreate', 'uses' => 'CourseController@courseAdminCreate']);
Route::post('course/admin/create', ['as' => 'doCourseAdminCreate', 'uses' => 'CourseController@doCourseAdminCreate']);

//other code

Views:

@extends('layouts.app')

@section('title')
Course
@stop

@section('content')
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                @yield('title') Add
            </div>
            <div class="panel-body">
                {!! Form::open(['url' => '/course/admin/create']) !!}
                <div class="row">
                    <div class="col-lg-12">

                        <!--START PRINT ERROR MESSAGE -->
                        @if (count($errors) > 0)
                        <div class="alert alert-warning alert-dismissable">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            @foreach ($errors->all() as $error)
                                <li></li>
                            @endforeach
                        </div>
                        @endif
                        <!-- END PRINT ERROR MESSAGE -->

                    </div>
                    <div class="col-lg-6">
                        <div class="form-group">
                            <label>Course Name</label>
                            <input class="form-control" name="name" placeholder="name.." required>
                        </div>
                        <div class="form-group">
                            <label>Contact Name</label>
                            <input class="form-control" name="contact_name" placeholder="contact.." required>
                        </div>
                        <div class="form-group">
                            <label>Contact Number</label>
                            <input class="form-control" name="contact_number" placeholder="number.." required>
                        </div>
                        <div class="form-group">
                            <label>Account Number</label>
                            <input class="form-control" name="account_number" placeholder="account.." required>
                        </div>
                    </div>
                    <div class="col-lg-6">
                        <div class="form-group">
                            <label>Address</label>
                            <textarea class="form-control" name="address" rows="5" placeholder="address.." required></textarea>
                        </div>
                        <div class="form-group">
                            <label>latitude</label>
                            <input class="form-control" name="latitude" placeholder="latitude.." required>
                        </div>
                        <div class="form-group">
                            <label>longitude</label>
                            <input class="form-control" name="longitude" placeholder="longitude.." required>
                        </div>
                    </div>
                    <!-- /.col-lg-12 -->
                    <div class="col-lg-12">
                        <input type="submit" class="btn btn-default btn-success" value="Save"/>
                        <a href="" class="btn btn-default btn-warning">Cancel</a>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row (nested) -->
                {!! Form::close() !!}
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- /.row -->
@stop

The validation is actually working, so for instance if i input other character beside numeric in "contact_number" it will redirect me back to course/admin/create, the problem are i cannot print the message. $errors in the view always count as empty array if i try to var_dump it.

Hope my information is enough, thank you very much.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire