/** * Create a form * * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return \Zend\Form\Form */ public function createService(ServiceLocatorInterface $serviceLocator) { $formFactory = new \Zend\Form\Factory(); $form = $formFactory->createForm(array('elements' => [['spec' => ['type' => \Zend\Form\Element\Text::class, 'name' => 'name', 'options' => array('label' => 'Name'), 'attributes' => ['class' => 'form-control']]], ['spec' => ['type' => \Zend\Form\Element\Text::class, 'name' => 'phone', 'options' => array('label' => 'Phone'), 'attributes' => array('type' => 'tel', 'class' => 'form-control')]], ['spec' => ['type' => \Zend\Form\Element\Radio::class, 'name' => 'gender', 'options' => ['label' => 'Gender', 'value_options' => \Application\Entity\Teacher::$gender_text, 'label_attributes' => ['class' => 'radio_button']], 'attributes' => ['class' => 'form-control']]], ['spec' => ['type' => \Zend\Form\Element\Csrf::class, 'name' => 'security']]], 'input_filter' => ['name' => ['required' => true, 'filters' => [['name' => \Zend\Filter\StringTrim::class]]], 'phone' => ['required' => true, 'filters' => [['name' => \Zend\Filter\StringTrim::class]]]])); $form->setAttribute('class', 'form-horizontal'); return $form; }
/** * Create a form * * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return \Zend\Form\Form */ public function createService(ServiceLocatorInterface $serviceLocator) { $formFactory = new \Zend\Form\Factory(); $form = $formFactory->createForm(array('elements' => [['spec' => ['type' => \Zend\Form\Element\Text::class, 'name' => 'name', 'options' => array('label' => 'Name'), 'attributes' => ['class' => 'form-control']]], ['spec' => ['type' => \Zend\Form\Element\Email::class, 'name' => 'email', 'options' => ['label' => 'Email address'], 'attributes' => ['class' => 'form-control']]], ['spec' => ['type' => \Zend\Form\Element\Date::class, 'name' => 'birthday', 'options' => ['label' => 'Birthday'], 'attributes' => ['class' => 'form-control datepicker']]], ['spec' => ['type' => \Zend\Form\Element\Radio::class, 'name' => 'level', 'options' => ['label' => 'Level', 'label_attributes' => ['class' => 'radio_button'], 'value_options' => \Application\Entity\Pupil::$level_text], 'attributes' => ['class' => 'form-control']]], ['spec' => ['type' => \Zend\Form\Element\Csrf::class, 'name' => 'security']]], 'input_filter' => ['name' => array('required' => true, 'filters' => [['name' => \Zend\Filter\StringTrim::class]]), 'email' => ['required' => true, 'filters' => [['name' => \Zend\Filter\StringTrim::class]], 'validators' => [new \Zend\Validator\EmailAddress()]], 'birthday' => ['required' => true], 'level' => ['required' => true]])); $form->setAttribute('class', 'form-horizontal'); return $form; }
/** * @param \Zend\Form\Element $element * @param string $typeOverride * @return string */ public function __invoke(Element $element, $typeOverride = '') { if (is_string($typeOverride) && !empty($typeOverride)) { $factory = new \Zend\Form\Factory(); $element = $factory->createElement(array('name' => $element->getName(), 'type' => $typeOverride, 'value' => $element->getValue(), 'attributes' => $element->getAttributes(), 'options' => $element->getOptions())); } $decorator = new ViewModel(array('label' => $this->renderLabel($element), 'element' => $this->renderElement($element))); $decorator->setTemplate('arc/bootstrap/control-group'); return $this->getView()->render($decorator); }
/** * Render buttons markup * @param array $aButtons * @return string */ protected function renderButtons(array $aButtons, $bJustified = false) { $sMarkup = ''; foreach ($aButtons as $oButton) { if (is_array($oButton) || $oButton instanceof \Traversable && !$oButton instanceof \Zend\Form\ElementInterface) { $oFactory = new \Zend\Form\Factory(); $oButton = $oFactory->create($oButton); } elseif (!$oButton instanceof \Zend\Form\ElementInterface) { throw new \LogicException(sprintf('Button expects an instanceof \\Zend\\Form\\ElementInterface or an array / \\Traversable, "%s" given', is_object($oButton) ? get_class($oButton) : gettype($oButton))); } $sButtonMarkup = $this->getFormElementHelper()->__invoke($oButton); $sMarkup .= $bJustified ? sprintf(self::$buttonGroupJustifiedFormat, $sButtonMarkup) : $sButtonMarkup; } return $sMarkup; }
/** * * @param array $formConfig * @return \Zend\Form\Factory */ protected function createFormFromConfig(array $formConfig) { $Ofactory = new \Zend\Form\Factory(); $form = $Ofactory->create($formConfig); $form->setAttribute("class", "form-horizontal"); $form->setAttribute("id", "crud"); /*if($this->getFrmFilterCrud() instanceof \Zend\InputFilter\InputFilterInterface){ $form->setInputFilter($this->getFrmFilterCrud()); }*/ $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Save', 'id' => 'submitbutton'))); $form->add(array('name' => 'cancel', 'attributes' => array('type' => 'button', 'value' => 'Cancel', 'id' => 'cancelbutton'))); return $form; }
/** * Render addo-on markup * @param string $aAddOnOptions * @throws \InvalidArgumentException * @throws \LogicException * @return string */ protected function renderAddOn($aAddOnOptions) { if (empty($aAddOnOptions)) { throw new \InvalidArgumentException('Addon options are empty'); } if ($aAddOnOptions instanceof \Zend\Form\ElementInterface) { $aAddOnOptions = array('element' => $aAddOnOptions); } elseif (is_scalar($aAddOnOptions)) { $aAddOnOptions = array('text' => $aAddOnOptions); } elseif (!is_array($aAddOnOptions)) { throw new \InvalidArgumentException('Addon options expects an array or a scalar value, "' . gettype($aAddOnOptions) . '" given'); } $sMarkup = ''; $sAddonTagName = 'span'; $sAddonClass = ''; if (!empty($aAddOnOptions['text'])) { if (!is_scalar($aAddOnOptions['text'])) { throw new \LogicException('"text" option expects a scalar value, "' . gettype($aAddOnOptions['text']) . '" given'); } elseif ($oTranslator = $this->getTranslator()) { $sMarkup .= $oTranslator->translate($aAddOnOptions['text'], $this->getTranslatorTextDomain()); } else { $sMarkup .= $aAddOnOptions['text']; } $sAddonClass .= ' input-group-addon'; } elseif (!empty($aAddOnOptions['element'])) { if (is_array($aAddOnOptions['element']) || $aAddOnOptions['element'] instanceof \Traversable && !$aAddOnOptions['element'] instanceof \Zend\Form\ElementInterface) { $oFactory = new \Zend\Form\Factory(); $aAddOnOptions['element'] = $oFactory->create($aAddOnOptions['element']); } elseif (!$aAddOnOptions['element'] instanceof \Zend\Form\ElementInterface) { throw new \LogicException(sprintf('"element" option expects an instanceof \\Zend\\Form\\ElementInterface, "%s" given', is_object($aAddOnOptions['element']) ? get_class($aAddOnOptions['element']) : gettype($aAddOnOptions['element']))); } $aAddOnOptions['element']->setOptions(array_merge($aAddOnOptions['element']->getOptions(), array('disable-twb' => true))); $sMarkup .= $this->render($aAddOnOptions['element']); if ($aAddOnOptions['element'] instanceof \Zend\Form\Element\Button) { $sAddonClass .= ' input-group-btn'; //Element contains dropdown, so add-on container must be a "div" if ($aAddOnOptions['element']->getOption('dropdown')) { $sAddonTagName = 'div'; } } else { $sAddonClass .= ' input-group-addon'; } } return sprintf(self::$addonFormat, $sAddonTagName, trim($sAddonClass), $sMarkup, $sAddonTagName); }
<?php require_once './autoloader.php'; $config = array('name' => 'codeType', 'attributes' => array('type' => 'radio', 'label' => 'Code Type', 'options' => array('Markdown' => 'markdown', 'HTML' => 'html', 'Wiki' => 'wiki'), 'value' => array('markdown'))); $factory = new Zend\Form\Factory(); $element = $factory->create($config); $helper = new Zend\Form\View\Helper\FormMultiCheckbox(); echo $helper->render($element);