mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-28 16:24:04 +02:00
commit
ab1673f198
13
doc/form.md
13
doc/form.md
@ -24,11 +24,22 @@ In here you can add elements to your form, add validations and filters of your
|
|||||||
choice. The creation method is invoked lazy just before a form is rendered or
|
choice. The creation method is invoked lazy just before a form is rendered or
|
||||||
*isValid()* is called.
|
*isValid()* is called.
|
||||||
|
|
||||||
|
In order to let icingaweb create a submit button for you (which is required for using the *isSubmittedAndValid*
|
||||||
|
method) you have to call the *setSubmitLabel($label)* method, which will add a
|
||||||
|
Zend_Form_Element_Submit element to your form.
|
||||||
|
|
||||||
#### Calling is *isSubmittedAndValid()*
|
#### Calling is *isSubmittedAndValid()*
|
||||||
|
|
||||||
*isSubmittedAndValid()* is used to check whether the form is ready to be processed or not.
|
*isSubmittedAndValid()* is used to check whether the form is ready to be processed or not.
|
||||||
It ensures that the current request method is POST, that the form was manually submitted
|
It ensures that the current request method is POST, that the form was manually submitted
|
||||||
and that the data provided in the request is valid and gets repopulated in case its invalid.
|
and that the data provided in the request is valid and gets repopulated in case its invalid. This only works when
|
||||||
|
the sumbit button has been added with the *setSubmitLabel($label)* function, otherwise a form is always considered to be
|
||||||
|
submitted when a POST request is received.
|
||||||
|
|
||||||
|
If the form has been updated, but not submitted (for example, because the a button has been pressed that adds or removes
|
||||||
|
some fields in the form) the form is repopulated but not validated at this time. is SubmittedAndValid() returns false
|
||||||
|
in this case, but no errors are added to the created form.
|
||||||
|
|
||||||
|
|
||||||
#### Pre validation
|
#### Pre validation
|
||||||
|
|
||||||
|
@ -294,9 +294,14 @@ abstract class Form extends Zend_Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($submitted) {
|
if ($submitted) {
|
||||||
|
// perform full validation if submitted
|
||||||
$this->preValidation($checkData);
|
$this->preValidation($checkData);
|
||||||
|
return $this->isValid($checkData);
|
||||||
|
} else {
|
||||||
|
// only populate if not submitted
|
||||||
|
$this->populate($checkData);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return parent::isValid($checkData) && $submitted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -361,20 +366,16 @@ abstract class Form extends Zend_Form
|
|||||||
if ($this->getElement($this->tokenElementName) === null) {
|
if ($this->getElement($this->tokenElementName) === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($elementValue, '|') === false) {
|
if (strpos($elementValue, '|') === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
list($seed, $token) = explode('|', $elementValue);
|
list($seed, $token) = explode('|', $elementValue);
|
||||||
|
|
||||||
if (!is_numeric($seed)) {
|
if (!is_numeric($seed)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$seed -= intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
|
||||||
|
|
||||||
return $token === hash('sha256', $this->getSessionId() . $seed);
|
return $token === hash('sha256', $this->getSessionId() . $seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,7 +387,6 @@ abstract class Form extends Zend_Form
|
|||||||
{
|
{
|
||||||
$seed = mt_rand();
|
$seed = mt_rand();
|
||||||
$hash = hash('sha256', $this->getSessionId() . $seed);
|
$hash = hash('sha256', $this->getSessionId() . $seed);
|
||||||
$seed += intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
|
|
||||||
|
|
||||||
return array($seed, $hash);
|
return array($seed, $hash);
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,14 @@ use Icinga\Protocol\Commandpipe\Comment;
|
|||||||
*/
|
*/
|
||||||
class AcknowledgeForm extends CommandForm
|
class AcknowledgeForm extends CommandForm
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Initialize form
|
||||||
|
*/
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->setName('AcknowledgeForm');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface method to build the form
|
* Interface method to build the form
|
||||||
* @see CommandForm::create
|
* @see CommandForm::create
|
||||||
|
@ -58,6 +58,14 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
|
|||||||
*/
|
*/
|
||||||
const TYPE_FLEXIBLE = 'flexible';
|
const TYPE_FLEXIBLE = 'flexible';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize form
|
||||||
|
*/
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->setName('ScheduleDowntimeForm');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build an array of timestamps
|
* Build an array of timestamps
|
||||||
*
|
*
|
||||||
|
@ -39,7 +39,8 @@ class DelayNotificationFormFormTest extends BaseFormTest
|
|||||||
public function testInvalidMinuteValue()
|
public function testInvalidMinuteValue()
|
||||||
{
|
{
|
||||||
$form = $this->getRequestForm(array(
|
$form = $this->getRequestForm(array(
|
||||||
'minutes' => 'SCHAHH-LAHH-LAHH'
|
'minutes' => 'SCHAHH-LAHH-LAHH',
|
||||||
|
'btn_submit' => 'foo'
|
||||||
), 'Monitoring\Form\Command\DelayNotificationForm');
|
), 'Monitoring\Form\Command\DelayNotificationForm');
|
||||||
|
|
||||||
$form->buildForm();
|
$form->buildForm();
|
||||||
@ -50,6 +51,7 @@ class DelayNotificationFormFormTest extends BaseFormTest
|
|||||||
);
|
);
|
||||||
|
|
||||||
$errors = $form->getErrors('minutes');
|
$errors = $form->getErrors('minutes');
|
||||||
|
$this->assertCount(1, $errors, "Asserting an error to be added when the minutes value is invalid");
|
||||||
$this->assertEquals('notBetween', $errors[0], "Assert correct error message");
|
$this->assertEquals('notBetween', $errors[0], "Assert correct error message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user