<?php
/**
* Helper for rendering mulpile form array elements.
* @see http://stackoverflow.com/questions/13157113/zf-form-array-field-how-to-display-values-in-the-view-correctly
*/
class Website_View_Helper_FormArrayElements extends Zend_View_Helper_Abstract
{
private $elements;
private $partial;
/**
* Creates a helper instance.
* @param array $arrayElements array of elements that are used in the given partial script; they must be an array fields
* @param string $partial script to render for each value in the given form array elements
* @return Website_View_Helper_FormArrayElements
*/
public function formArrayElements
(array $arrayElements, $partial){ // check some preconditions
if(count($arrayElements) == 0) throw InvalidArgumentException('Elements array must contain at least one element.');
foreach($arrayElements as $element){
if(!$element instanceof Zend_Form_Element)
throw InvalidArgumentException
('Elements of class ' . get_class($element) . ' is not a form element.'); /* @var $element Zend_Form_Element */
if(!$element->isArray())
throw InvalidArgumentException('Element [' . $element->getName() . '] is not an array.');
}
$this->elements = $arrayElements;
$this->partial = $partial;
return $this;
}
/**
* Render the fields.
*/
public function render(){
// only if the form were submitted we need to validate fields' values
// and display errors next to them; otherwise when user enter the page
// and render the form for the first time - he would see Required validator errors
$needsValidation = false;
foreach($this->elements as $element){
$v = $element->getValue();
$needsValidation = true;
$values[$element->getName()] = $v;
}
else{
// print empty fields when the form is displayed the first time
$values[$element->getName()] = array(''); }
}
// iterate over all values
$rendered = '';
foreach($this->elements as $element){
$currentValue = $values[$element->getName()][$valueKey];
if($needsValidation)
$element->isValid($currentValue);
$elements[$element->getName()] = (string)$element->setValue($currentValue);
}
$rendered .= $this->view->partial($this->partial, $elements);
}
return $rendered;
}
public function __toString(){
return $this->render();
}
}