Skip to content

Commit

Permalink
We only want to rely on Form::isValid() when about to process forms
Browse files Browse the repository at this point in the history
As the new auto-submission implementation does not depend on whether
a submit button was pressed or not we need another way to determine
whether a form should be processed.

refs #5525
  • Loading branch information
Johannes Meyer committed Jul 18, 2014
1 parent 802c095 commit 58188c5
Showing 1 changed file with 0 additions and 92 deletions.
92 changes: 0 additions & 92 deletions library/Icinga/Web/Form.php
Expand Up @@ -9,7 +9,6 @@
use Icinga\Web\Form\Decorator\HelpText;
use Icinga\Web\Form\Decorator\ElementWrapper;
use Icinga\Web\Form\InvalidCSRFTokenException;
use Icinga\Exception\ProgrammingError;

/**
* Base class for forms providing CSRF protection, confirmation logic and auto submission
Expand Down Expand Up @@ -185,97 +184,6 @@ public function loadDefaultDecorators()
return $this;
}

/**
* Create, add this form's elements and populate them with the given values
*
* @param array $values The values with which to populate the elements
*
* @return self
*
* @throws ProgrammingError In case the parent for a dependent field cannot be found
*/
public function applyValues(array $values)
{
foreach ($this->createElements() as $element) {
$parentName = $element->getAttrib('depends');
if ($parentName !== null) {
$parent = $this->getElement($parentName);
if ($parent) {
$parentValue = isset($values[$parentName]) ? $values[$parentName] : $parent->getValue();
if ($parentValue != $element->getAttrib('requires')) {
if ($element->getAttrib('action') === 'disable') {
$this->addElement($element->setAttrib('disabled', 'disabled'));
}
} else {
$this->addElement($element);
}
} else {
throw new ProgrammingError(
'Cannot find parent "' . $parentName . '" for dependent field "' . $element->getName() . '"'
. '(Correct usage of field dependencies requires their parents to occur beforehand in order)'
);
}
} else {
$this->addElement($element);
}
}

$this->initCsrfToken();
$this->initSubmitButton();
$this->initCancelButton();
$this->populate($values);
return $this;
}

/**
* Check whether the form was submitted with a valid request
*
* Create and add this form's elements, populate them with the given request data and
* run a full validation if the form was submitted or a partial validation if not.
*
* @param array The request data to validate
*
* @return bool True when the form is submitted and valid, otherwise false
*/
public function isSubmittedAndValid(array $data)
{
$this->applyValues(array_merge($this->getConfiguration(), $data));

if ($this->isSubmitted()) {
$this->assertValidCsrfToken($data);
return $this->isValid($data); // Run full validation once this form's data is going to be processed
} else {
$this->isValidPartial($data); // Run a partial validation to not to overwrite default values
return false;
}
}

/**
* Check whether this form has been submitted
*
* Per default, this checks whether the button set with the 'setSubmitLabel' method
* is being submitted. For custom submission logic, this method must be overwritten.
*
* @return bool True when the form has been submitted, otherwise false
*
* @throws ProgrammingError In case the submit button has not yet been created
*/
public function isSubmitted()
{
if ($this->submitLabel) {
$submitBtn = $this->getElement('btn_submit');
if ($submitBtn) {
return $submitBtn->isChecked();
}

throw new ProgrammingError(
'Submit button not created yet. You need to call isSubmittedAndValid or applyValues beforehand!'
);
}

return false;
}

/**
* Add CSRF counter measure field to this form
*/
Expand Down

0 comments on commit 58188c5

Please sign in to comment.