mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-31 01:34:09 +02:00
Make the FormDescriptions decorator able to handle sub forms
refs #7947
This commit is contained in:
parent
31560c9249
commit
764f125778
@ -13,6 +13,29 @@ use Icinga\Web\Form;
|
|||||||
*/
|
*/
|
||||||
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
|
||||||
*
|
*
|
||||||
@ -32,9 +55,16 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
$descriptions = $form->getDescriptions();
|
$descriptions = $this->recurseForm($form, $entirelyRequired);
|
||||||
if (($requiredDesc = $this->getRequiredDescription($form)) !== null) {
|
if ($entirelyRequired) {
|
||||||
$descriptions[] = $requiredDesc;
|
$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)) {
|
||||||
@ -60,53 +90,57 @@ class FormDescriptions extends Zend_Form_Decorator_Abstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the description for the given form's required elements
|
* Recurse the given form and return the descriptions for it and all of its subforms
|
||||||
*
|
*
|
||||||
* @param Form $form
|
* @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 string|null
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function getRequiredDescription(Form $form)
|
protected function recurseForm(Form $form, & $entirelyRequired = null, $elementsPassed = false)
|
||||||
{
|
{
|
||||||
if (($cue = $form->getRequiredCue()) === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$requiredLabels = array();
|
$requiredLabels = array();
|
||||||
$entirelyRequired = true;
|
if ($form->getRequiredCue() !== null) {
|
||||||
$partiallyRequired = false;
|
$partiallyRequired = $partiallyOptional = false;
|
||||||
$blacklist = array(
|
foreach ($form->getElements() as $element) {
|
||||||
'Zend_Form_Element_Hidden',
|
if (! in_array($element->getType(), $this->blacklist)) {
|
||||||
'Zend_Form_Element_Submit',
|
if (! $element->isRequired()) {
|
||||||
'Zend_Form_Element_Button',
|
$partiallyOptional = true;
|
||||||
'Icinga\Web\Form\Element\Note',
|
if ($entirelyRequired) {
|
||||||
'Icinga\Web\Form\Element\Button',
|
$entirelyRequired = false;
|
||||||
'Icinga\Web\Form\Element\CsrfCounterMeasure'
|
}
|
||||||
);
|
} else {
|
||||||
foreach ($form->getElements() as $element) {
|
$partiallyRequired = true;
|
||||||
if (! in_array($element->getType(), $blacklist)) {
|
if (($label = $element->getDecorator('label')) !== false) {
|
||||||
if (! $element->isRequired()) {
|
$requiredLabels[] = $label;
|
||||||
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($entirelyRequired && $partiallyRequired) {
|
$descriptions = array($form->getDescriptions());
|
||||||
|
foreach ($form->getSubForms() as $subForm) {
|
||||||
|
$descriptions[] = $this->recurseForm($subForm, $entirelyRequired, $elementsPassed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($entirelyRequired) {
|
||||||
foreach ($requiredLabels as $label) {
|
foreach ($requiredLabels as $label) {
|
||||||
$label->setRequiredSuffix('');
|
$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
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return call_user_func_array('array_merge', $descriptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user