|
@@ -8,7 +8,24 @@ use Illuminate\Validation\Rule;
|
|
|
|
|
|
trait CustomFieldRuleHelper
|
|
|
{
|
|
|
- protected function customFieldRuleByGroup(string $group, array $keys = []): array
|
|
|
+ protected array $groupLabelValue = [];
|
|
|
+
|
|
|
+ public function getGroupLabelValue(string $group, string $key, string $label = null)
|
|
|
+ {
|
|
|
+ if (! $label) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->groupLabelValue[$group][$key][$label] ?? null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $group
|
|
|
+ * @param array $keys
|
|
|
+ * @param string $inField value,label
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected function customFieldRuleByGroup(string $group, array $keys = [], string $inField = "value"): array
|
|
|
{
|
|
|
$customFields = CustomField::query()
|
|
|
->where("group", $group)
|
|
@@ -19,7 +36,7 @@ trait CustomFieldRuleHelper
|
|
|
|
|
|
$rules = [];
|
|
|
foreach ($customFields as $customField) {
|
|
|
- $rule = $this->customFieldToRule($customField);
|
|
|
+ $rule = $this->customFieldToRule($customField, $inField);
|
|
|
if (! $rule) {
|
|
|
continue;
|
|
|
}
|
|
@@ -30,17 +47,29 @@ trait CustomFieldRuleHelper
|
|
|
return $rules;
|
|
|
}
|
|
|
|
|
|
- protected function customFieldToRule(CustomField $customField): array
|
|
|
+ protected function customFieldToRule(CustomField $customField, string $inField = "value"): array
|
|
|
{
|
|
|
$rule = [];
|
|
|
if ($customField->required == 1) {
|
|
|
$rule[] = "required";
|
|
|
}
|
|
|
|
|
|
+ $inOptions = [];
|
|
|
+ if ($inField == "value") {
|
|
|
+ $inOptions = array_column($customField->options, "value");
|
|
|
+ } else {
|
|
|
+ foreach ($customField->options as $option) {
|
|
|
+ foreach ($option['lang'] as $label) {
|
|
|
+ $inOptions[] = $label;
|
|
|
+ $this->groupLabelValue[$customField->group][$customField->key][$label] = $option['value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$typeRule = match (CustomFieldType::tryFrom($customField->type)) {
|
|
|
CustomFieldType::TEXT => "string",
|
|
|
CustomFieldType::TEXTAREA => "string",
|
|
|
- CustomFieldType::SELECT => $customField->options ? Rule::in(array_column($customField->options, "value")) : "",
|
|
|
+ CustomFieldType::SELECT => $customField->options ? Rule::in($inOptions) : "",
|
|
|
CustomFieldType::NUMBER => "numeric",
|
|
|
default => ""
|
|
|
};
|