Make autosubmit work in non-JS enviroments

We require forms to set autosubmit=true on elements which are supposed
to be submitted automatically now. The base form then ensures that this
works in JS environments and non-JS environments as well by applying the
right changes to the HTML.

refs #5525
This commit is contained in:
Johannes Meyer 2014-08-29 09:08:53 +02:00
parent 486104d59e
commit bf23688e73
2 changed files with 44 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use Zend_Form;
use Zend_View_Interface;
use Icinga\Application\Icinga;
use Icinga\Web\Form\Decorator\HelpText;
use Icinga\Web\Form\Decorator\NoScriptApply;
use Icinga\Web\Form\Decorator\ElementWrapper;
use Icinga\Web\Form\Element\CsrfCounterMeasure;
@ -330,6 +331,14 @@ class Form extends Zend_Form
$el->removeDecorator('HtmlTag');
$el->removeDecorator('Label');
$el->removeDecorator('DtDdWrapper');
if ($el->getAttrib('autosubmit')) {
// Need to add this decorator first or it interferes with the other's two HTML otherwise
$el->addDecorator(new NoScriptApply()); // Non-JS environments
$el->setAttrib('class', 'autosubmit'); // JS environments
unset($el->autosubmit);
}
$el->addDecorator(new ElementWrapper());
$el->addDecorator(new HelpText());
}

View File

@ -0,0 +1,35 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
use Icinga\Application\Icinga;
/**
* Decorator to add a submit button encapsulated in noscript-tags
*
* This enables users in non-JS environments to update the contents
* of a form without the use of the main submit button.
*/
class NoScriptApply extends Zend_Form_Decorator_Abstract
{
/**
* Add a submit button encapsulated in noscript-tags to the element
*
* @param string $content The html rendered so far
*
* @return string The updated html
*/
public function render($content = '')
{
if ($content) {
$content .= '<noscript><button name="noscript_apply" style="margin-left: 0.5em;" type="submit" value="1">'
. Icinga::app()->getViewRenderer()->view->icon('refresh.png') . '&nbsp;' . t('Apply')
. '</button></noscript>';
}
return $content;
}
}