Merge branch 'bugfix/improve-form-notifications-8983'

fixes #8983
This commit is contained in:
Johannes Meyer 2015-07-20 15:54:14 +02:00
commit c9a532a2b1
6 changed files with 219 additions and 90 deletions

View File

@ -160,6 +160,13 @@ class Form extends Zend_Form
*/ */
protected $notifications; protected $notifications;
/**
* The hints of this form
*
* @var array
*/
protected $hints;
/** /**
* Whether the Autosubmit decorator should be applied to this form * Whether the Autosubmit decorator should be applied to this form
* *
@ -566,6 +573,49 @@ class Form extends Zend_Form
return $this->notifications; return $this->notifications;
} }
/**
* Set the hints for this form
*
* @param array $hints
*
* @return $this
*/
public function setHints(array $hints)
{
$this->hints = $hints;
return $this;
}
/**
* Add a hint for this form
*
* If $hint is an array the second value should be an
* array as well containing additional HTML properties.
*
* @param string|array $hint
*
* @return $this
*/
public function addHint($hint)
{
$this->hints[] = $hint;
return $this;
}
/**
* Return the hints of this form
*
* @return array
*/
public function getHints()
{
if ($this->hints === null) {
return array();
}
return $this->hints;
}
/** /**
* Set whether the Autosubmit decorator should be applied to this form * Set whether the Autosubmit decorator should be applied to this form
* *
@ -1087,10 +1137,11 @@ class Form extends Zend_Form
->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'header')); ->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'header'));
} }
$this->addDecorator('FormErrors', array('onlyCustomFormErrors' => true)) $this->addDecorator('FormDescriptions')
->addDecorator('FormNotifications') ->addDecorator('FormNotifications')
->addDecorator('FormDescriptions') ->addDecorator('FormErrors', array('onlyCustomFormErrors' => true))
->addDecorator('FormElements') ->addDecorator('FormElements')
->addDecorator('FormHints')
//->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')) //->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
->addDecorator('Form'); ->addDecorator('Form');
} }

View File

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

View File

@ -0,0 +1,142 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Form\Decorator;
use Zend_Form_Decorator_Abstract;
use Icinga\Web\Form;
/**
* Decorator to add a list of hints at the top or bottom of a form
*
* The hint for required form elements is automatically being handled.
*/
class FormHints 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 hints
*
* @param string $content The html rendered so far
*
* @return string The updated html
*/
public function render($content = '')
{
$form = $this->getElement();
if (! $form instanceof Form) {
return $content;
}
$view = $form->getView();
if ($view === null) {
return $content;
}
$hints = $this->recurseForm($form, $entirelyRequired);
if ($entirelyRequired !== null) {
$hints[] = $form->getView()->translate(sprintf(
'Required fields are marked with %s and must be filled in to complete the form.',
$form->getRequiredCue()
));
}
if (empty($hints)) {
return $content;
}
$html = '<ul class="hints">';
foreach ($hints as $hint) {
if (is_array($hint)) {
list($hint, $properties) = $hint;
$html .= '<li' . $view->propertiesToString($properties) . '>' . $view->escape($hint) . '</li>';
} else {
$html .= '<li>' . $view->escape($hint) . '</li>';
}
}
switch ($this->getPlacement()) {
case self::APPEND:
return $content . $html . '</ul>';
case self::PREPEND:
return $html . '</ul>' . $content;
}
}
/**
* Recurse the given form and return the hints for it and all of its subforms
*
* @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
*/
protected function recurseForm(Form $form, & $entirelyRequired = null, $elementsPassed = false)
{
$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;
}
}
$hints = array($form->getHints());
foreach ($form->getSubForms() as $subForm) {
$hints[] = $this->recurseForm($subForm, $entirelyRequired, $elementsPassed);
}
if ($entirelyRequired) {
foreach ($requiredLabels as $label) {
$label->setRequiredSuffix('');
}
}
return call_user_func_array('array_merge', $hints);
}
}

View File

@ -89,9 +89,6 @@ class AdminAccountPage extends Form
} }
if (count($choices) > 1) { if (count($choices) > 1) {
$this->addDescription($this->translate(
'Below are several options you can choose from for how to define the desired account.'
));
$this->addElement( $this->addElement(
'select', 'select',
'user_type', 'user_type',
@ -99,6 +96,7 @@ class AdminAccountPage extends Form
'required' => true, 'required' => true,
'autosubmit' => true, 'autosubmit' => true,
'label' => $this->translate('Type Of Definition'), 'label' => $this->translate('Type Of Definition'),
'description' => $this->translate('Choose how to define the desired account.'),
'multiOptions' => $choices, 'multiOptions' => $choices,
'value' => $choice 'value' => $choice
) )
@ -124,7 +122,7 @@ class AdminAccountPage extends Form
'label' => $this->translate('Username'), 'label' => $this->translate('Username'),
'description' => $this->translate( 'description' => $this->translate(
'Define the initial administrative account by providing a username that reflects' 'Define the initial administrative account by providing a username that reflects'
. ' a user created later or one that is authenticated using external mechanisms' . ' a user created later or one that is authenticated using external mechanisms.'
) )
) )
); );
@ -139,7 +137,7 @@ class AdminAccountPage extends Form
'label' => $this->translate('Username'), 'label' => $this->translate('Username'),
'description' => sprintf( 'description' => sprintf(
$this->translate( $this->translate(
'Choose a user reported by the %s backend as the initial administrative account', 'Choose a user reported by the %s backend as the initial administrative account.',
'setup.admin' 'setup.admin'
), ),
$this->backendConfig['backend'] === 'db' $this->backendConfig['backend'] === 'db'
@ -159,7 +157,7 @@ class AdminAccountPage extends Form
'required' => true, 'required' => true,
'label' => $this->translate('Username'), 'label' => $this->translate('Username'),
'description' => $this->translate( 'description' => $this->translate(
'Enter the username to be used when creating an initial administrative account' 'Enter the username to be used when creating an initial administrative account.'
) )
) )
); );
@ -170,7 +168,9 @@ class AdminAccountPage extends Form
'required' => true, 'required' => true,
'renderPassword' => true, 'renderPassword' => true,
'label' => $this->translate('Password'), 'label' => $this->translate('Password'),
'description' => $this->translate('Enter the password to assign to the newly created account') 'description' => $this->translate(
'Enter the password to assign to the newly created account.'
)
) )
); );
$this->addElement( $this->addElement(
@ -181,7 +181,7 @@ class AdminAccountPage extends Form
'renderPassword' => true, 'renderPassword' => true,
'label' => $this->translate('Repeat password'), 'label' => $this->translate('Repeat password'),
'description' => $this->translate( 'description' => $this->translate(
'Please repeat the password given above to avoid typing errors' 'Please repeat the password given above to avoid typing errors.'
), ),
'validators' => array( 'validators' => array(
array('identical', false, array('new_user_password')) array('identical', false, array('new_user_password'))

View File

@ -259,3 +259,13 @@ form ul.descriptions {
} }
} }
} }
form ul.hints {
.non-list-like-list;
padding: 0.5em 0.5em 0 0.5em;
li {
font-size: 0.8em;
padding-bottom: 0.5em;
}
}

View File

@ -104,7 +104,7 @@ table.benchmark {
.info-box { .info-box {
padding: 0.5em; padding: 0.5em;
border: 1px solid lightgrey; border: 1px solid lightgrey;
background-color: #fbfcc5; background-color: #f2f4fd;
} }
/* Action table */ /* Action table */