dimanche 24 décembre 2017

sending error message laravel required inputs

so i wanted to make validation to my register form so i made all my inputs required so i can test my validation that it works or not, so this is what i used in my controller

$Message = [
        'required' => 'This input is required',
    ];
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required',
        'password' => 'required',
        'UserName' => 'required',
        'Phone' => 'required',
        'SSN' => 'required',
        'SDT' => 'required',
        'Country' => 'required',
        'InsuranceAmount' => 'required',
        'City' => 'required',
        'Location' => 'required'
    ], $Message);

    if ($validator->fails()) {


        return redirect('/admin/users/create')
            ->withErrors($validator)
            ->withInput();
    }

    $user = new User;
    $user->name = Input::get('name');
    $user->email = Input::get('email');
    $user->password = bcrypt(Input::get('password'));
    $user->UserName = input::get('UserName');
    $user->Phone = input::get('Phone');
    $user->SSN = input::get('SSN');
    $user->SDT = input::get('SDT');
    $user->Country = input::get('Country');
    $user->City = input::get('City');
    $user->Location = input::get('Location');
    $user->save();
    return Redirect::to('/admin/users')->with('message', 'User Created');

Now if theres no errors it works fine and redirect to user list, but if a input is empty it will just redirect to the creation page whict is what i wanted but the problem is it won't send the error message with the redirect i tried dd the validator and it has all the messages fine heres my view

<form class="form-horizontal" role="form" method="POST" action="">
                    {!! csrf_field() !!}

                    <div class="form-group">
                        <label class="col-md-4 control-label">الاسم</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="name" value="">

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

                    <div class="form-group ">
                        <label class="col-md-4 control-label">اسم المستخدم</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="UserName" value="">
                            @if ($errors->has('UserName'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-md-4 control-label">رقم الجوال</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="Phone" value="">
                            @if ($errors->has('Phone'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif

                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-md-4 control-label">الرقم الوطني</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="SSN" value="">
                            @if ($errors->has('SSN'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-md-4 control-label">نوع الوثيقة</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="SDT" value="">
                            @if ($errors->has('SDT'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-md-4 control-label">الدولة</label>

                        <div class="col-md-6">
                            <select class="form-control" name="Country">

                                <option>الاردن</option>
                            </select>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 control-label">المدينة</label>

                        <div class="col-md-6">
                          <select class="form-control" name="City" >
                              <option>عمان</option>
                          </select>

                        </div>
                    </div>

                    <div class="form-group ">
                        <label class="col-md-4 control-label">اسم الشارع</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="Location" value="">
                            @if ($errors->has('Location'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group ltr-input">
                        <label class="col-md-4 control-label">البريد الإلكتروني</label>

                        <div class="col-md-6">
                            <input type="email" class="form-control" name="email" value="">

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

                    <div class="form-group ltr-input">
                        <label class="col-md-4 control-label">كلمة المرور</label>

                        <div class="col-md-6">
                            <input type="password" class="form-control" name="password">

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

                    <div class="form-group ltr-input">
                        <label class="col-md-4 control-label">تأكيد كلمة المرور</label>

                        <div class="col-md-6">
                            <input type="password" class="form-control" name="password_confirmation">

                            @if ($errors->has('password_confirmation'))
                                <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 pull-right">
                                <i class="fa fa-btn fa-user"></i> تسجيل
                            </button>
                        </div>
                    </div>
                </form>

and btw this is laravel 5.1



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire