优化产品数据验证
This commit is contained in:
parent
a01c57c025
commit
d42cd187dc
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Admin\Http\Requests\ProductRequest;
|
||||
use Beike\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Beike\Repositories\ProductRepo;
|
||||
|
|
@ -57,9 +58,14 @@ class ProductController extends Controller
|
|||
return $this->form($request, new Product());
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
public function store(ProductRequest $request)
|
||||
{
|
||||
return $this->save($request, new Product());
|
||||
try {
|
||||
(new ProductService)->create($request->all());
|
||||
return redirect()->to($this->getRedirect())->with('success', trans('common.created_success'));
|
||||
} catch (\Exception $e) {
|
||||
return redirect(admin_route('product.create'))->withErrors(['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function edit(Request $request, Product $product)
|
||||
|
|
@ -67,16 +73,20 @@ class ProductController extends Controller
|
|||
return $this->form($request, $product);
|
||||
}
|
||||
|
||||
public function update(Request $request, Product $product)
|
||||
public function update(ProductRequest $request, Product $product)
|
||||
{
|
||||
return $this->save($request, $product);
|
||||
try {
|
||||
(new ProductService)->update($product, $request->all());
|
||||
return redirect()->to($this->getRedirect())->with('success', trans('common.updated_success'));
|
||||
} catch (\Exception $e) {
|
||||
return redirect(admin_route('product.edit', $product))->withErrors(['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Product $product)
|
||||
{
|
||||
$product->delete();
|
||||
|
||||
return ['success' => true];
|
||||
return json_success(trans('common.deleted_success'));
|
||||
}
|
||||
|
||||
public function restore(Request $request)
|
||||
|
|
@ -110,17 +120,6 @@ class ProductController extends Controller
|
|||
return view('admin::pages.products.form.form', $data);
|
||||
}
|
||||
|
||||
protected function save(Request $request, Product $product)
|
||||
{
|
||||
if ($product->id) {
|
||||
$product = (new ProductService)->update($product, $request->all());
|
||||
} else {
|
||||
$product = (new ProductService)->create($request->all());
|
||||
}
|
||||
|
||||
return redirect($this->getRedirect())->with('success', 'product created');
|
||||
}
|
||||
|
||||
public function name(int $id)
|
||||
{
|
||||
$name = ProductRepo::getName($id);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
/**
|
||||
* PageRequest.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-19 21:58:20
|
||||
* @modified 2022-08-19 21:58:20
|
||||
*/
|
||||
|
||||
namespace Beike\Admin\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ProductRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'descriptions.*.name' => 'required|string|min:3|max:32',
|
||||
// 'descriptions.*.description' => 'required|string',
|
||||
'brand_id' => 'int',
|
||||
'sku' => 'required|string',
|
||||
'price' => 'required|float',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,15 @@
|
|||
|
||||
@section('content')
|
||||
|
||||
@if ($errors->has('error'))
|
||||
<x-admin-alert type="danger" msg="{{ $errors->first('error') }}" class="mt-4" />
|
||||
@endif
|
||||
|
||||
|
||||
@if ($errors->any())
|
||||
@dump($errors)
|
||||
@endif
|
||||
|
||||
<ul class="nav nav-tabs nav-bordered mb-3" role="tablist">
|
||||
<li class="nav-item" role="presentation">
|
||||
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tab-basic" type="button" >基础信息</button>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@
|
|||
@endpush
|
||||
|
||||
@section('content')
|
||||
|
||||
@if ($errors->has('error'))
|
||||
<x-admin-alert type="danger" msg="{{ $errors->first('error') }}" class="mt-4" />
|
||||
@endif
|
||||
|
||||
<div id="product-app">
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
|
|
|
|||
Loading…
Reference in New Issue