mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-20 04:14:24 +02:00
FormDescriptions: Do not render a description for required fields
refs #8983
This commit is contained in:
parent
147f6be714
commit
ea6f707bdf
@ -7,35 +7,10 @@ use Zend_Form_Decorator_Abstract;
|
|||||||
use Icinga\Web\Form;
|
use Icinga\Web\Form;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decorator to add a list of descriptions at the top of a form
|
* Decorator to add a list of descriptions at the top or bottom of a form
|
||||||
*
|
|
||||||
* The description for required form elements is automatically being handled.
|
|
||||||
*/
|
*/
|
||||||
class FormDescriptions extends Zend_Form_Decorator_Abstract
|
class FormDescriptions extends Zend_Form_Decorator_Abstract
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* A list of element class names to be ignored when detecting which message to use to describe required elements
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $blacklist;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function __construct($options = null)
|
|
||||||
{
|
|
||||||
parent::__construct($options);
|
|
||||||
$this->blacklist = array(
|
|
||||||
'Zend_Form_Element_Hidden',
|
|
||||||
'Zend_Form_Element_Submit',
|
|
||||||
'Zend_Form_Element_Button',
|
|
||||||
'Icinga\Web\Form\Element\Note',
|
|
||||||
'Icinga\Web\Form\Element\Button',
|
|
||||||
'Icinga\Web\Form\Element\CsrfCounterMeasure'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render form descriptions
|
* Render form descriptions
|
||||||
*
|
*
|
||||||
@ -55,18 +30,7 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
$descriptions = $this->recurseForm($form, $entirelyRequired);
|
$descriptions = $this->recurseForm($form);
|
||||||
if ($entirelyRequired) {
|
|
||||||
$descriptions[] = $form->getView()->translate(
|
|
||||||
'All fields are required and must be filled in to complete the form.'
|
|
||||||
);
|
|
||||||
} elseif ($entirelyRequired === false) {
|
|
||||||
$descriptions[] = $form->getView()->translate(sprintf(
|
|
||||||
'Required fields are marked with %s and must be filled in to complete the form.',
|
|
||||||
$form->getRequiredCue()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($descriptions)) {
|
if (empty($descriptions)) {
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
@ -92,53 +56,15 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
|
|||||||
/**
|
/**
|
||||||
* Recurse the given form and return the descriptions for it and all of its subforms
|
* Recurse the given form and return the descriptions for it and all of its subforms
|
||||||
*
|
*
|
||||||
* @param Form $form The form to recurse
|
* @param Form $form The form to recurse
|
||||||
* @param mixed $entirelyRequired Set by reference, true means all elements in the hierarchy are
|
|
||||||
* required, false only a partial subset and null none at all
|
|
||||||
* @param bool $elementsPassed Whether there were any elements passed during the recursion until now
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function recurseForm(Form $form, & $entirelyRequired = null, $elementsPassed = false)
|
protected function recurseForm(Form $form)
|
||||||
{
|
{
|
||||||
$requiredLabels = array();
|
|
||||||
if ($form->getRequiredCue() !== null) {
|
|
||||||
$partiallyRequired = $partiallyOptional = false;
|
|
||||||
foreach ($form->getElements() as $element) {
|
|
||||||
if (! in_array($element->getType(), $this->blacklist)) {
|
|
||||||
if (! $element->isRequired()) {
|
|
||||||
$partiallyOptional = true;
|
|
||||||
if ($entirelyRequired) {
|
|
||||||
$entirelyRequired = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$partiallyRequired = true;
|
|
||||||
if (($label = $element->getDecorator('label')) !== false) {
|
|
||||||
$requiredLabels[] = $label;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! $elementsPassed) {
|
|
||||||
$elementsPassed = $partiallyRequired || $partiallyOptional;
|
|
||||||
if ($entirelyRequired === null && $partiallyRequired) {
|
|
||||||
$entirelyRequired = ! $partiallyOptional;
|
|
||||||
}
|
|
||||||
} elseif ($entirelyRequired === null && $partiallyRequired) {
|
|
||||||
$entirelyRequired = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$descriptions = array($form->getDescriptions());
|
$descriptions = array($form->getDescriptions());
|
||||||
foreach ($form->getSubForms() as $subForm) {
|
foreach ($form->getSubForms() as $subForm) {
|
||||||
$descriptions[] = $this->recurseForm($subForm, $entirelyRequired, $elementsPassed);
|
$descriptions[] = $this->recurseForm($subForm);
|
||||||
}
|
|
||||||
|
|
||||||
if ($entirelyRequired) {
|
|
||||||
foreach ($requiredLabels as $label) {
|
|
||||||
$label->setRequiredSuffix('');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return call_user_func_array('array_merge', $descriptions);
|
return call_user_func_array('array_merge', $descriptions);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user