This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Product; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class CartRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'product_id' => [ | ||
'required', | ||
function ($attribute, $value, $fail) { | ||
if (!$product = Product::find($value)) { | ||
return $fail('該商品不存在'); | ||
} | ||
if (!$product->on_sale) { | ||
return $fail('該商品未上架'); | ||
} | ||
}, | ||
], | ||
'amount' => ['required', 'integer', 'min:1'], | ||
]; | ||
} | ||
|
||
public function attributes() | ||
{ | ||
return [ | ||
'amount' => '商品數量', | ||
]; | ||
} | ||
|
||
public function messages() | ||
{ | ||
return [ | ||
'required' => '請選擇商品', | ||
'min' => '「:attribute」至少要 :min 個', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters