From 5dde4e1aa530c2df8e099426deef7fb0337add08 Mon Sep 17 00:00:00 2001 From: jitendra Date: Tue, 2 Nov 2021 22:22:04 +0530 Subject: [PATCH] Issue #308 fixed --- .../src/Database/Seeders/AttributeSeeder.php | 2 +- .../src/Http/Requests/AttributeForm.php | 57 ++++++++++--------- .../Repositories/AttributeValueRepository.php | 13 +++-- .../src/Repositories/PersonRepository.php | 2 +- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/packages/Webkul/Admin/src/Database/Seeders/AttributeSeeder.php b/packages/Webkul/Admin/src/Database/Seeders/AttributeSeeder.php index 5b9924fb7..09dd5c1bd 100644 --- a/packages/Webkul/Admin/src/Database/Seeders/AttributeSeeder.php +++ b/packages/Webkul/Admin/src/Database/Seeders/AttributeSeeder.php @@ -160,7 +160,7 @@ public function run() 'validation' => 'numeric', 'sort_order' => '3', 'is_required' => '0', - 'is_unique' => '0', + 'is_unique' => '1', 'quick_add' => '1', 'is_user_defined' => '0', 'created_at' => $now, diff --git a/packages/Webkul/Attribute/src/Http/Requests/AttributeForm.php b/packages/Webkul/Attribute/src/Http/Requests/AttributeForm.php index 0f15f4c27..1d4843ae6 100644 --- a/packages/Webkul/Attribute/src/Http/Requests/AttributeForm.php +++ b/packages/Webkul/Attribute/src/Http/Requests/AttributeForm.php @@ -74,6 +74,8 @@ public function rules() })->get(); foreach ($attributes as $attribute) { + $validations = []; + if ($attribute->type == 'boolean') { continue; } else if ($attribute->type == 'address') { @@ -81,34 +83,30 @@ public function rules() continue; } - $this->rules = array_merge($this->rules, [ + $validations = [ $attribute->code . '.address' => 'required', $attribute->code . '.country' => 'required', $attribute->code . '.state' => 'required', $attribute->code . '.city' => 'required', $attribute->code . '.postcode' => 'required', - ]); + ]; } else if ($attribute->type == 'email') { - $this->rules = array_merge($this->rules, [ - $attribute->code => $attribute->is_required ? 'required' : 'nullable', - $attribute->code . '.*.value' => [$attribute->is_required ? 'required' : 'nullable', 'email'], - $attribute->code . '.*.label' => $attribute->is_required ? 'required' : 'nullable', - ]); + $validations = [ + $attribute->code => [$attribute->is_required ? 'required' : 'nullable'], + $attribute->code . '.*.value' => [$attribute->is_required ? 'required' : 'nullable', 'email'], + $attribute->code . '.*.label' => $attribute->is_required ? 'required' : 'nullable', + ]; } else if ($attribute->type == 'phone') { - if (! $attribute->is_required) { - continue; - } - - $this->rules = array_merge($this->rules, [ - $attribute->code => 'required', - $attribute->code . '.*.value' => 'required', - $attribute->code . '.*.label' => 'required', - ]); + $validations = [ + $attribute->code => [$attribute->is_required ? 'required' : 'nullable'], + $attribute->code . '.*.value' => [$attribute->is_required ? 'required' : 'nullable'], + $attribute->code . '.*.label' => $attribute->is_required ? 'required' : 'nullable', + ]; } else { - $validations = [$attribute->is_required ? 'required' : 'nullable']; + $validations[$attribute->code] = [$attribute->is_required ? 'required' : 'nullable']; if ($attribute->type == 'text' && $attribute->validation) { - array_push($validations, + array_push($validations[$attribute->code], $attribute->validation == 'decimal' ? new Decimal : $attribute->validation @@ -116,19 +114,22 @@ public function rules() } if ($attribute->type == 'price') { - array_push($validations, new Decimal); - } - - if ($attribute->is_unique) { - array_push($validations, function ($field, $value, $fail) use ($attribute) { - if (! $this->attributeValueRepository->isValueUnique($this->id, request('entity_type'), $attribute, request($attribute->code))) { - $fail('The :attribute has already been taken.'); - } - }); + array_push($validations[$attribute->code], new Decimal); } + } - $this->rules[$attribute->code] = $validations; + if ($attribute->is_unique) { + array_push($validations[in_array($attribute->type, ['email', 'phone']) + ? $attribute->code . '.*.value' + : $attribute->code + ], function ($field, $value, $fail) use ($attribute) { + if (! $this->attributeValueRepository->isValueUnique($this->id, request('entity_type'), $attribute, request($field))) { + $fail('The value has already been taken.'); + } + }); } + + $this->rules = array_merge($this->rules, $validations); } return $this->rules; diff --git a/packages/Webkul/Attribute/src/Repositories/AttributeValueRepository.php b/packages/Webkul/Attribute/src/Repositories/AttributeValueRepository.php index c2947b2d5..cc13b83e9 100755 --- a/packages/Webkul/Attribute/src/Repositories/AttributeValueRepository.php +++ b/packages/Webkul/Attribute/src/Repositories/AttributeValueRepository.php @@ -127,13 +127,18 @@ public function save(array $data, $entityId) */ public function isValueUnique($entityId, $entityType, $attribute, $value) { - $result = $this->resetScope()->model - ->where($this->model::$attributeTypeFields[$attribute->type], $value) + $query = $this->resetScope()->model ->where('attribute_id', $attribute->id) ->where('entity_type', $entityType) - ->where('entity_id', '!=', $entityId)->get(); + ->where('entity_id', '!=', $entityId); - return $result->count() ? false : true; + if (in_array($attribute->type, ['email', 'phone'])) { + $query->where($this->model::$attributeTypeFields[$attribute->type], 'like', "%{$value}%"); + } else { + $query->where($this->model::$attributeTypeFields[$attribute->type], $value); + } + + return $query->get()->count() ? false : true; } /** diff --git a/packages/Webkul/Contact/src/Repositories/PersonRepository.php b/packages/Webkul/Contact/src/Repositories/PersonRepository.php index 1686f2326..9d9da3b2d 100644 --- a/packages/Webkul/Contact/src/Repositories/PersonRepository.php +++ b/packages/Webkul/Contact/src/Repositories/PersonRepository.php @@ -71,7 +71,7 @@ public function update(array $data, $id, $attribute = "id") } /** - * Retreives customers count based on date + * Retrieves customers count based on date * * @return number */