diff --git a/doc/form.md b/doc/form.md index 13845031c..0150ad431 100644 --- a/doc/form.md +++ b/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 *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()* *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 -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 diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 797f749da..29b1ced8b 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -294,13 +294,14 @@ abstract class Form extends Zend_Form } if ($submitted) { + // perform full validation if submitted $this->preValidation($checkData); return $this->isValid($checkData); } else { + // only populate if not submitted $this->populate($checkData); return false; } - } /** diff --git a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php index 3e8536b2f..6c04d8f1c 100644 --- a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php @@ -39,7 +39,8 @@ class DelayNotificationFormFormTest extends BaseFormTest public function testInvalidMinuteValue() { $form = $this->getRequestForm(array( - 'minutes' => 'SCHAHH-LAHH-LAHH' + 'minutes' => 'SCHAHH-LAHH-LAHH', + 'btn_submit' => 'foo' ), 'Monitoring\Form\Command\DelayNotificationForm'); $form->buildForm(); @@ -50,6 +51,7 @@ class DelayNotificationFormFormTest extends BaseFormTest ); $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"); } }