<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Decorators | Jörg Drzycimski</title>
	<atom:link href="https://drzycimski.com/tag/decorators/feed/" rel="self" type="application/rss+xml" />
	<link>https://drzycimski.com/tag/decorators/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Oct 2017 16:51:51 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>No-MVC Zend Framework: Zend_Decorator and Zend_Validator</title>
		<link>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_decorator-and-zend_validator/</link>
					<comments>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_decorator-and-zend_validator/#respond</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Mon, 07 Feb 2011 18:20:51 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[comment spam blocker]]></category>
		<category><![CDATA[Decorators]]></category>
		<category><![CDATA[spam filter]]></category>
		<category><![CDATA[Validators]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<guid isPermaLink="false">http://www.drzycimski.com/?p=133</guid>

					<description><![CDATA[<p>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 &#8222;Form&#8220; subfolder, create a folder &#8222;Decorator&#8220;, and in it, a file [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_decorator-and-zend_validator/">No-MVC Zend Framework: Zend_Decorator and Zend_Validator</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>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 &#8222;Form&#8220; subfolder, create a folder &#8222;Decorator&#8220;, and in it, a file named &#8222;Label.php&#8220;:</p>
<pre class="brush:php">class JD_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
 public function getLabel()
 {
   $element = $this-&gt;getElement();
   $label = trim($element-&gt;getLabel());
   if ($element-&gt;isRequired()) {
     $label .= ' *';
   }
   $errors = $element-&gt;getMessages();
   if (empty($errors)) {
     $element-&gt;setLabel($label);
     return parent::getLabel();
   }
   $label .= '&lt;br /&gt;&lt;span style="color:red;font-size:0.8em"&gt;'
   . implode('&lt;br /&gt;', $errors)
   . '&lt;/span&gt;';
   $element-&gt;setLabel($label);
   return parent::getLabel();
 }
}</pre>
<p>The name &#8222;Label&#8220; 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 <a title="Padraic Brady" href="http://blog.astrumfutura.com/" target="_blank" rel="noopener">Padraic Brady</a>, where one can get a lot of good ideas of how to solve ZF problems 😉</p>
<p>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 <a title="surfspot.de" href="http://www.surfspot.de" target="_blank" rel="noopener">surfspot.de</a>, I needed a validator to check if the user input contains foul language&#8230; 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 &#8222;Zend_Form&#8220; 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&#8230; surfers just don´t need little blue pills to have some fun 😉</p>
<p>Anyway, create &#8222;JD/Form/Validator/Badwords.php&#8220;, and add the following lines:</p>
<pre class="brush:php">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 =&gt; "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-&gt;_setValue($valueString);
   for($i=0; $i&lt;count($this-&gt;badwords); $i++) {
     if(strstr(strtoupper($valueString), strtoupper($this-&gt;badwords[$i]))) {
       $this-&gt;_error();
       return false;
     }
   }
  return true;
 }
}</pre>
<p>Whatever you personally consider &#8222;bad words&#8220;, 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 &#8222;<em>&lt;a href=</em>&#8220; to the array to avoid having links posted, but that subject will be in the &#8222;filter&#8220; article up next.</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_decorator-and-zend_validator/">No-MVC Zend Framework: Zend_Decorator and Zend_Validator</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_decorator-and-zend_validator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>No-MVC Zend Framework: Zend_Forms, Decorator, Validator and Filters</title>
		<link>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_forms-decorator-validator-and-filters/</link>
					<comments>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_forms-decorator-validator-and-filters/#respond</comments>
		
		<dc:creator><![CDATA[Jörg]]></dc:creator>
		<pubDate>Sat, 05 Feb 2011 18:49:27 +0000</pubDate>
				<category><![CDATA[Programmierung]]></category>
		<category><![CDATA[autoloader]]></category>
		<category><![CDATA[bootstrap]]></category>
		<category><![CDATA[Controller]]></category>
		<category><![CDATA[Decorators]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[View]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Form]]></category>
		<guid isPermaLink="false">http://www.drzycimski.com/?p=127</guid>

					<description><![CDATA[<p>Managing Zend_Form in both MVC and no-MVC environments has a huge advantage, since Zend Framework comes with a load of decorators (HTML to display form fields), validators (functions to check user input) and filters (functions to filter user input). If you look for an easy way to handle all kinds of user input thru forms, [&#8230;]</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_forms-decorator-validator-and-filters/">No-MVC Zend Framework: Zend_Forms, Decorator, Validator and Filters</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Managing Zend_Form in both MVC and no-MVC environments has a huge advantage, since Zend Framework comes with a load of decorators (HTML to display form fields), validators (functions to check user input) and filters (functions to filter user input). If you look for an easy way to handle all kinds of user input thru forms, ZF is the way to go&#8230; at least if you have a lot of different forms on you website, that is 😉</p>
<p>This articles is based on the first parts of the Star Rating series (which I still have to finish&#8230;), so it assumes you have managed to install ZF on your system, have your bootstrap.php up and running. Pls note: I changed the naming from &#8222;Mylib&#8220; to &#8222;JD&#8220;&#8230; using my initials is easier because I can copy&amp;paste my classes 😉 Just in case: here´s the bootstrap.php that you have to include, already with some new code:</p>
<pre class="brush:php">&lt;?php
// Define path to application directory
defined('APPLICATION_PATH')
 || define('APPLICATION_PATH', ($_SERVER['DOCUMENT_ROOT']));

// Define application environment
defined('APPLICATION_ENV')
 || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
 realpath(APPLICATION_PATH . '/../library'),
 get_include_path(),
)));

/**
 * General PHP settings
 */
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
ini_set('default_charset', 'UTF-8');

/**
 * Autoloader
 */
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// Register folder library/JD/ for own classes
$autoloader-&gt;registerNamespace('JD_');

/**
 * Load Zend Config from application.ini
 */
$config = new Zend_Config_Ini($_SERVER['DOCUMENT_ROOT'] . '/../library/JD/configs/application.ini', APPLICATION_ENV);
Zend_Registry::set('config', $config);

/**
 * Connect to default DB
 */
$db = Zend_Db::factory($config-&gt;database);
Zend_Db_Table::setDefaultAdapter($db);

/**
 * Get Zend View object (required for any view helpers, e.g. Form, Navigation)
 */
$view = new Zend_View;
$view-&gt;setEncoding('UTF-8');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
// Doctype for Form Renderer
$doctypeHelper = new Zend_View_Helper_Doctype();
$doctypeHelper-&gt;doctype('XHTML1_TRANSITIONAL');

/**
 * Set default language for form errors etc
 */
$translate = new Zend_Translate('array', $_SERVER['DOCUMENT_ROOT'] . '/../library/JD/Languages/de.php', $config-&gt;language-&gt;default);
Zend_Form::setDefaultTranslator($translate);
</pre>
<p>In order to be able to use Zend_Form, you need to set the Zend_View, else it would not render it. The language part is optional, it´s a simple file replacing English error messages with (in this case) German ones. Refer to the ZF manual for language details.</p>
<p>The next part is to set some rendering options, A.K.A decorators. With most websites, I use <a title="YAML CSS framework" href="http://www.yaml.de/" target="_blank" rel="noopener">YAML</a> as a CSS framework, and ZF renders form elements into dt/dd tags (didn´t know anyone uses those anymore&#8230;). Anyway, re-decorating a form is a piece of cake. Create a subfolder &#8222;Form&#8220; in your own &#8222;library&#8220; subfolder (mine is &#8222;JD&#8220; was &#8222;Mylib&#8220;), and create a form controller, named &#8222;Controller.php&#8220;.  If you´ve read the previous articles, you can guess the name of the class&#8230; JD_Form_Controller. Replace the &#8222;JD&#8220; part with anything you like, according to your directory structure (e.g. &#8222;library/somename/Form/Controller&#8220; translates to class name &#8222;somename_Form_Controller&#8230; just basic autoloading knowledge). Here´s what the controller looks like:</p>
<pre class="brush:php">class JD_Form_Controller extends Zend_Form
{
 // COLUMNAR YFORM decorators

 // standard input type=text &amp; textareas
 protected $_standardTextDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-text')),
   // not used
   array('Description')
 );
 // standard input type=select
 protected $_standardSelectDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-select')),
   // not used
   array('Description')
 );
 // standard checkbox type=checkbox
 protected $_standardCheckboxDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-check')),
   // not used
   array('Description')
 );
 // special input type=captcha -&gt; into type-text for styling
 protected $_standardCaptchaDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-text')),
   // not used
   array('Description')
 );
 // standard input type=hidden w/out tags
 protected $_standardHiddenDecorator = array(
   'ViewHelper'
 );
 protected $_standardButtonDecorator = array(
   'ViewHelper',
   array('HtmlTag', array('class'=&gt;'type-button'))
 );

 // Standard YFORM decorators
 protected $_leftTextDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-textleft'))
 );
 protected $_fullTextDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-textfull'))
 );
 protected $_leftCheckDecorator = array(
   'ViewHelper',
   array('Label', array('escape'=&gt;false)),
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-checkleft'))
 );
 protected $_leftButtonDecorator = array(
   'ViewHelper',
   array('HtmlTag', array('tag'=&gt;'div', 'class'=&gt;'type-buttonleft'))
 );

 /**
 * Class constructor
 *
 * @param array $options
 */
 public function __construct($options = null)
 {        
   // path setting for custom classes MUST ALWAYS be first!
   $this-&gt;addElementPrefixPath('JD_Form_Decorator','JD/Form/Decorator','decorator');
   $this-&gt;addElementPrefixPath('JD_Form_Validator','JD/Form/Validator','validate');
   $this-&gt;addElementPrefixPath('JD_Filter','JD/Filter','filter');
   parent::__construct($options);
   $this-&gt;setAttrib('class', 'yform columnar');
   $this-&gt;setDecorators(array(
     'FormElements',
     'Form'
   ));
 }    
}
</pre>
<p>This is a lot at once, but I already set everything up for decorators (defining them and adding the prefix path), validators (still to be defined, but prefixed) and filters (also not defined yet, but prefixed). By default, I use columnar forms on the website (label next to form field: all $_standard* decorators), but also have an option to use labels above their fields ($_left or $_full). E.g. the $_standardTextDecorator wraps an INPUT element, inluding the label, into a DIV with the class &#8222;type-text&#8220;, and wraps the label into a new decorator &#8211; one that appends an * to each required input field.</p>
<p>Next up: Zend_Decorator and Zend_Validator</p>
<p>The post <a href="https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_forms-decorator-validator-and-filters/">No-MVC Zend Framework: Zend_Forms, Decorator, Validator and Filters</a> appeared first on <a href="https://drzycimski.com">Jörg Drzycimski</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://drzycimski.com/programmierung/no-mvc-zend-framework-zend_forms-decorator-validator-and-filters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
