Skip to content

Commit

Permalink
Merge pull request #628 from jitendra-webkul/master
Browse files Browse the repository at this point in the history
Issue #308 fixed
  • Loading branch information
jitendra-webkul committed Nov 2, 2021
2 parents 3104bd0 + 5dde4e1 commit 785bc6f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
57 changes: 29 additions & 28 deletions packages/Webkul/Attribute/src/Http/Requests/AttributeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,61 +74,62 @@ public function rules()
})->get();

foreach ($attributes as $attribute) {
$validations = [];

if ($attribute->type == 'boolean') {
continue;
} else if ($attribute->type == 'address') {
if (! $attribute->is_required) {
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
);
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 785bc6f

Please sign in to comment.