Skip to content

Commit

Permalink
added ability to add data attributes to htmlform and to elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasjonas committed Nov 20, 2015
1 parent ae1b007 commit 79f142d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
33 changes: 32 additions & 1 deletion Abstracts/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* The abstract element class contains the basic attributes and tools of
* container and input elements.
**/
abstract class element
abstract class Element
{
// {{{ variables
/**
Expand All @@ -36,6 +36,18 @@ abstract class element
* @brief Log object reference
**/
protected $log;

/**
* @addtogroup htmlformInputDefaults
*
* @default variant $dataAttr
* Optional associated Array of values that will be added as data-Attribute
* to the container element
**/
/**
* @brief Extra information about the data that is saved inside the element.
**/
public $dataAttr;
// }}}

// {{{ __construct()
Expand Down Expand Up @@ -76,6 +88,7 @@ protected function setDefaults()
{
$this->defaults['log'] = null;
$this->defaults['class'] = null;
$this->defaults['dataAttr'] = array();
}
// }}}

Expand Down Expand Up @@ -229,6 +242,24 @@ protected function htmlEscape($options = array())
return $htmlOptions;
}
// }}}

// {{{ htmlDataAttributes()
/**
* @brief Returns dataAttr escaped as attribute string
**/
protected function htmlDataAttributes()
{
$attributes = "";
if (is_array($this->dataAttr)) {
foreach ($this->dataAttr as $key => $val) {
// @todo throw error when key is not plain string?
$attributes .= " data-$key=\"" . $this->htmlEscape($val) . "\"";
}
}

return $attributes;
}
// }}}
}

/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
14 changes: 13 additions & 1 deletion Abstracts/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,18 @@ protected function htmlInputAttributes()
}
// }}}

// {{{ htmlDataAttributes()
/**
* @brief Returns dataAttr escaped as attribute string
**/
protected function htmlDataAttributes()
{
$this->dataAttr['errorMessage'] = $this->errorMessage;

return parent::htmlDataAttributes();
}
// }}}

// {{{ htmlWrapperAttributes()
/**
* @brief Returns string of HTML attributes for element wrapper paragraph.
Expand All @@ -584,7 +596,7 @@ protected function htmlWrapperAttributes()

$attributes .= ($this->title) ? " title=\"" . $this->htmlEscape($this->title) . "\"" : "";

$attributes .= " data-errorMessage=\"" . $this->htmlEscape($this->errorMessage) . "\"";
$attributes .= $this->htmlDataAttributes();

return $attributes;
}
Expand Down
16 changes: 7 additions & 9 deletions Elements/Richtext.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,23 @@ protected function setDefaults()
);
}
// }}}
// {{{ htmlWrapperAttributes()

// {{{ htmlDataAttributes()
/**
* @brief Returns string of HTML attributes for element wrapper paragraph.
*
* @return string $attributes HTML attribute
* @brief Returns dataAttr escaped as attribute string
**/
protected function htmlWrapperAttributes()
protected function htmlDataAttributes()
{
$attributes = parent::htmlWrapperAttributes();

$options = array();
$options['stylesheet'] = $this->stylesheet;
$options['allowedTags'] = $this->allowedTags;

$attributes .= " data-richtext-options=\"" . $this->htmlEscape(json_encode($options)) . "\"";
$this->dataAttr['richtext-options'] = json_encode($options);

return $attributes;
return parent::htmlDataAttributes();
}
// }}}

// {{{ htmlValue()
/**
* @brief Returns HTML-rendered element value
Expand Down
15 changes: 6 additions & 9 deletions Elements/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,21 @@ public function __toString()
}
// }}}

// {{{ htmlWrapperAttributes()
// {{{ htmlDataAttributes()
/**
* @brief Returns string of HTML attributes for element wrapper paragraph.
*
* @return string $attributes HTML attribute
* @brief Returns dataAttr escaped as attribute string
**/
protected function htmlWrapperAttributes()
protected function htmlDataAttributes()
{
$attributes = parent::htmlWrapperAttributes();

$options = array();
$options['autogrow'] = $this->autogrow;

$attributes .= " data-textarea-options=\"" . $this->htmlEscape(json_encode($options)) . "\"";
$this->dataAttr['textarea-options'] = json_encode($options);

return $attributes;
return parent::htmlDataAttributes();
}
// }}}

// {{{ htmlRows()
/**
* @brief Renders HTML rows attribute
Expand Down
16 changes: 15 additions & 1 deletion HtmlForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,19 @@ protected function getNewCsrfToken()
}
// }}}

// {{{ htmlDataAttributes()
/**
* @brief Returns dataAttr escaped as attribute string
**/
protected function htmlDataAttributes()
{
$this->dataAttr['jsvalidation'] = $this->jsValidation;
$this->dataAttr['jsautosave'] = $this->jsAutosave === true ? "true" : $this->jsAutosave;

return parent::htmlDataAttributes();
}
// }}}

// {{{ __toString()
/**
* @brief Renders form to HTML.
Expand All @@ -611,6 +624,7 @@ public function __toString()
$submitURL = $this->htmlSubmitURL();
$jsValidation = $this->htmlJsValidation();
$jsAutosave = $this->jsAutosave === true ? "true" : $this->htmlJsAutosave();
$dataAttr = $this->htmlDataAttributes();

foreach ($this->elementsAndHtml as $element) {
// leave out inactive step elements
Expand All @@ -633,7 +647,7 @@ public function __toString()
}


return "<form id=\"{$this->name}\" name=\"{$this->name}\" class=\"depage-form {$class}\" method=\"{$method}\" action=\"{$submitURL}\" data-jsvalidation=\"{$jsValidation}\" data-jsautosave=\"{$jsAutosave}\" enctype=\"multipart/form-data\">" . "\n" .
return "<form id=\"{$this->name}\" name=\"{$this->name}\" class=\"depage-form {$class}\" method=\"{$method}\" action=\"{$submitURL}\"{$dataAttr} enctype=\"multipart/form-data\">" . "\n" .
$renderedElements .
"<p id=\"{$this->name}-submit\" class=\"submit\"><input type=\"submit\" name=\"formSubmit\" value=\"{$label}\"></p>" . "\n" .
$cancel .
Expand Down
8 changes: 4 additions & 4 deletions Tests/textareaToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp()
**/
public function testSimple()
{
$expected = '<p id="formName-textareaName" class="input-textarea" data-errorMessage="Please enter valid data" data-textarea-options="{&quot;autogrow&quot;:false}">' .
$expected = '<p id="formName-textareaName" class="input-textarea" data-textarea-options="{&quot;autogrow&quot;:false}" data-errorMessage="Please enter valid data">' .
'<label>' .
'<span class="depage-label">textareaName</span>' .
'<textarea name="textareaName"></textarea>' .
Expand All @@ -38,7 +38,7 @@ public function testSimple()
**/
public function testValue()
{
$expected = '<p id="formName-textareaName" class="input-textarea" data-errorMessage="Please enter valid data" data-textarea-options="{&quot;autogrow&quot;:false}">' .
$expected = '<p id="formName-textareaName" class="input-textarea" data-textarea-options="{&quot;autogrow&quot;:false}" data-errorMessage="Please enter valid data">' .
'<label>' .
'<span class="depage-label">textareaName</span>' .
'<textarea name="textareaName">testValue</textarea>' .
Expand All @@ -56,7 +56,7 @@ public function testValue()
**/
public function testRequired()
{
$expected = '<p id="formName-textareaName" class="input-textarea required" data-errorMessage="Please enter valid data" data-textarea-options="{&quot;autogrow&quot;:false}">' .
$expected = '<p id="formName-textareaName" class="input-textarea required" data-textarea-options="{&quot;autogrow&quot;:false}" data-errorMessage="Please enter valid data">' .
'<label>' .
'<span class="depage-label">textareaName <em>*</em></span>' .
'<textarea name="textareaName" required="required"></textarea>' .
Expand All @@ -74,7 +74,7 @@ public function testRequired()
**/
public function testHtmlEscaping()
{
$expected = '<p id="formName-textareaName" class="input-textarea" title="ti&quot;&gt;tle" data-errorMessage="er&quot;&gt;rorMessage" data-textarea-options="{&quot;autogrow&quot;:false}">' .
$expected = '<p id="formName-textareaName" class="input-textarea" title="ti&quot;&gt;tle" data-textarea-options="{&quot;autogrow&quot;:false}" data-errorMessage="er&quot;&gt;rorMessage">' .
'<label>' .
'<span class="depage-label">la&quot;&gt;bel</span>' .
'<textarea name="textareaName"></textarea>' .
Expand Down

0 comments on commit 79f142d

Please sign in to comment.