Skip to content

Instantly share code, notes, and snippets.

@neilime
Created February 18, 2013 13:42
Show Gist options
  • Save neilime/4977484 to your computer and use it in GitHub Desktop.
Save neilime/4977484 to your computer and use it in GitHub Desktop.
Exemple overriding FormRow Helper for adding "required" class to element's label if it has required attribute
<?php
/**
* This class ovveride FormRow view helper
* The file should be in "module/Application/src/Application/Form/View/Helper" directory
*/
namespace Application\Form\View\Helper;
class ExempleFormRow extends \TwbBundle\Form\View\Helper\TwbBundleFormRow{
public function render(\Zend\Form\ElementInterface $oElement){
if($oElement->getAttribute('required') === true){
$aLabelAttributes = $oElement->getLabelAttributes()?:$this->labelAttributes;
if(empty($aLabelAttributes['class']))$aLabelAttributes['class'] = 'required';
elseif(strpos($aLabelAttributes['class'], 'required') === false)$aLabelAttributes['class'] .= ' required';
$oElement->setLabelAttributes($aLabelAttributes);
}
return parent::render($oElement);
}
}
<?php
//Config for Application module
return array(
//...
'view_helpers' => array(
'invokables' => array(
//Define formRow service
'formRow' => 'Application\Form\View\Helper\ExempleFormRow',
)
)
//...
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment