当满足所有必需的验证字段时,laravel请求数组清空

时间:2017-05-31 07:00:04

标签: php laravel laravel-5

当我的一个必填字段标准不符合时它一切正常,因为它应该给我一个关于填写所需字段的错误但是当我满足所有必需的验证字段时,请求数组变空,并且Laravel提出了所有必需的错误。 PS:我确实创建了一个Request类来处理请求,但这并没有解决问题。

形式:

@extends('frontend.myAccount.master')

 @section('myAccountPageContent')

<div class="page-content-on-my-account-page">
    <h1 class="text-center title-of-my-account text-rose">ثبت آگهی</h1>
    @if(count($errors) > 0 )
        <div class="alert alert-danger text-center">
            کاربر گرامی خطاهای موجود را اصلاح نموده و مجددا اطلاعات را ارسال کنید.
        </div>
    @endif
    <form action="{{ route('insertAds') }}" method="post" enctype="multipart/form-data">
        {{csrf_field()}}
        <div class="row">
            <div class="col-xs-12 col-md-12">
                <div class="panel panel-default custom-panel">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i
                                    class="material-icons text-rose">work</i><span> اطلاعات شغلی</span></h3>
                        <div class="panel-border-bottom"></div>
                    </div>
                    <div class="panel-body">
                        <div>
                            <select name="guild" title="انتخاب صنف" class="form-control"
                                    id="select-guild-in-add-new-ad">
                                <option disabled selected>انتخاب صنف</option>
                                @foreach($guilds as $guild)
                                    <option
                                            value="{{ $guild->id }}"
                                            @if(old('guild') == $guild->id)
                                            selected
                                            @endif>{{ $guild->name }}</option>
                                @endforeach
                            </select>
                            @if($errors->has('guild'))
                                <ul class="list-unstyled">
                                    @foreach($errors->get('guild') as $msg)
                                        <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                    @endforeach
                                </ul>
                            @endif
                            <select name="job" disabled title="انتخاب شغل" class="form-control"
                                    id="select-job-in-add-new-ad">
                                <option disabled selected>انتخاب شغل</option>
                            </select>
                            @if($errors->has('job'))
                                <ul class="list-unstyled">
                                    @foreach($errors->get('job') as $msg)
                                        <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                    @endforeach
                                </ul>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row card-view-wrapper">
            <div class="col-xs-12 col-md-6">
                <div class="panel panel-default custom-panel">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i
                                    class="material-icons text-rose">local_post_office</i><span>آدرس</span>
                        </h3>
                        <div class="panel-border-bottom"></div>
                    </div>
                    <div class="panel-body">
                        <div>
                            <select name="province" title="انتخاب استان" class="form-control"
                                    id="select-province-in-add-new-ad">
                                <option disabled selected>انتخاب استان</option>
                                @foreach($provinces as $prv)
                                    <option value="{{ $prv->id }}"
                                            @if(old('province') == $prv->id)
                                            selected
                                            @endif>{{ $prv->name }}</option>
                                @endforeach
                            </select>
                            @if($errors->has('province'))
                                <ul class="list-unstyled">
                                    @foreach($errors->get('province') as $msg)
                                        <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                    @endforeach
                                </ul>
                            @endif
                            <select name="city" disabled title="انتخاب شهر" class="form-control"
                                    id="select-city-in-add-new-ad">
                                <option disabled selected>انتخاب شهر</option>
                            </select>
                            @if($errors->has('city'))
                                <ul class="list-unstyled">
                                    @foreach($errors->get('city') as $msg)
                                        <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                    @endforeach
                                </ul>
                            @endif
                            <div class="form-group label-floating custom-form-group">
                                <label for="address-in-add-new-ad" class="control-label">آدرس پستی</label>
                                <textarea name="postal_address" id="address-in-add-new-ad" class="form-control"
                                          cols="30"
                                          rows="2">{{ old('postal_address') }}</textarea>
                                @if($errors->has('postal_address'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('postal_address') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>


                        </div>
                    </div>
                </div>
            </div>

            <div class="col-xs-12 col-md-6">
                <div class="panel panel-default custom-panel">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i
                                    class="material-icons text-rose">perm_contact_calendar</i><span>اطلاعات تماس</span>
                        </h3>
                        <div class="panel-border-bottom"></div>
                    </div>
                    <div class="panel-body">
                        <div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="phone-1-in-add-new-ad" class="control-label">تلفن تماس 1</label>
                                <input type="text" value="{{ old('tel1') }}" name="tel1" id="phone-1-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('tel1'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('tel1') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="phone-2-in-add-new-ad" class="control-label">تلفن تماس 2</label>
                                <input type="text" value="{{ old('tel2') }}" name="tel2" id="phone-2-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('tel2'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('tel2') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="phone-3-in-add-new-ad" class="control-label">تلفن تماس 3</label>
                                <input type="text" name="tel3" value="{{ old('tel3') }}" id="phone-3-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('tel3'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('tel3') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="instagram-address-in-add-new-ad" class="control-label">آدرس
                                    اینستاگرام</label>
                                <input type="text" name="instagram" value="{{ old('instagram') }}"
                                       id="instagram-address-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('instagram'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('instagram') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="telegram-address-in-add-new-ad" class="control-label">آدرس
                                    تلگرام</label>
                                <input type="text" name="telegram" value="{{ old('telegram') }}"
                                       id="telegram-address-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('telegram'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('telegram') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="website-address-in-add-new-ad" class="control-label">آدرس وب
                                    سایت</label>
                                <input name="website_url" value="{{ old('website_url') }}" type="text"
                                       id="website-address-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('website_url'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('website_url') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row card-view-wrapper">
            <div class="col-xs-12 col-md-6">
                <div class="panel panel-default custom-panel">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="material-icons text-rose">bookmark</i><span>اطلاعات پایه آگهی</span>
                        </h3>
                        <div class="panel-border-bottom"></div>
                    </div>
                    <div class="panel-body">
                        <div style="min-height:900px;">
                            <div class="form-group label-floating custom-form-group">
                                <label for="full-name-in-add-new-ad" class="control-label">نام صاحب
                                    آگهی</label>
                                <input name="full_name" value="{{ old('full_name') }}" type="text"
                                       id="full-name-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('full_name'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('full_name') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="full-name-in-add-new-ad" class="control-label">عنوان
                                    آگهی</label>
                                <input name="titleeee" value="{{ old('title') }}" type="text"
                                       id="full-name-in-add-new-ad" class="form-control">
                                @if($errors->has('title'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('title') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="facilities-in-add-new-ad" class="control-label">خدمات و
                                    امکانات</label>
                                <input name="description" value="{{ old('description') }}" type="text"
                                       id="facilities-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('description'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('description') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group label-floating custom-form-group">
                                <label for="working-time-in-add-new-ad" class="control-label">ساعت
                                    کاری</label>
                                <input name="working_time" value="{{ old('working_time') }}" type="text"
                                       id="working-time-in-add-new-ad"
                                       class="form-control">
                                @if($errors->has('working_time'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('working_time') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12 col-md-6">
                <div class="panel panel-default custom-panel">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i
                                    class="material-icons text-rose">attach_file</i><span>ضمیمه ها</span>
                        </h3>
                        <div class="panel-border-bottom"></div>
                    </div>
                    <div class="panel-body">
                        <div class="text-center">
                            <div class="fileinput fileinput-new text-center" data-provides="fileinput">
                                <div class="fileinput-new thumbnail img-raised">
                                    <img src="{{ \Illuminate\Support\Facades\URL::to('/frontend/img') }}/notavailable.jpg"
                                         alt="...">
                                </div>
                                <div class="fileinput-preview fileinput-exists thumbnail img-raised"></div>
                                <div>
                                <span class="btn btn-raised btn-round btn-success btn-file">
                                    <span class="fileinput-new">انتخاب تصویر کاور</span>
                                    <span class="fileinput-exists">تغییر عکس</span>
                                    <input type="file" name="cover_photo"/>
                                </span>
                                    <a href="#pablo" class="btn btn-danger btn-round fileinput-exists"
                                       data-dismiss="fileinput"><i class="fa fa-times"></i> حذف</a>
                                </div>
                                @if($errors->has('cover_photo'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('cover_photo') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>

                            <div class="form-group form-file-upload">
                                <label for="upload-video-clip-in-add-new">آپلود ویدئو کلیپ(مخصوص اپلیکیشن موبایل با
                                    فرمت 3gp):</label>
                                <input type="file" name="video_clip" id="upload-video-clip-in-add-new">
                                <div class="input-group">
                                    <input type="text" readonly="" class="form-control">
                                    <span class="input-group-btn input-group-s">
                                    <button type="button" class="btn btn-just-icon btn-round btn-rose">
                                        <i class="material-icons">attach_file</i>
                                    </button>
                                </span>
                                </div>
                                @if($errors->has('video_clip'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('video_clip') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group form-file-upload">
                                <label for="upload-video-clip-mp4-in-add-new">آپلود ویدئو کلیپ(مخصوص وب سایت با فرمت
                                    mp4):</label>
                                <input type="file" name="video_clip_mp4" id="upload-video-clip-mp4-in-add-new">
                                <div class="input-group">
                                    <input type="text" readonly="" class="form-control">
                                    <span class="input-group-btn input-group-s">
                                    <button type="button" class="btn btn-just-icon btn-round btn-rose">
                                        <i class="material-icons">attach_file</i>
                                    </button>
                                </span>
                                </div>
                                @if($errors->has('video_clip_mp4'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('video_clip_mp4') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <hr>
                            <div class="form-group form-file-upload">
                                <label for="upload-amr-audio-clip-in-add-new">آپلود فایل صوتی(مخصوص اپلیکیشن
                                    موبایل با فرمت amr):</label>
                                <input type="file" name="voice_file_name" id="upload-amr-audio-clip-in-add-new">
                                <div class="input-group">
                                    <input type="text" readonly="" class="form-control">
                                    <span class="input-group-btn input-group-s">
                                    <button type="button" class="btn btn-just-icon btn-round btn-rose">
                                        <i class="material-icons">attach_file</i>
                                    </button>
                                </span>
                                </div>
                                @if($errors->has('voice_file_name'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('voice_file_name') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                            <div class="form-group form-file-upload">
                                <label for="upload-audio-mep-clip-in-add-new">آپلود فایل صوتی(مخصوص پخش در
                                    وب با فرمت mp3):</label>
                                <input type="file" name="voice_file_name_mp3" id="upload-audio-mep-clip-in-add-new">
                                <div class="input-group">
                                    <input type="text" readonly="" class="form-control">
                                    <span class="input-group-btn input-group-s">
                                    <button type="button" class="btn btn-just-icon btn-round btn-rose">
                                        <i class="material-icons">attach_file</i>
                                    </button>
                                </span>
                                </div>
                                @if($errors->has('voice_file_name_mp3'))
                                    <ul class="list-unstyled">
                                        @foreach($errors->get('voice_file_name_mp3') as $msg)
                                            <li><span class="text-danger inline-error">{{ $msg }}</span></li>
                                        @endforeach
                                    </ul>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <div class="row">
            <div class="col-xs-12">
                <div class="form-group text-center">
                    <button type="submit" class="btn btn-rose btn-round">تایید</button>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection

路线:

Route::post('/ads/create', 'AdsUpdateHistoryController@add')->name('insertAds');
Route::get('/ads/create', 'AdsController@showCreatePage')->name('createAdShow');

控制器:

public function add(Request $request) {
    $validator = Validator::make($request->all(), [
        'guild' => 'required|numeric|exists:guild,id',
        'job' => 'required|numeric|exists:job,id',
        'province' => 'required|numeric|exists:province,id',
        'city' => 'required|numeric|exists:city,id',
        'postal_address' => 'nullable',
        'latitude' => 'numeric|nullable',
        'longitude' => 'numeric|nullable',
        'tel1' => 'string|nullable|max:100',
        'tel2' => 'string|nullable|max:100',
        'tel3' => 'string|nullable|max:100',
        'instagram' => 'string|nullable|max:300',
        'telegram' => 'string|nullable|max:300',
        'website_url' => 'string|nullable',
        'full_name' => 'nullable|string|max:200',
        'titleeee' => 'required|string|max:200',
        'description' => 'nullable|string',
        'working_time' => 'nullable|string',
        'cover_photo' => 'image|nullable',
        'video_clip' => 'file|mimes:3gp|nullable',
        'video_clip_mp4' => 'file|mimes:mp4|nullable',
        'voice_file_name' => 'file|mimes:amr|nullable',
        'voice_file_name_mp3' => 'file|mimes:mp3|nullable',
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors()->all();
        $inputs = $request->all();

        return [
            'errors' => $errors,
            'request' => $inputs
        ];
    }
}

1 个答案:

答案 0 :(得分:0)

您要返回一个数组,您需要Response的实例(例如view()redirect()back()等。)

检查最后一部分以查看更改:

public function add(Request $request) {
    $validator = Validator::make($request->all(), [
        'guild' => 'required|numeric|exists:guild,id',
        'job' => 'required|numeric|exists:job,id',
        'province' => 'required|numeric|exists:province,id',
        'city' => 'required|numeric|exists:city,id',
        'postal_address' => 'nullable',
        'latitude' => 'numeric|nullable',
        'longitude' => 'numeric|nullable',
        'tel1' => 'string|nullable|max:100',
        'tel2' => 'string|nullable|max:100',
        'tel3' => 'string|nullable|max:100',
        'instagram' => 'string|nullable|max:300',
        'telegram' => 'string|nullable|max:300',
        'website_url' => 'string|nullable',
        'full_name' => 'nullable|string|max:200',
        'titleeee' => 'required|string|max:200',
        'description' => 'nullable|string',
        'working_time' => 'nullable|string',
        'cover_photo' => 'image|nullable',
        'video_clip' => 'file|mimes:3gp|nullable',
        'video_clip_mp4' => 'file|mimes:mp4|nullable',
        'voice_file_name' => 'file|mimes:amr|nullable',
        'voice_file_name_mp3' => 'file|mimes:mp3|nullable',
    ]);

    if ($validator->fails()) {
        $errors = $validator->errors()->all();
        $inputs = $request->all();

        return back()->withErrors($errors)->withInput();
    }

    // Validator succeeded, do saving stuff etc.
}