【发布时间】:2019-10-25 05:24:21
【问题描述】:
当我验证产品名称时我有多个数据库连接我发送消息product name is exist before 进行查看,这里出现了问题。
消息出现在视图中,但所有表单输入均已清除。 如果产品名称不存在,我如何恢复这个问题。验证正确执行,如果在验证中发现错误,它会正常显示并且表单输入未清除。 这是我的控制器代码。
public function add(Request $request)
{
// start add
if($request->isMethod('post'))
{
if(isset($_POST['add']))
{
// start validatio array
$validationarray=$this->validate($request,[
//'name' =>'required|max:25|min:1|unique:mysql2.products,name|alpha',
'name' =>'required|alpha',
'price' =>'required|numeric',
]);
// check name is exist
$query = dBHelper::isExist('mysql2','products','`status`=? AND `deleted` =? AND `name`=?',array(1,1,$validationarray['name']));
if(!$query) {
$product=new productModel();
// start add
$product->name = $request->input('name');
$product->save();
$add = $product->id;
$poducten = new productEnModel();
$poducten->id_product = $add;
$poducten->name = $request->input('name');
$poducten->price = $request->input('price');
$poducten->save();
$dataview['message'] = 'data addes';
} else {
$dataview['message'] = 'name is exist before';
}
}
}
$dataview['pagetitle']="add product geka";
return view('productss.add',$dataview);
}
这是我的看法
@extends('layout.header')
@section('content')
@if(isset($message))
{{$message}}
@endif
@if(count($errors)>0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
<form role="form" action="add" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="box-body">
<div class="form-group{{$errors->has('name')?'has-error':''}}">
<label for="exampleInputEmail1">Employee Name</label>
<input type="text" name="name" value="{{Request::old('name')}}" class="form-control" id="" placeholder="Enter Employee Name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email Address</label>
<input type="text" name="price" value="{{Request::old('price')}}" class="form-control" id="" placeholder="Enter Employee Email Address">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="add" class="btn btn-primary">Add</button>
</div>
</form>
@endsection
这是我的路线
Route::get('/products/add',"produtController@add");
Route::post('/products/add',"produtController@add");
【问题讨论】:
-
将您的旧值从 {{ Request::old('price') }} 更改为 {{ old('price') }} 值得一试,这就是我使用它的方式,从来没有验证后出现空字段的问题。
标签: php laravel laravel-5 laravel-5.2