close #2291 Fixed: Quantity validation issue
This commit is contained in:
parent
9ddbeddd2b
commit
a40db8195d
@ -9,6 +9,8 @@ use Illuminate\Support\Str;
|
|||||||
|
|
||||||
class Document extends FormRequest
|
class Document extends FormRequest
|
||||||
{
|
{
|
||||||
|
protected $items_quantity_size = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the validation rules that apply to the request.
|
* Get the validation rules that apply to the request.
|
||||||
*
|
*
|
||||||
@ -43,13 +45,7 @@ class Document extends FormRequest
|
|||||||
// Get company id
|
// Get company id
|
||||||
$company_id = (int) $this->request->get('company_id');
|
$company_id = (int) $this->request->get('company_id');
|
||||||
|
|
||||||
$quantity_size = 5;
|
$rules = [
|
||||||
|
|
||||||
if ((Str::substrCount($this->request->get('quantity'), '.') > 1) || (Str::substrCount($this->request->get('quantity'), ',') > 1)) {
|
|
||||||
$quantity_size = 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
'type' => 'required|string',
|
'type' => 'required|string',
|
||||||
'document_number' => 'required|string|unique:documents,NULL,' . $id . ',id,type,' . $type . ',company_id,' . $company_id . ',deleted_at,NULL',
|
'document_number' => 'required|string|unique:documents,NULL,' . $id . ',id,type,' . $type . ',company_id,' . $company_id . ',deleted_at,NULL',
|
||||||
'status' => 'required|string',
|
'status' => 'required|string',
|
||||||
@ -57,7 +53,6 @@ class Document extends FormRequest
|
|||||||
'due_at' => 'required|date_format:Y-m-d H:i:s|after_or_equal:issued_at',
|
'due_at' => 'required|date_format:Y-m-d H:i:s|after_or_equal:issued_at',
|
||||||
'amount' => 'required',
|
'amount' => 'required',
|
||||||
'items.*.name' => 'required|string',
|
'items.*.name' => 'required|string',
|
||||||
'items.*.quantity' => 'required|max:' . $quantity_size,
|
|
||||||
'items.*.price' => 'required|amount',
|
'items.*.price' => 'required|amount',
|
||||||
'currency_code' => 'required|string|currency',
|
'currency_code' => 'required|string|currency',
|
||||||
'currency_rate' => 'required|gt:0',
|
'currency_rate' => 'required|gt:0',
|
||||||
@ -67,6 +62,23 @@ class Document extends FormRequest
|
|||||||
'company_logo' => $company_logo,
|
'company_logo' => $company_logo,
|
||||||
'attachment.*' => $attachment,
|
'attachment.*' => $attachment,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$items = $this->request->get('items');
|
||||||
|
|
||||||
|
if ($items) {
|
||||||
|
foreach ($items as $key => $item) {
|
||||||
|
$size = 5;
|
||||||
|
|
||||||
|
if (Str::contains($item['quantity'], ['.', ','])) {
|
||||||
|
$size = 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules['items.' . $key . '.quantity'] = 'required|max:' . $size;
|
||||||
|
$this->items_quantity_size[$key] = $size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withValidator($validator)
|
public function withValidator($validator)
|
||||||
@ -83,13 +95,20 @@ class Document extends FormRequest
|
|||||||
|
|
||||||
public function messages()
|
public function messages()
|
||||||
{
|
{
|
||||||
return [
|
$messages = [
|
||||||
'items.*.name.required' => trans('validation.required', ['attribute' => Str::lower(trans('general.name'))]),
|
'items.*.name.required' => trans('validation.required', ['attribute' => Str::lower(trans('general.name'))]),
|
||||||
'items.*.quantity.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.quantity'))]),
|
'items.*.quantity.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.quantity'))]),
|
||||||
'items.*.quantity.size' => trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity'))]),
|
|
||||||
'items.*.price.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.price'))]),
|
'items.*.price.required' => trans('validation.required', ['attribute' => Str::lower(trans('invoices.price'))]),
|
||||||
'items.*.currency.required' => trans('validation.custom.invalid_currency'),
|
'items.*.currency.required' => trans('validation.custom.invalid_currency'),
|
||||||
'items.*.currency.string' => trans('validation.custom.invalid_currency'),
|
'items.*.currency.string' => trans('validation.custom.invalid_currency'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($this->items_quantity_size) {
|
||||||
|
foreach ($this->items_quantity_size as $key => $quantity_size) {
|
||||||
|
$messages['items.' . $key . '.quantity.max'] = trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity')), 'size' => $quantity_size]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $messages;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ use Illuminate\Support\Str;
|
|||||||
|
|
||||||
class DocumentItem extends FormRequest
|
class DocumentItem extends FormRequest
|
||||||
{
|
{
|
||||||
|
protected $quantity_size = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the validation rules that apply to the request.
|
* Get the validation rules that apply to the request.
|
||||||
*
|
*
|
||||||
@ -14,21 +16,26 @@ class DocumentItem extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
$quantity_size = 5;
|
if (Str::contains($item['quantity'], ['.', ','])) {
|
||||||
|
$this->quantity_size = 7;
|
||||||
if ((Str::substrCount($this->request->get('quantity'), '.') > 1) || (Str::substrCount($this->request->get('quantity'), ',') > 1)) {
|
|
||||||
$quantity_size = 7;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'type' => 'required|string',
|
'type' => 'required|string',
|
||||||
'document_id' => 'required|integer',
|
'document_id' => 'required|integer',
|
||||||
'name' => 'required|string',
|
'name' => 'required|string',
|
||||||
'quantity' => 'required|max:' . $quantity_size,
|
'quantity' => 'required|max:' . $this->quantity_size,
|
||||||
'price' => 'required|amount',
|
'price' => 'required|amount',
|
||||||
'total' => 'required',
|
'total' => 'required',
|
||||||
'tax' => 'required',
|
'tax' => 'required',
|
||||||
'tax_id' => 'required',
|
'tax_id' => 'required',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function messages()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'quantity.max' => trans('validation.size', ['attribute' => Str::lower(trans('invoices.quantity')), 'size' => $this->quantity_size]),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user