Merge branch 'feature/dynamic-forms-4439'

resolves #4439
This commit is contained in:
Jannis Moßhammer 2013-08-06 15:22:30 +02:00
commit ab1673f198
6 changed files with 38 additions and 9 deletions

View File

@ -98,4 +98,4 @@ class AuthenticationController extends ActionController
}
}
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreEnd

View File

@ -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

View File

@ -294,9 +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;
}
return parent::isValid($checkData) && $submitted;
}
/**
@ -361,20 +366,16 @@ abstract class Form extends Zend_Form
if ($this->getElement($this->tokenElementName) === null) {
return false;
}
if (strpos($elementValue, '|') === false) {
return false;
}
list($seed, $token) = explode('|', $elementValue);
if (!is_numeric($seed)) {
return false;
}
$seed -= intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
return $token === hash('sha256', $this->getSessionId() . $seed);
}
@ -386,7 +387,6 @@ abstract class Form extends Zend_Form
{
$seed = mt_rand();
$hash = hash('sha256', $this->getSessionId() . $seed);
$seed += intval(time() / $this->tokenTimeout) * $this->tokenTimeout;
return array($seed, $hash);
}

View File

@ -39,6 +39,14 @@ use Icinga\Protocol\Commandpipe\Comment;
*/
class AcknowledgeForm extends CommandForm
{
/**
* Initialize form
*/
public function init()
{
$this->setName('AcknowledgeForm');
}
/**
* Interface method to build the form
* @see CommandForm::create

View File

@ -58,6 +58,14 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm
*/
const TYPE_FLEXIBLE = 'flexible';
/**
* Initialize form
*/
public function init()
{
$this->setName('ScheduleDowntimeForm');
}
/**
* Build an array of timestamps
*

View File

@ -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");
}
}