And here is an example for the "viewPattern" from James:
// Decorator
class Decorator_ViewPattern extends Zend_Form_Decorator_Abstract
{
/**
* Default placement: null
* @var string
*/
protected $_placement = null;
/**
* Pattern
*
* @var string
*/
protected $_pattern;
/**
* Set pattern
*
* @param string $pattern
* @return Decorator_ViewPattern
* @throws Zend_Form_Exception
*/
public function setPattern($pattern)
{
if (!is_string($pattern)) {
require_once 'Zend/Form/Exception.php';
throw new Zend_Form_Exception('Invalid pattern provided to
pattern decorator');
}
$this->_pattern = $pattern;
return $this;
}
/**
* Get pattern
*
* If not previously registered, checks to see if it exists in
registered
* options.
*
* @return null|string
*/
public function getPattern()
{
if (null === $this->_pattern) {
if (null !== ($pattern = $this->getOption('pattern'))) {
$this->setPattern($pattern);
$this->removeOption('pattern');
}
}
return $this->_pattern;
}
/**
* Render a view pattern (replace placeholders)
*
* @param string $content
* @return string
*/
public function render($content)
{
$pattern = $this->getPattern();
if (null === $pattern) {
return $content;
}
$element = $this->getElement();
$placement = $this->getPlacement();
$separator = $this->getSeparator();
// View helper decorator
if (strpos($pattern, '{field}')) {
if (false === $element->getDecorator('ViewHelper')) {
$element->addDecorator('ViewHelper');
}
$viewHelper = $element->getDecorator('ViewHelper');
$viewHelper->setElement($element);
// Replace element
$pattern = str_replace('{field}', $viewHelper->render(''),
$pattern);
}
// Errors decorator
if (strpos($pattern, '{errors}')) {
if (false === $element->getDecorator('Errors')) {
$element->addDecorator('Errors');
}
$errorsDecorator = $element->getDecorator('Errors');
$errorsDecorator->setElement($element);
// Replace errors
$pattern = str_replace('{errors}',
$errorsDecorator->render(''), $pattern);
}
// Description decorator
if (strpos($pattern, '{description}')) {
if (false === $element->getDecorator('Description')) {
$element->addDecorator('Description');
}
$descriptionDecorator = $element->getDecorator('Description');
$descriptionDecorator->setElement($element);
// Replace description
$pattern = str_replace('{description}',
$descriptionDecorator->render(''), $pattern);
}
// Label decorator
if (strpos($pattern, '{label}')) {
if (false === $element->getDecorator('Label')) {
$element->addDecorator('Label');
}
$labelDecorator = $element->getDecorator('Label');
$labelDecorator->setElement($element);
// Replace label
$pattern = str_replace('{label}', $labelDecorator->render(''),
$pattern);
}
switch ($placement) {
case self::APPEND:
return $content . $separator . $pattern;
case self::PREPEND:
return $pattern . $separator . $content;
default:
// replace content
return $pattern;
}
}
}
// Form
$form = new Zend_Form();
$form->setDecorators(array(
'FormElements',
'Form',
));
// Create decorator
$decorator = new Decorator_ViewPattern();
$decorator->setPattern('<div>{label}</div><div>{field}<br
/>{description}</div>');
// Add element
$form->addElement(
'text',
'foo',
array(
'label' => '<strong>Foo</strong>',
'description' => 'Foobar...',
'decorators' => array(
array('Description', array('class' => 'notice')),
array('Label', array('escape' => false)),
$decorator,
),
)
);
// View
$view = new Zend_View();
$view->setEncoding('utf-8');
$view->doctype('XHTML1_STRICT');
// Render
echo $form->render($view);
/*
Output:
<form enctype="application/x-www-form-urlencoded" action="" method="post">
<div>
<label for="foo" class="optional"><strong>Foo</strong></label>
</div>
<div>
<input type="text" name="foo" id="foo" value="" /><br />
<p class="notice">Foobar...</p>
</div>
</form>
*/
--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]