From a04548a814e4a72d7024aec5cdf9ebe16131dbb3 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 9 Sep 2022 13:51:36 -0400 Subject: [PATCH] Update InputfieldWrapper to have an error method that enables you get get Inputfields having errors rather than just getting error messages. --- wire/core/Inputfield.php | 14 ++++++++++++++ wire/core/InputfieldWrapper.php | 34 +++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/wire/core/Inputfield.php b/wire/core/Inputfield.php index 8f8384e3..b8d0f44c 100644 --- a/wire/core/Inputfield.php +++ b/wire/core/Inputfield.php @@ -1910,6 +1910,20 @@ abstract class Inputfield extends WireData implements Module { return $errors; } + /** + * Clear errors from this Inputfield + * + * This is the same as `$inputfield->getErrors(true);` but has no return value. + * + * #pw-group-states + * + * @since 3.0.205 + * + */ + public function clearErrors() { + $this->getErrors(true); + } + /** * Does this Inputfield have the requested property or attribute? * diff --git a/wire/core/InputfieldWrapper.php b/wire/core/InputfieldWrapper.php index a437e8ca..5f4a4ed9 100644 --- a/wire/core/InputfieldWrapper.php +++ b/wire/core/InputfieldWrapper.php @@ -937,9 +937,9 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre if(!isset($ffAttrs['id'])) $ffAttrs['id'] = 'wrap_' . $inputfield->attr('id'); $ffAttrs['class'] = str_replace('Inputfield_ ', '', $ffAttrs['class']); $wrapClass = $inputfield->getSetting('wrapClass'); - if($wrapClass) $ffAttrs['class'] .= " " . $wrapClass; + if($wrapClass) $ffAttrs['class'] = trim("$ffAttrs[class] $wrapClass"); foreach($inputfield->wrapAttr() as $k => $v) { - if(!empty($ffAttrs[$k])) { + if($k === 'class' && !empty($ffAttrs[$k])) { $ffAttrs[$k] .= " $v"; } else { $ffAttrs[$k] = $v; @@ -1306,8 +1306,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre * * Should only be called after `InputfieldWrapper::processInput()`. * - * #pw-group-input - * #pw-group-retrieval-and-traversal + * #pw-group-errors * * @param bool $clear Specify true to clear out the errors (default=false). * @return array Array of error strings @@ -1326,6 +1325,33 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre return $errors; } + /** + * Get Inputfield objects that have errors + * + * #pw-group-errors + * + * @return Inputfield[] + * @since 3.0.205 + * + */ + public function getErrorInputfields() { + $a = array(); + if(count(parent::getErrors())) { + $name = $this->attr('name'); + $a[$name] = $this; + } + foreach($this->children() as $child) { + /** @var Inputfield $child */ + if($child instanceof InputfieldWrapper) { + $a = array_merge($a, $child->getErrorInputfields()); + } else if(count($child->getErrors())) { + $name = $child->attr('name'); + $a[$name] = $child; + } + } + return $a; + } + /** * Return all children Inputfield objects *