Issues

ZF-11667: error message via addError() un-neccessarily gets duplicated multiple times

Description

h1. ISSUE with Zend_Form_Element_Multi and it's subclasses...


## create element with 4 checkboxes
$elem = new Zend_Form_Element_MultiCheckbox('age', array(
    'multiOptions' => array(
        array('key'=>'a', 'value'=>'A'),
        array('key'=>'b', 'value'=>'B'),
        array('key'=>'c', 'value'=>'C'),
        array('key'=>'d', 'value'=>'D'),
    )
));

## check 3 of them
$elem->setValue(array('A','B','D'));

## later on, fails some validation on submit
$elem->addError('some error!');

print_r( $elem->getMessages() );
# =>Array
# (
#   [0] => some error!; some error!; some error!
# )

h2. SUSPICION:

  • why need to duplicate error messages based on number of values checked?
  • => some error!; some error!; some error!

h2. SUPPOSITION:

  • message should appear only once,
  • and %value% replacement should be affected by value array count.

with:

  $elem->addError('some error!');

result:

  Array (
     [0] => some error!
  )

with:

  $elem->addError('some error! %value%');
  
result:

  Array (
     [0] => some error! a; b; c
  )

h1. THE FIX


diff -Naru ZendFramework/library/Zend/Form/Element.php lib-zend-patch/Zend/Form/Element.php 
--- ZendFramework/library/Zend/Form/Element.php 2011-07-06 01:04:15.000000000 +0900
+++ lib-zend-patch/Zend/Form/Element.php        2011-08-15 00:34:32.000000000 +0900
@@ -226,6 +226,8 @@
      */
     protected $_isPartialRendering = false;
 
+    public $concatJustValuesInErrorMessage = true;
+
     /**
      * Constructor
      *
@@ -2234,11 +2236,16 @@
             if (($this->isArray() || is_array($value))
                 && !empty($value)
             ) {
-                $aggregateMessages = array();
-                foreach ($value as $val) {
-                    $aggregateMessages[] = str_replace('%value%', $val, $message);
+                if( $this->concatJustValuesInErrorMessage ){
+                    $concatVals = implode($this->getErrorMessageSeparator(), $value);
+                    $messages[$key] = str_replace('%value%', $concatVals, $message);
+                }else{
+                    $aggregateMessages = array();
+                    foreach ($value as $val) {
+                        $aggregateMessages[] = str_replace('%value%', $val, $message);
+                    }
+                    $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
                 }
-                $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
             } else {
                 $messages[$key] = str_replace('%value%', $value, $message);
             }

Comments

You will need to sign and submit a CLA before we can apply your suggested improvement. See here: http://framework.zend.com/wiki/display/…