Beispiel #1
0
 public function testPassingElementToOpenTagWillUseIdInForAttributeWhenPresent()
 {
     $element = new Element('foo');
     $element->setAttribute('id', 'bar');
     $markup = $this->helper->openTag($element);
     $this->assertContains('for="bar"', $markup);
 }
Beispiel #2
0
 public function __construct()
 {
     parent::__construct('nested_fieldset');
     $field = new Element('anotherField', array('label' => 'Name'));
     $field->setAttribute('type', 'text');
     $this->add($field);
 }
Beispiel #3
0
 public function testCanCreateMarkupWithoutLabel()
 {
     $element = new Element('foo');
     $element->setAttribute('type', 'text');
     $markup = $this->helper->render($element);
     $this->assertRegexp('/<input name="foo" type="text"(\\s*\\/)?>/', $markup);
 }
Beispiel #4
0
 public function getElement()
 {
     $element = new Element('foo');
     $options = array('This is the first label' => 'value1', 'This is the second label' => 'value2', 'This is the third label' => 'value3');
     $element->setAttribute('options', $options);
     return $element;
 }
 public function testRendersValueAttributeAsTextareaContent()
 {
     $element = new Element('foo');
     $element->setAttribute('value', 'Initial content');
     $markup = $this->helper->render($element);
     $this->assertContains('>Initial content</textarea>', $markup);
 }
Beispiel #6
0
 /**
  * Set a single element attribute
  * 
  * @param  string $key 
  * @param  mixed $value 
  * @return Element
  */
 public function setAttribute($key, $value)
 {
     if ('captcha' == strtolower($key)) {
         $this->setCaptcha($value);
         return $this;
     }
     return parent::setAttribute($key, $value);
 }
Beispiel #7
0
 public function testSetUseHiddenElementAttributeDoesNotRenderHiddenInput()
 {
     $element = new Element('foo');
     $element->setAttribute('useHiddenElement', false);
     $markup = $this->helper->render($element);
     $this->assertRegexp('#type="checkbox"\\s+value="1"#', $markup);
     $this->assertNotRegexp('#type="hidden"\\s+name="foo"\\s+value="0"#', $markup);
 }
Beispiel #8
0
 /**
  * Override: set a single element attribute
  *
  * Does not allow setting value attribute; this will always be
  * retrieved from the validator.
  * 
  * @param  string $name 
  * @param  mixed $value 
  * @return Csrf
  */
 public function setAttribute($name, $value)
 {
     if ('value' == $name) {
         // Do not allow setting this
         return;
     }
     return parent::setAttribute($name, $value);
 }
Beispiel #9
0
 public function testRendersOnErrorAsEspected()
 {
     $element = new Element('testElement');
     $element->setAttribute('value', 'Initial Content');
     $element->setMessages(array('testElement' => 'no valid'));
     $markup = $this->helper->render($element);
     $this->assertContains('error', $markup);
 }
Beispiel #10
0
 public function testGeneratesEmailInputTagRegardlessOfElementType()
 {
     $element = new Element('foo');
     $element->setAttribute('type', 'radio');
     $markup = $this->helper->render($element);
     $this->assertContains('<input ', $markup);
     $this->assertContains('type="email"', $markup);
 }
Beispiel #11
0
 /**
  * @dataProvider inputTypes
  */
 public function testOpenTagOnlyAllowsValidButtonTypes($type, $assertion)
 {
     $element = new Element('foo');
     $element->setAttribute('type', $type);
     $markup = $this->helper->openTag($element);
     $expected = sprintf('type="%s"', $type);
     $this->{$assertion}($expected, $markup);
 }
Beispiel #12
0
 public function __construct()
 {
     parent::__construct('basic_fieldset');
     $field = new Element('field', array('label' => 'Name'));
     $field->setAttribute('type', 'text');
     $this->add($field);
     $nestedFieldset = new NestedFieldset();
     $this->add($nestedFieldset);
 }
Beispiel #13
0
 public function testInvokeProxiesToRender()
 {
     $element = new Element('foo');
     $element->setAttribute('src', 'foo.png');
     $markup = $this->helper->__invoke($element);
     $this->assertContains('<input', $markup);
     $this->assertContains('name="foo"', $markup);
     $this->assertContains('type="image"', $markup);
     $this->assertContains('src="foo.png"', $markup);
 }
Beispiel #14
0
 public function __construct()
 {
     parent::__construct('test_form');
     $this->setAttribute('method', 'post')->setHydrator(new ClassMethodsHydrator());
     $field1 = new Element('name', array('label' => 'Name'));
     $field1->setAttribute('type', 'text');
     $this->add($field1);
     $field2 = new Element('email', array('label' => 'Email'));
     $field2->setAttribute('type', 'text');
     $this->add($field2);
     $this->add(array('name' => 'csrf', 'type' => 'Zend\\Form\\Element\\Csrf', 'attributes' => array()));
     $this->add(array('name' => 'submit', 'attributes' => array('type' => 'submit')));
 }
 public function __construct($name = null, $options = array())
 {
     parent::__construct("page", $options);
     $this->setAttribute("method", "post");
     $this->setAttribute("id", "pageform");
     $this->add(array("name" => "id", "attributes" => array("type" => "hidden", "value" => "0")));
     $this->add(array("name" => "article", "attributes" => array("type" => "textarea"), "options" => array("label" => "Description")));
     $title = new Element("title");
     $title->setAttribute("type", "text");
     $title->setLabel("Title");
     $this->add($title);
     $this->add(array("name" => "date", "attributes" => array("type" => "text"), "options" => array("label" => "Publication Date")));
     $this->add(array("name" => "submit", "type" => "submit", "attributes" => array("value" => "Отправить", "id" => "submitbutton", "class" => "btn")));
 }
Beispiel #16
0
 /**
  * Outputs message depending on flag
  *
  * @return string
  */
 public function __invoke(Element $element)
 {
     $element->setLabelAttributes(array('class' => 'control-label'));
     $element->setAttribute('data-placement', 'bottom')->setAttribute('data-content', $element->getOption('description'));
     if (!$element->getLabel()) {
         $element->setLabel('öö');
     }
     $output = '<div class="control-group">';
     $output .= $this->getView()->formLabel($element);
     $output .= '<div class="controls">';
     $output .= $this->getView()->formElement($element);
     if ($element->getOption('description')) {
         $output .= '<div class="description">' . $element->getOption('description') . '</div>';
     }
     if ($element->getMessages()) {
         $output .= '<div class="alert alert-error">' . $this->getView()->formElementErrors($element) . '</div>';
     }
     $output .= '</div>';
     $output .= '</div>';
     return $output;
 }
Beispiel #17
0
 public function testDoesNotOverrideClassesIfAlreadyPresentWhenThereAreErrors()
 {
     $element = new Element('foo');
     $element->setMessages(array('Error message'));
     $element->setAttribute('class', 'foo bar');
     $markup = $this->helper->setInputErrorClass('custom-error-class')->render($element);
     $this->assertRegexp('/<input name="foo" class="foo bar custom-error-class" type="text" [^\\/>]*\\/?>/', $markup);
 }
Beispiel #18
0
 /**
  * Add content to form
  *
  * @param Fieldset $form       Form
  * @param mixed    $elements   Elements
  * @param string   $prefix     Add belong to for each elements
  * @param integer  $datatypeId Datatype id
  *
  * @static
  * @return void
  */
 public static function addContent(Fieldset $form, $elements, $prefix = null, $datatypeId = null)
 {
     if (empty($elements)) {
         return;
     }
     if (!empty($prefix) and $datatypeId === null) {
         $datatypeId = mt_rand();
     }
     if (is_array($elements)) {
         $return = array();
         foreach ($elements as $element) {
             $return[] = self::addContent($form, $element, $prefix, $datatypeId);
         }
         return $return;
     } elseif ($elements instanceof Element) {
         if (!empty($prefix)) {
             $id = $elements->getAttribute('id');
             if (empty($id)) {
                 $id = $elements->getAttribute('name');
             }
             $elements->setAttribute('id', $id . $datatypeId);
             $elements->setAttribute('name', $prefix . '[' . $elements->getAttribute('name') . ']');
         }
         $form->add($elements);
         return $elements;
     } elseif (is_string($elements)) {
         if (!empty($prefix)) {
             $elements = preg_replace('~name="(.+)((&#x5B;|\\[).*(&#x5D;|\\]))?"~iU', 'name="' . $prefix . '&#x5B;$1&#x5D;$2"', $elements);
             $elements = preg_replace('~name\\\\x3D\\\\x22(.+)(\\\\x5B.*\\\\x5D)?\\\\x22~iU', 'name\\\\x3D\\\\x22' . $prefix . '\\\\x5B$1\\\\x5D$2\\\\x22', $elements);
             $elements = preg_replace('~id="(.+)"~iU', 'id="${1}' . $datatypeId . '"', $elements);
             $elements = preg_replace('~for="(.+)"~iU', 'for="${1}' . $datatypeId . '"', $elements);
             $elements = preg_replace('~id\\\\x3D\\\\x22"(.+)\\\\x22~iU', 'id\\\\x3D\\\\x22${1}' . $datatypeId . '\\\\x22', $elements);
             $elements = preg_replace('~for\\\\x3D\\\\x22"(.+)\\\\x22~iU', 'for\\\\x3D\\\\x22${1}' . $datatypeId . '\\\\x22', $elements);
             $elements = preg_replace('~(?:(?!(?<=value=)))("|\')#(.+)("|\')~iU', '${1}#${2}' . $datatypeId . '${3}', $elements);
         }
         $hiddenElement = new Element('hidden' . uniqid());
         $hiddenElement->setAttribute('content', $elements);
         $form->add($hiddenElement);
         return $hiddenElement;
     } else {
         throw new Exception('Invalid element ' . __CLASS__ . '::' . __METHOD__ . ')');
     }
 }
Beispiel #19
0
 public function getElement()
 {
     $element = new Element('foo');
     $element->setAttribute('captcha', $this->captcha);
     return $element;
 }
Beispiel #20
0
 public function testAppendLabelEvenIfElementHasId()
 {
     $element = new Element('foo');
     $element->setAttribute('id', 'bar');
     $element->setLabel('Baz');
     $this->helper->setLabelPosition('append');
     $markup = $this->helper->render($element);
     $this->assertEquals('<input name="foo" id="bar" type="text" value=""/><label for="bar">Baz</label>', $markup);
 }
Beispiel #21
0
 /**
  * @inheritdoc
  */
 public function setAttribute($key, $value)
 {
     return $this->element->setAttribute($key, $value);
 }
Beispiel #22
0
 public function testRendersTextareaAsExpected()
 {
     $element = new Element('foo');
     $element->setAttribute('type', 'textarea');
     $element->setAttribute('value', 'Initial content');
     $markup = $this->helper->render($element);
     $this->assertContains('<textarea', $markup);
     $this->assertContains('>Initial content<', $markup);
 }
Beispiel #23
0
 /**
  * Set a single element attribute
  *
  * @param  string $key
  * @param  mixed  $value
  * @return Select|ElementInterface
  */
 public function setAttribute($key, $value)
 {
     // Do not include the options in the list of attributes
     // TODO: Deprecate this
     if ($key === 'options') {
         $this->setValueOptions($value);
         return $this;
     }
     return parent::setAttribute($key, $value);
 }
 /**
  * @group ZF2-489
  */
 public function testCanTranslatePlaceholder()
 {
     $element = new Element('test');
     $element->setAttribute('placeholder', 'test');
     $mockTranslator = $this->getMock('Zend\\I18n\\Translator\\Translator');
     $mockTranslator->expects($this->exactly(1))->method('translate')->will($this->returnValue('translated string'));
     $this->helper->setTranslator($mockTranslator);
     $this->assertTrue($this->helper->hasTranslator());
     $markup = $this->helper->__invoke($element);
     $this->assertContains('placeholder="translated string"', $markup);
 }
Beispiel #25
0
 public function testSettingNameAttributeAllowsRetrievingName()
 {
     $element = new Element();
     $element->setAttribute('name', 'foo');
     $this->assertEquals('foo', $element->getName());
 }
Beispiel #26
0
 public function testPassingElementToRenderGeneratesButtonMarkup()
 {
     $element = new Element('foo');
     $element->setAttribute('label', '{button_content}');
     $markup = $this->helper->render($element);
     $this->assertContains('>{button_content}<', $markup);
     $this->assertContains('name="foo"', $markup);
     $this->assertContains('<button', $markup);
     $this->assertContains('</button>', $markup);
 }
Beispiel #27
0
 /**
  * @group ZF2-338
  * @dataProvider getScalarOptionsDataProvider
  */
 public function testScalarOptionValues($options)
 {
     $element = new Element('foo');
     $element->setAttribute('options', $options);
     $markup = $this->helper->render($element);
     list($label, $value) = each($options);
     $this->assertRegexp(sprintf('#option .*?value="%s"#', (string) $value), $markup);
 }
Beispiel #28
0
 public function testCanTranslateContent()
 {
     $element = new Element('foo');
     $element->setAttribute('options', array(array('label' => 'label1', 'value' => 'value1')));
     $markup = $this->helper->render($element);
     $mockTranslator = $this->getMock('Zend\\I18n\\Translator\\Translator');
     $mockTranslator->expects($this->exactly(1))->method('translate')->will($this->returnValue('translated content'));
     $this->helper->setTranslator($mockTranslator);
     $this->assertTrue($this->helper->hasTranslator());
     $markup = $this->helper->__invoke($element);
     $this->assertContains('>translated content<', $markup);
 }
Beispiel #29
0
 /**
  * @group ZF2-391
  * @dataProvider booleanAttributeTypes
  */
 public function testBooleanAttributeTypesAreRenderedCorrectly($attribute, $on, $off)
 {
     $element = new Element('foo');
     $element->setAttribute($attribute, true);
     $markup = $this->helper->render($element);
     $expect = sprintf('%s="%s"', $attribute, $on);
     $this->assertContains($expect, $markup, sprintf("Enabled value for %s should be '%s'; received %s", $attribute, $on, $markup));
     $element->setAttribute($attribute, false);
     $markup = $this->helper->render($element);
     $expect = sprintf('%s="%s"', $attribute, $off);
     if ($off !== '') {
         $this->assertContains($expect, $markup, sprintf("Disabled value for %s should be '%s'; received %s", $attribute, $off, $markup));
     } else {
         $this->assertNotContains($expect, $markup, sprintf("Disabled value for %s should not be rendered; received %s", $attribute, $markup));
     }
     // ZF2-391 : Ability to use non-boolean values that match expected end-value
     $element->setAttribute($attribute, $on);
     $markup = $this->helper->render($element);
     $expect = sprintf('%s="%s"', $attribute, $on);
     $this->assertContains($expect, $markup, sprintf("Enabled value for %s should be '%s'; received %s", $attribute, $on, $markup));
     $element->setAttribute($attribute, $off);
     $markup = $this->helper->render($element);
     $expect = sprintf('%s="%s"', $attribute, $off);
     if ($off !== '') {
         $this->assertContains($expect, $markup, sprintf("Disabled value for %s should be '%s'; received %s", $attribute, $off, $markup));
     } else {
         $this->assertNotContains($expect, $markup, sprintf("Disabled value for %s should not be rendered; received %s", $attribute, $markup));
     }
 }
Beispiel #30
0
 public function testCanAppendLabelContentUsingFlagToInvoke()
 {
     $element = new Element('foo');
     $element->setAttribute('label', 'The value for foo:');
     $markup = $this->helper->__invoke($element, '<input type="text" id="foo" />', FormLabelHelper::APPEND);
     $this->assertContains('"foo" />The value for foo:</label>', $markup);
     $this->assertContains('for="foo"', $markup);
     $this->assertContains('<label', $markup);
     $this->assertContains('><input type="text" id="foo" />', $markup);
 }