I have my current form setup to use a table layout without any issues except
for when I try to apply my Error decorators.

Before the form is submitted the form should look like:

<tr>
  <td id="email-label">
    <label for="email" class="formLabel required">1. Your Email
Address*</label>
  </td>
  <td class="formInput">
    <input type="text" name="email" id="email" value="" requiredSuffix="*"
size="30">
    <div class="hint">We guarantee we won't spam you</div>
  </td>
</tr>
.....


if an error needs to be displayed then the error message should display
underneath it's corresponding form element, which it does.  My problem is
that if I apply my custom error decorator, if there isn't an error message
then I get double "tr" tags around the elements:

<form id="signup" enctype="application/x-www-form-urlencoded" action=""
method="post"><table>
<tr><tr><td id="email-label"><label for="email" class="formLabel
required">1. Your Email Address*</label></td>
<td class="formInput">
<input type="text" name="email" id="email" value="" requiredSuffix="*"
size="30">
<div class="hint">We guarantee we won't spam you</div></td></tr></tr>
<tr><tr><td id="pass-label"><label for="pass" class="formLabel required">2.
Choose Password*</label></td>
<td class="formInput">
<input type="password" name="pass" id="pass" value="" requiredSuffix="*"
size="30">
<div class="hint">Minimum of 8 characters</div></td></tr></tr>
<tr><tr><td id="password_confirm-label"><label for="password_confirm"
class="formLabel required">3. Verify Password*</label></td>

<td class="formInput">
<input type="password" name="password_confirm" id="password_confirm"
value="" requiredSuffix="*" size="30"></td></tr></tr>
<tr><tr><td id="username-label"><label for="username" class="formLabel
required">4. Choose Username*</label></td>
<td class="formInput">
<input type="text" name="username" id="username" value="" requiredSuffix="*"
size="30"></td></tr></tr>

// standard element decorator
  protected $_standardElementDecorator = array(
    'ViewHelper',
    array('Description', array('escape' => false, 'tag' => 'div')),
    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' =>
'formInput')),
    array('Label', array('tag' => 'td', 'class' => 'formLabel')),
    array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    array('HtmlTagError', array('tag'=>'tr'))
  );

//HtmlTagError
<?php

require_once 'Zend/Form/Decorator/HtmlTag.php';

class Site_Form_Decorator_HtmlTagError extends Zend_Form_Decorator_HtmlTag {

  public function render($content) {

    // Get element contained by decorator
    $element = $this->getElement();

    // Get view object
    $view    = $element->getView();

    if (null === $view) {
      return parent::render($content);
    }

    $errCount = 0;

    // If not a single element, get elements and all associated error
messages
    if (method_exists($element, 'getElements')) {
      $elements = $element->getElements();
      foreach ($elements as $el) {
        if ($el->getMessages()) {
          // Count errors
          $errCount++;
          $errors[] = $el->getMessages();
        }
      }
    } else {
      // If single element, get error messages
      $errors = $element->getMessages();
    }

    if (0 == $errCount && empty($errors)) {
      return parent::render($content);
    }

    // Add error class to HtmlTag tag.
    // Use array merge to ensure that existing options are persisted
    $options = $this->getOptions();

    if (array_key_exists('class', $options)) {
      $options['class'] = $options['class'] . ' error';
    } else {
      $options['class'] = 'error';
    }

    $this->setOptions($options);

    // Set up the FormErrors view helper
    $displayErrors = new Zend_View_Helper_FormErrors();
    $displayErrors->setView($view);
    $displayErrors->setElementStart('<td class="error" colspan="2">')
      ->setElementSeparator("<br/>\n")
      ->setElementEnd("</td>\n");

    // If only one field has errors
    if (0 == $errCount) {
      $html    = $displayErrors->formErrors($errors);
      $content .= $html;
    } else {
      // If more than one field has errors (usually a display group)
      $html = '';
      foreach ($errors as $error) {
        $html .= $displayErrors->formErrors($error);
      }
      $content .= $html;
      // Append generic error message to content
      //$content .= '<p class="error">This is a required field</p>';
    }

    // Render HtmlTag decorator
    return parent::render($content);
  }
}


Any suggestions?
-- 
View this message in context: 
http://www.nabble.com/Zend-Form-Error-Decorators-tp24158654p24158654.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to