Exemplo n.º 1
0
 public function __invoke(FormModel $model, $route)
 {
     $form = new ZendForm();
     foreach ($model->getElements() as $element) {
         $type = $element->getType();
         $name = $element->getName();
         $options = (array) Json::decode($element->getOptions());
         $form->addElement($type, $name, $options);
     }
     $form->addElement('hidden', IndexController::ID, array('value' => $model->getId()));
     $url = $this->getView()->url(array(), array('name' => $route . '/send'));
     $form->setView($this->getView())->setAction($url);
     return $form;
 }
Exemplo n.º 2
0
 public function getForm()
 {
     $form = new Form();
     $form->addElement('text', 'foo')->addElement('text', 'bar')->addElement('text', 'baz')->addElement('text', 'bat');
     $subForm = new SubForm();
     $subForm->addElement('text', 'foo')->addElement('text', 'bar')->addElement('text', 'baz')->addElement('text', 'bat');
     $form->addDisplayGroup(array('foo', 'bar'), 'foobar')->addSubForm($subForm, 'sub')->setView(new View\PhpRenderer());
     return $form;
 }
Exemplo n.º 3
0
 /**
  * @group ZF-4038
  */
 public function testCaptchaShouldRenderFullyQualifiedElementName()
 {
     $form = new Form();
     $form->addElement($this->element)->setElementsBelongTo('bar');
     $html = $form->render(new View());
     $this->assertContains('name="bar[foo', $html, $html);
     $this->assertContains('id="bar-foo-', $html, $html);
     $this->form = $form;
 }
Exemplo n.º 4
0
 public function setupForm()
 {
     $form1 = new SubForm();
     $form1->addElement('text', 'foo', array('label' => 'Sub Foo: ', 'required' => true, 'validators' => array('NotEmpty', 'Alpha')))->addElement('text', 'bar', array('label' => 'Sub Bar: ', 'required' => true, 'validators' => array('Alpha', 'Alnum')));
     $form2 = new Form();
     $form2->addElement('text', 'foo', array('label' => 'Master Foo: ', 'required' => true, 'validators' => array('NotEmpty', 'Alpha')))->addElement('text', 'bar', array('required' => true, 'validators' => array('Alpha', 'Alnum')))->addSubForm($form1, 'sub');
     $form2->isValid(array('foo' => '', 'bar' => 'foo 2 u 2', 'sub' => array('foo' => '', 'bar' => 'foo 2 u 2')));
     $form2->setView($this->getView());
     $this->decorator->setElement($form2);
     $this->form = $form2;
     return $form2;
 }
 public function addElement($element, $name = null, $options = null)
 {
     if (is_string($element)) {
         $element = strtolower($element);
         if (isset($this->customeElementDecorators[$element])) {
             $baseOptions = $this->customeElementDecorators[$element];
             if (!isset($options['decorators'])) {
                 if (isset($baseOptions['decorators'])) {
                     $decorators = $baseOptions['decorators'];
                     $options['decorators'] = isset($options['decorators']) ? array_merge($options['decorators'], $decorators) : $decorators;
                 } else {
                     $options['decorators'] = $this->customeElementDecoratorDefault;
                 }
             }
             if (isset($baseOptions['helper'])) {
                 $element = $baseOptions['helper'];
             }
             if (isset($baseOptions['options'])) {
                 foreach ($baseOptions['options'] as $key => $value) {
                     if (!isset($options[$key])) {
                         $options[$key] = null;
                     }
                     switch ($key) {
                         case 'class':
                             $options[$key] .= ' ' . $baseOptions['options'][$key];
                             break;
                         case 'rows':
                             $options[$key] = null !== $options[$key] ?: $baseOptions['options'][$key];
                             break;
                         default:
                             throw new \InvalidArgumentException('Merging option key "' . $key . '" is not defained');
                     }
                 }
             }
             return parent::addElement($element, $name, $options);
         }
     }
     return parent::addElement($element, $name, $options);
 }
Exemplo n.º 6
0
 /**
  * @group ZF-11831
  */
 public function testElementsOfSubFormReceiveCorrectDefaultTranslator()
 {
     $isEmptyKey = \Zend\Validator\NotEmpty::IS_EMPTY;
     // Global default translator
     $trDefault = new Translator(array('adapter' => 'arrayAdapter', 'content' => array($isEmptyKey => 'Default'), 'locale' => 'en'));
     Registry::set('Zend_Translate', $trDefault);
     // Translator to use for elements
     $trElement = new Translator(array('adapter' => 'arrayAdapter', 'content' => array($isEmptyKey => 'Element'), 'locale' => 'en'));
     \Zend\Validator\AbstractValidator::setDefaultTranslator($trElement);
     // Change the form's translator
     $form = new Form();
     $form->addElement(new \Zend\Form\Element\Text('foo', array('required' => true, 'validators' => array('NotEmpty'))));
     // Create a subform with it's own validator
     $sf1 = new SubForm();
     $sf1->addElement(new \Zend\Form\Element\Text('foosub', array('required' => true, 'validators' => array('NotEmpty'))));
     $form->addSubForm($sf1, 'Test1');
     $form->isValid(array());
     $messages = $form->getMessages();
     $this->assertEquals('Element', @$messages['foo'][$isEmptyKey], 'Form element received wrong validator');
     $this->assertEquals('Element', @$messages['Test1']['foosub'][$isEmptyKey], 'SubForm element received wrong validator');
 }
Exemplo n.º 7
0
 /**
  * @group ZF-2950
  */
 public function testDtDdElementsWithLabelGetUniqueId()
 {
     $form = new Form();
     $form->setView($this->getView());
     $fooElement = new \Zend\Form\Element\Text('foo');
     $fooElement->setLabel('Foo');
     $form->addElement($fooElement);
     $html = $form->render();
     $this->assertContains('<dt id="foo-label">', $html);
     $this->assertContains('<dd id="foo-element">', $html);
 }
Exemplo n.º 8
0
 /**
  * @group ZF-9682
  */
 public function testCustomLabelDecorator()
 {
     $form = new Form();
     $form->addElementPrefixPath('My\\Decorator', __DIR__ . '/../TestAsset/decorators/', 'decorator');
     $form->addElement($this->element);
     $element = $form->getElement('foo');
     $this->assertInstanceOf('My\\Decorator\\Label', $element->getDecorator('Label'));
 }