路由器
Route::get('/contact',['uses'=>'Admin\ContactController@show','as'=>'contact']);
Route::post('/contact',['uses'=>'Admin\ContactController@store']);
控制器
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\ContactRequest;
use App\Http\Controllers\Controller;
use Validator;
class ContactController extends Controller
{
public function store(Request $request,$id=FALSE) {
if($request->isMethod('post')) {
$messages = [
'name.required' => 'ПОЛЕ :attribute обязательно к заполнению!!!!',
'email.max' => 'Максимально допустимое количество символов - :max',
];
$validator = Validator::make($request->all(),[
'name'=>'required',
/*'email'=>'required'*/
],$messages);
$validator->sometimes(['email','site'],'required',function($input) {
/*dump($input);
exit();*/
return strlen($input->name) >= 10;
});
$validator->after(function($validator) {
$validator->errors()->add('name','ДОполнительное сообщение');
});
if($validator->fails()) {
$messages = $validator->errors();
//dump ($messages->first());
dump($validator->failed());
exit();
return redirect()->route('contact')->withErrors($validator)->withInput();
}
}
return view('default.contact',['title'=>'Contacts']);
}
public function show() {
return view('default.contact',['title'=>'Contacts']);
}
}
模板
extends('default.layouts.layout')
@section('content')
<div class="col-md-9">
<div class="">
<h2>Contact us!</h2>
</div>
<p>
This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.
</p>
{{ count($errors)}}
@if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error}}</li>
@endforeach
</ul>
</div>
@endif
<form method="post" action="{{ route('contact') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Jane Doe">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" value="{{ old('email') }}" name="email" placeholder="Email">
</div>
<div class="form-group">
<label for="site">Site</label>
<input type="text" class="form-control" id="site" value="{{ old('site') }}" name="site" placeholder="Site">
</div>
<div class="form-group">
<label for="text">Text</label>
<textarea class="form-control" id="text" name="text" rows="3">{{ old('text') }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
@endsection
问题是我没有显示验证错误和消息“更多消息”。虽然验证成功。问题是。我不想显示我尝试使用foreach
在其上行走的$ messages,但是laravel说它找到了$ messages。虽然显示控制器$ messages。该模式还显示错误为0,但是在控制器中显示为验证错误。
有什么问题吗?
答案 0 :(得分:1)
尝试一下:我希望它对您有用。
//controller
if ($request->method() == 'POST') {
$rules = [
'name' => 'required',
'email' => 'required'
];
$validator = Validator::make($request->all(), $rules);
if (!$validator->fails()) {
}
else{
//validation_error();
}
}
并使用js进行验证
$(function () {
$("#form_id").validate({
errorElement: 'small', errorClass: 'invalid-feedback',
rules: {
name: {
required: true,
},
email: {
required: true,
}
},
messages: {
name: {
required: "Name Field Is Required.",
},
email: {
required: "Email Field Is Required.",
}
},
highlight: function (element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function (element) {
$(element).closest('.form-group').removeClass('has-error');
},
success: function (element) {
$(element).closest('.form-group').removeClass('has-error');
$(element).closest('.form-group').children('small.invalid-feedback').remove();
},
errorPlacement: function (error, element) {
error.appendTo(element.closest('.form-group'));
},
submitHandler: function (form) {
$(window).block({
'message': '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>',
'css': {
border: '0',
padding: '0',
backgroundColor: 'none',
marginTop: '5%',
zIndex: '10600'
},
overlayCSS: {backgroundColor: '#555', opacity: 0.3, cursor: 'wait', zIndex: '10600'},
});
$.post('', $("#form_id").serialize(), function (data) {
if (data.code == '1') {
swal({
title: "",
text: data.message,
type: "success"
}, function () {
document.location.href = base_url + '/listing_page';
});
} else if (data === '0') {
swal("", "Something wrong while save building information.", "warning");
} else {
swal("", data, "error");
}
$(window).unblock();
});
}
});
$('select').change(function () {
$("#form_id").validate().element($(this));
});
});