QuickForm/FormLoader: add helper for subforms

This commit is contained in:
Thomas Gelf 2015-07-02 15:30:02 +02:00
parent 6730ac7447
commit 61795ccdcd
2 changed files with 32 additions and 1 deletions

View File

@ -24,7 +24,12 @@ class FormLoader
if (file_exists($file)) { if (file_exists($file)) {
require_once($file); require_once($file);
$class = $ns . $class; $class = $ns . $class;
return new $class(); $options = array();
if ($module !== null) {
$options['icingaModule'] = $module;
}
return new $class($options);
} }
} }
throw new ProgrammingError(sprintf('Cannot load %s (%s), no such form', $name, $file)); throw new ProgrammingError(sprintf('Cannot load %s (%s), no such form', $name, $file));

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Web\Form; namespace Icinga\Module\Director\Web\Form;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Web\Notification; use Icinga\Web\Notification;
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Web\Url; use Icinga\Web\Url;
@ -48,8 +49,18 @@ abstract class QuickForm extends Zend_Form
protected $submitLabel; protected $submitLabel;
/**
* The Icinga module this form belongs to. Usually only set if the
* form is initialized through the FormLoader
*/
protected $icingaModule;
public function __construct($options = null) public function __construct($options = null)
{ {
if (array_key_exists('icingaModule', $options)) {
$this->icingaModule = $options['icingaModule'];
unset($options['icingaModule']);
}
parent::__construct($options); parent::__construct($options);
$this->setMethod('post'); $this->setMethod('post');
$this->setAction(Url::fromRequest()); $this->setAction(Url::fromRequest());
@ -89,6 +100,15 @@ abstract class QuickForm extends Zend_Form
return $this; return $this;
} }
protected function loadForm($name, Module $module = null)
{
if ($module === null) {
$module = $this->icingaModule;
}
return FormLoader::load($name, $module);
}
public function regenerateCsrfToken() public function regenerateCsrfToken()
{ {
if (! $element = $this->getElement(self::CSRF)) { if (! $element = $this->getElement(self::CSRF)) {
@ -137,6 +157,12 @@ abstract class QuickForm extends Zend_Form
return parent::setAction((string) $action); return parent::setAction((string) $action);
} }
public function setIcingaModule(Module $module)
{
$this->icingaModule = $module;
return $this;
}
public function hasBeenSubmitted() public function hasBeenSubmitted()
{ {
if ($this->hasBeenSubmitted === null) { if ($this->hasBeenSubmitted === null) {