예제 #1
0
 /**
  * @param array $spec
  * @return \Zend\Form\FormInterface
  */
 protected function buildForm(array $spec)
 {
     if (!$this->formFactory) {
         $this->formFactory = new Factory();
     }
     return $this->formFactory->create($spec);
 }
예제 #2
0
파일: Factory.php 프로젝트: Andyyang1981/pi
 /**
  * Create a form or form element
  *
  * @{inheritdoc}
  */
 public function create($spec)
 {
     // Canonize type
     if (isset($spec['type']) && is_string($spec['type']) && false === strpos($spec['type'], '\\')) {
         $type = strtolower($spec['type']);
         if ($type == 'form' || $type == 'fieldset') {
             $spec['type'] = sprintf('%s\\%s', __NAMESPACE__, ucfirst($type));
         } else {
             $canonizedType = str_replace(' ', '', ucwords(str_replace(array('_', '-'), ' ', $spec['type'])));
             $type = sprintf('%s\\Element\\%s', __NAMESPACE__, $canonizedType);
             if (class_exists($type)) {
                 $spec['type'] = $type;
             } else {
                 $type = sprintf('Zend\\Form\\Element\\%s', $canonizedType);
                 if (class_exists($type)) {
                     $spec['type'] = $type;
                 } else {
                     if (!isset($spec['attributes']['type'])) {
                         $spec['attributes']['type'] = $spec['type'];
                     }
                     unset($spec['type']);
                 }
             }
         }
     }
     return parent::create($spec);
 }
예제 #3
0
 public function testCanCreateElementUsingCreate()
 {
     $element = $this->factory->create(array('name' => 'foo', 'attributes' => array('type' => 'text', 'class' => 'foo-class', 'data-js-type' => 'my.form.text')));
     $this->assertInstanceOf('Zend\\Form\\ElementInterface', $element);
     $this->assertEquals('foo', $element->getName());
     $this->assertEquals('text', $element->getAttribute('type'));
     $this->assertEquals('foo-class', $element->getAttribute('class'));
     $this->assertEquals('my.form.text', $element->getAttribute('data-js-type'));
 }
예제 #4
0
 /**
  * Creates form for a  test
  * @param string $id
  * @return \Zend\Form\Form
  */
 public function createForm($id)
 {
     $data = $this->get($id);
     $spec = json_decode($data['definition'], true);
     if (!$spec) {
         throw new \Exception('Invalid form specification');
     }
     $factory = new FormFactory();
     return $factory->create($spec);
 }
 /**
  * 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 ElementInterface) {
             $oFactory = new Factory();
             $oButton = $oFactory->create($oButton);
         } elseif (!$oButton instanceof 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;
 }
 /**
  * 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 ElementInterface) {
         $aAddOnOptions = array('element' => $aAddOnOptions);
     } elseif (is_scalar($aAddOnOptions)) {
         $aAddOnOptions = array('text' => $aAddOnOptions);
     } elseif (!is_array($aAddOnOptions)) {
         throw new InvalidArgumentException(sprintf('Addon options expects an array or a scalar value, "%s" given', is_object($aAddOnOptions) ? get_class($aAddOnOptions) : gettype($aAddOnOptions)));
     }
     $sMarkup = '';
     $sAddonTagName = 'span';
     $sAddonClass = '';
     if (!empty($aAddOnOptions['text'])) {
         if (!is_scalar($aAddOnOptions['text'])) {
             throw new InvalidArgumentException(sprintf('"text" option expects a scalar value, "%s" given', is_object($aAddOnOptions['text']) ? get_class($aAddOnOptions['text']) : gettype($aAddOnOptions['text'])));
         } 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 ElementInterface) {
             $oFactory = new Factory();
             $aAddOnOptions['element'] = $oFactory->create($aAddOnOptions['element']);
         } elseif (!$aAddOnOptions['element'] instanceof 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-bootstrap' => true)));
         $sMarkup .= $this->render($aAddOnOptions['element']);
         //Element is a button, so add-on container must be a "div"
         if ($aAddOnOptions['element'] instanceof Button) {
             $sAddonClass .= ' input-group-btn';
             $sAddonTagName = 'div';
         } else {
             $sAddonClass .= ' input-group-addon';
         }
     }
     return sprintf(self::$addonFormat, $sAddonTagName, trim($sAddonClass), $sMarkup, $sAddonTagName);
 }
예제 #7
0
파일: Factory.php 프로젝트: gridguyz/zork
 /**
  * Create an element, fieldset, or form
  *
  * Introspects the 'type' key of the provided $spec, and determines what
  * type is being requested; if none is provided, assumes the spec
  * represents simply an element.
  *
  * @param  array|Traversable $spec
  * @return ElementInterface
  * @throws Exception\DomainException
  */
 public function create($spec)
 {
     $element = parent::create($spec);
     if ($element instanceof ServiceLocatorAwareInterface) {
         $element->setServiceLocator($this->getServiceLocator());
     }
     if ($element instanceof FieldsetInterface) {
         $element->setFormFactory($this);
     }
     if ($element instanceof PrepareElementsAwareInterface) {
         $element->prepareElements();
     }
     return $element;
 }