Improve the display of the text cue on required form elements

Shows just a message at the top of the form if all elements are required
and a explaining message what the cue is standing for otherwise.

refs #7934
This commit is contained in:
Johannes Meyer 2015-03-02 13:48:35 +01:00
parent 1a334f8d64
commit 1eacaa4c48
1 changed files with 55 additions and 0 deletions

View File

@ -8,6 +8,8 @@ use Icinga\Web\Form;
/**
* Decorator to add a list of descriptions at the top of a form
*
* The description for required form elements is automatically being handled.
*/
class FormDescriptions extends Zend_Form_Decorator_Abstract
{
@ -31,6 +33,10 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
}
$descriptions = $form->getDescriptions();
if (($requiredDesc = $this->getRequiredDescription($form)) !== null) {
$descriptions[] = $requiredDesc;
}
if (empty($descriptions)) {
return $content;
}
@ -52,4 +58,53 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
return $html . '</ul>' . $content;
}
}
/**
* Return the description for the given form's required elements
*
* @param Form $form
*
* @return string|null
*/
protected function getRequiredDescription(Form $form)
{
if (($cue = $form->getRequiredCue()) === null) {
return;
}
$requiredLabels = array();
$entirelyRequired = true;
$partiallyRequired = false;
$blacklist = array(
'Zend_Form_Element_Hidden',
'Zend_Form_Element_Submit',
'Zend_Form_Element_Button',
'Icinga\Web\Form\Element\CsrfCounterMeasure'
);
foreach ($form->getElements() as $element) {
if (! in_array($element->getType(), $blacklist)) {
if (! $element->isRequired()) {
$entirelyRequired = false;
} else {
$partiallyRequired = true;
if (($label = $element->getDecorator('label')) !== false) {
$requiredLabels[] = $label;
}
}
}
}
if ($entirelyRequired && $partiallyRequired) {
foreach ($requiredLabels as $label) {
$label->setRequiredSuffix('');
}
return $form->getView()->translate('All fields are required and must be filled in to complete the form.');
} elseif ($partiallyRequired) {
return $form->getView()->translate(sprintf(
'Required fields are marked with %s and must be filled in to complete the form.',
$cue
));
}
}
}