Эх сурвалжийг харах

embeds和hasMany读取field上设置的validation message

jqh 6 жил өмнө
parent
commit
b5741fae6c

+ 25 - 2
src/Form/Field/Embeds.php

@@ -60,7 +60,7 @@ class Embeds extends Field
 
         $input = Arr::only($input, $this->column);
 
-        $rules = $attributes = [];
+        $rules = $attributes = $messages = [];
 
         /** @var Field $field */
         foreach ($this->buildEmbeddedForm()->fields() as $field) {
@@ -123,13 +123,36 @@ class Embeds extends Field
                 $attributes,
                 $this->formatValidationAttribute($input, $field->label(), $column)
             );
+
+            $messages = array_merge(
+                $messages,
+                $this->formatValidationMessages($input, $field->getValidationMessages())
+            );
         }
 
         if (empty($rules)) {
             return false;
         }
 
-        return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
+        return Validator::make($input, $rules, array_merge($this->getValidationMessages(), $messages), $attributes);
+    }
+
+    /**
+     * Format validation messages.
+     *
+     * @param array $input
+     * @param array $messages
+     *
+     * @return array
+     */
+    protected function formatValidationMessages(array $input, array $messages)
+    {
+        $result = [];
+        foreach ($messages as $k => $message) {
+            $result[$this->column.'.'.$k] = $message;
+        }
+
+        return $result;
     }
 
     /**

+ 29 - 2
src/Form/Field/HasMany.php

@@ -114,7 +114,7 @@ class HasMany extends Field
 
         $form = $this->buildNestedForm($this->column, $this->builder);
 
-        $rules = $attributes = [];
+        $rules = $attributes = $messages = [];
 
         /* @var Field $field */
         foreach ($form->fields() as $field) {
@@ -138,6 +138,11 @@ class HasMany extends Field
                 $attributes,
                 $this->formatValidationAttribute($input, $field->label(), $column)
             );
+
+            $messages = array_merge(
+                $messages,
+                $this->formatValidationMessages($input, $field->getValidationMessages())
+            );
         }
 
         Arr::forget($rules, NestedForm::REMOVE_FLAG_NAME);
@@ -169,7 +174,29 @@ class HasMany extends Field
             $newInput = $input;
         }
 
-        return Validator::make($newInput, $newRules, $this->getValidationMessages(), $attributes);
+        return Validator::make($newInput, $newRules, array_merge($this->getValidationMessages(), $messages), $attributes);
+    }
+
+    /**
+     * Format validation messages.
+     *
+     * @param array $input
+     * @param array $messages
+     *
+     * @return array
+     */
+    protected function formatValidationMessages(array $input, array $messages)
+    {
+        $result = [];
+        foreach ($input[$this->column] as $key => &$value) {
+            $newKey = $this->column.'.'.$key;
+
+            foreach ($messages as $k => $message) {
+                $result[$newKey.'.'.$k] = $message;
+            }
+        }
+
+        return $result;
     }
 
     /**