Make it possible to provide a closure to be called instead of onSuccess()
This allows us to write generic forms like the ConfirmRemoval form but to utilize handleRequest() while providing a closure in the array that is passed to a form's constructor. refs #5525
This commit is contained in:
parent
bf23688e73
commit
79b0ed64ec
|
@ -4,6 +4,7 @@
|
|||
|
||||
namespace Icinga\Web;
|
||||
|
||||
use LogicException;
|
||||
use Zend_Form;
|
||||
use Zend_View_Interface;
|
||||
use Icinga\Application\Icinga;
|
||||
|
@ -24,6 +25,13 @@ class Form extends Zend_Form
|
|||
*/
|
||||
protected $created = false;
|
||||
|
||||
/**
|
||||
* The callback to call instead of Form::onSuccess()
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
protected $onSuccess;
|
||||
|
||||
/**
|
||||
* Label to use for the standard submit button
|
||||
*
|
||||
|
@ -68,6 +76,33 @@ class Form extends Zend_Form
|
|||
*/
|
||||
protected $uidElementName = 'formUID';
|
||||
|
||||
/**
|
||||
* Create a new form
|
||||
*
|
||||
* Accepts an additional option `onSuccess' which is a
|
||||
* callback that is called instead of this form's method.
|
||||
*
|
||||
* @see Zend_Form::__construct()
|
||||
*
|
||||
* @throws LogicException In case `onSuccess' is not callable
|
||||
*/
|
||||
public function __construct($options = null)
|
||||
{
|
||||
if (is_array($options) && isset($options['onSuccess'])) {
|
||||
$this->onSuccess = $options['onSuccess'];
|
||||
unset($options['onSuccess']);
|
||||
} elseif (isset($options->onSuccess)) {
|
||||
$this->onSuccess = $options->onSuccess;
|
||||
unset($options->onSuccess);
|
||||
}
|
||||
|
||||
if ($this->onSuccess !== null && false === is_callable($this->onSuccess)) {
|
||||
throw new LogicException('The option `onSuccess\' is not callable');
|
||||
}
|
||||
|
||||
parent::__construct($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the label to use for the standard submit button
|
||||
*
|
||||
|
@ -413,7 +448,9 @@ class Form extends Zend_Form
|
|||
if ($this->wasSent($formData)) {
|
||||
$this->populate($formData); // Necessary to get isSubmitted() to work
|
||||
if (! $this->getSubmitLabel() || $this->isSubmitted()) {
|
||||
if ($this->isValid($formData) && false !== $this->onSuccess($request)) {
|
||||
if ($this->isValid($formData)
|
||||
&& (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $request))
|
||||
|| ($this->onSuccess === null && false !== $this->onSuccess($request)))) {
|
||||
$this->getResponse()->redirectAndExit($this->getRedirectUrl());
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue