public function testPassingElementToOpenTagWillUseIdInForAttributeWhenPresent() { $element = new Element('foo'); $element->setAttribute('id', 'bar'); $markup = $this->helper->openTag($element); $this->assertContains('for="bar"', $markup); }
public function __construct() { parent::__construct('nested_fieldset'); $field = new Element('anotherField', array('label' => 'Name')); $field->setAttribute('type', 'text'); $this->add($field); }
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); }
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); }
/** * 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); }
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); }
/** * 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); }
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); }
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); }
/** * @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); }
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); }
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); }
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"))); }
/** * 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; }
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); }
/** * 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="(.+)(([|\\[).*(]|\\]))?"~iU', 'name="' . $prefix . '[$1]$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__ . ')'); } }
public function getElement() { $element = new Element('foo'); $element->setAttribute('captcha', $this->captcha); return $element; }
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); }
/** * @inheritdoc */ public function setAttribute($key, $value) { return $this->element->setAttribute($key, $value); }
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); }
/** * 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); }
public function testSettingNameAttributeAllowsRetrievingName() { $element = new Element(); $element->setAttribute('name', 'foo'); $this->assertEquals('foo', $element->getName()); }
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); }
/** * @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); }
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); }
/** * @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)); } }
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); }