Hi everyone, I'm struggling to create a composite form element in ZF2 based on this ZF1 tutorial <http://www.mwop.net/blog/217-Creating-composite-elements.html> .
I've created my element in a custom library called 'Si': namespace Si\Form\Element;use Zend\Form\Element;class DateSegmented extends Element{ protected $_dateFormat = '%year%-%month%-%day%'; protected $_day; protected $_month; protected $_year; /** * Seed attributes * * @var array */ protected $attributes = array( 'type' => 'datesegmented', ); public function setDay($value) { $this->_day = (int) $value; return $this; } public function getDay() { return $this->_day; } public function setMonth($value) { $this->_month = (int) $value; return $this; } public function getMonth() { return $this->_month; } public function setYear($value) { $this->_year = (int) $value; return $this; } public function getYear() { return $this->_year; } public function setValue($value) { if (is_int($value)) { $this->setDay(date('d', $value)) ->setMonth(date('m', $value)) ->setYear(date('Y', $value)); } elseif (is_string($value)) { $date = strtotime($value); $this->setDay(date('d', $date)) ->setMonth(date('m', $date)) ->setYear(date('Y', $date)); } elseif (is_array($value) && (isset($value['day']) && isset($value['month']) && isset($value['year']) ) ) { $this->setDay($value['day']) ->setMonth($value['month']) ->setYear($value['year']); } else { throw new Exception('Invalid date value provided'); } return $this; } public function getValue() { return str_replace( array('%year%', '%month%', '%day%'), array($this->getYear(), $this->getMonth(), $this->getDay()), $this->_dateFormat ); }} And then the view helper: namespace Si\Form\View\Helper;use Zend\Form\View\Helper\FormElement;use Zend\Form\Element;use Zend\Form\ElementInterface;use Zend\Form\Exception;class DateSegmented extends FormElement{ /** * Render a form input> element from the provided $element * * @param ElementInterface $element * @throws Exception\InvalidArgumentException * @throws Exception\DomainException * @return string */ public function render(ElementInterface $element) { $content = ""; if (!$element instanceof \Si\Form\Element\DateSegmented) { throw new Exception\InvalidArgumentException(sprintf( '%s requires that the element is of type Si\Form\Input\DateSegmented', __METHOD__ )); } $name = $element->getName(); if (empty($name) && $name !== 0) { throw new Exception\DomainException(sprintf( '%s requires that the element has an assigned name; none discovered', __METHOD__ )); } $day = $element->getDay(); $month = $element->getMonth(); $year = $element->getYear(); $params = array( 'size' => 2, 'maxlength' => 2 ); $yearParams = array( 'size' => 4, 'maxlength' => 4, ); $markup = ""; $elementText = new \Zend\Form\Element\Text($name . '[day]', $params); $elementText->setValue($day); $markup .= $this->view->formText($elementText); $elementText = new \Zend\Form\Element\Text($name . '[month]', $params); $elementText->setValue($month); $markup .= $this->view->formText($elementText); $elementText = new \Zend\Form\Element\Text($name . '[year]', $yearParams); $elementText->setValue($year); $markup .= $this->view->formText($elementText); return $markup; } /** * Return input type * * @return string */ protected function getInputType() { return 'datesegmented'; } } The element has been set as invokable in my custom library 'HelperConfig.php' file. namespace Si\Form\View;use Zend\ServiceManager\ConfigInterface;use Zend\ServiceManager\ServiceManager;class HelperConfig implements ConfigInterface{ /** * @var array Pre-aliased view helpers */ protected $invokables = array( 'datesegmented' => '\Si\Form\View\Helper\DateSegmented', ); /** * Configure the provided service manager instance with the configuration * in this class. * * Adds the invokables defined in this class to the SM managing helpers. * * @param ServiceManager $serviceManager * @return void */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $service) { $serviceManager->setInvokableClass($name, $service); } }} The above code generated the three input fields and also returns them as an array in the raw POST. If I run 'getValue()' against the element, I get the correct value. Unfortunately, the validator still seems to get the old array (which at this point has been flattened into a string called 'Array') and fails. In ZF1, you would overrite the 'getValue' function to work with validators. What do I need to do for my adjusted value to be picked up by the validators in ZF2?Thanks!Oli -- View this message in context: http://zend-framework-community.634137.n4.nabble.com/Creating-composite-form-elements-tp4658802.html Sent from the Zend Framework mailing list archive at Nabble.com.