77use InvalidArgumentException ;
88
99use PHPForm \Exceptions \ValidationError ;
10+ use PHPForm \PHPFormConfig ;
1011use PHPForm \Utils \Attributes ;
1112
1213abstract class Field
@@ -55,9 +56,7 @@ abstract class Field
5556 /**
5657 * @var array Array of message errors.
5758 */
58- protected $ error_messages = array (
59- 'required ' => 'This field is required. '
60- );
59+ protected $ error_messages = array ();
6160
6261 /**
6362 * Instantiates a field.
@@ -84,6 +83,12 @@ public function __construct(array $args = array())
8483 $ this ->error_messages = array_key_exists ('error_messages ' , $ args ) ?
8584 $ args ['error_messages ' ] : $ this ->error_messages ;
8685
86+ $ default_error_messages = array (
87+ 'required ' => PHPFormConfig::getIMessage ("REQUIRED " ),
88+ );
89+
90+ $ this ->error_messages = array_merge ($ default_error_messages , $ this ->error_messages );
91+
8792 if (!is_null ($ this ->widget )) {
8893 // instantiate widget class if string is passed like so: Widget::class
8994 if (is_string ($ this ->widget )) {
@@ -92,8 +97,6 @@ public function __construct(array $args = array())
9297
9398 $ this ->widget ->setAttrs ($ this ->widgetAttrs ($ this ->widget ));
9499 }
95-
96- $ this ->error_messages = array_merge ($ this ->getErrorMessages (), $ this ->error_messages );
97100 }
98101
99102 /**
@@ -245,27 +248,4 @@ public function widgetAttrs($widget)
245248 {
246249 return $ this ->widget_attrs ;
247250 }
248-
249- /**
250- * Returns the combined inherited error messages for the field.
251- *
252- * @param string $class The class name to retrieve error message from.
253- *
254- * @return array
255- */
256- private function getErrorMessages ($ class = null )
257- {
258- if (is_null ($ class )) {
259- $ class = get_class ($ this );
260- }
261-
262- $ error_messages = array ();
263-
264- do {
265- $ class_vars = get_class_vars ($ class );
266- $ error_messages = array_merge ($ class_vars ['error_messages ' ], $ error_messages );
267- } while ($ class = get_parent_class ($ class ));
268-
269- return $ error_messages ;
270- }
271251}
0 commit comments