In the last article, we have already set some basic decorators for Zend_Form. The next one is a decorator for the label, appending an asterisk (*) to labels of required fields, and display error messages below the label instead of the field. In your „Form“ subfolder, create a folder „Decorator“, and in it, a file named „Label.php“:

class JD_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
 public function getLabel()
 {
   $element = $this->getElement();
   $label = trim($element->getLabel());
   if ($element->isRequired()) {
     $label .= ' *';
   }
   $errors = $element->getMessages();
   if (empty($errors)) {
     $element->setLabel($label);
     return parent::getLabel();
   }
   $label .= '<br /><span style="color:red;font-size:0.8em">'
   . implode('<br />', $errors)
   . '</span>';
   $element->setLabel($label);
   return parent::getLabel();
 }
}

The name „Label“ corresponds to the settings for the ViewHelper in the decorators, and folder / name to the settings in the prefix path. Credits for this script, though, goes to Padraic Brady, where one can get a lot of good ideas of how to solve ZF problems 😉

Zend Framework comes with a lot of smart validators to use with Zend_Form. But with my goal in mind to create a page comment plugin for the relaunch of surfspot.de, I needed a validator to check if the user input contains foul language… no point in people abusing each other in the anonymity of the Internet 😉 The old version of the site uses a validator, comparing user input to a string of bad bad words I don´t want to see on my site, so I needed to make that one „Zend_Form“ compatible. Besides, over the years I added some of the most notorious spam words to the validator, so it acts as a low level spam filter as well… surfers just don´t need little blue pills to have some fun 😉

Anyway, create „JD/Form/Validator/Badwords.php“, and add the following lines:

class JD_Form_Validator_Badwords extends Zend_Validate_Abstract
{
 /**
 * Validation failure message key for when the value contains foul language
 */
 const BAD_WORDS = 'badWords';

 /**
 * Validation failure message template definitions
 *
 * @var array
 */
 protected $_messageTemplates = array(
   self::BAD_WORDS => "The text uses foul language"
 );

 /**
 * Badword array - change as required by your application
 */
 private $badwords = array('games', 'college', 'prewrcar', 'parrot', 'xx');

 /**
 * Defined by Zend_Validate_Interface
 *
 * Returns true if and only if $value does not contain bad words as defined by array
 *
 * @param  string $value
 * @return boolean
 */
 public function isValid($value)
 {
   $valueString = (string) $value;
   $this->_setValue($valueString);
   for($i=0; $i<count($this->badwords); $i++) {
     if(strstr(strtoupper($valueString), strtoupper($this->badwords[$i]))) {
       $this->_error();
       return false;
     }
   }
  return true;
 }
}

Whatever you personally consider „bad words“, add to the array, and rest assured that nobody can post those on your website. I omitted my selection out intentionally, but left some spam words in to show how it works. Make sure to add the f-word, and you can even add something like „<a href=“ to the array to avoid having links posted, but that subject will be in the „filter“ article up next.