It should not be a form's responsibility how its submit button should look..

...in case it's a standard button. No need to be DRY here.

refs #5525
This commit is contained in:
Johannes Meyer 2014-08-22 15:20:54 +02:00
parent 403f745488
commit 7b221e2aba
15 changed files with 72 additions and 168 deletions

View File

@ -84,7 +84,7 @@ class DashboardController extends ActionController
$form = new AddUrlForm(); $form = new AddUrlForm();
$request = $this->getRequest(); $request = $this->getRequest();
if ($request->isPost()) { if ($request->isPost()) {
if ($form->isValid($request->getPost()) && $form->getElement('btn_submit')->isChecked()) { if ($form->isValid($request->getPost()) && $form->isSubmitted()) {
$dashboard = $this->getDashboard(); $dashboard = $this->getDashboard();
$dashboard->setComponentUrl( $dashboard->setComponentUrl(
$form->getValue('pane'), $form->getValue('pane'),

View File

@ -18,6 +18,7 @@ class LoginForm extends Form
public function init() public function init()
{ {
$this->setName('form_login'); $this->setName('form_login');
$this->setSubmitLabel(t('Login'));
} }
/** /**
@ -55,19 +56,4 @@ class LoginForm extends Form
) )
); );
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Login')
)
);
}
} }

View File

@ -17,6 +17,7 @@ class AutologinBackendForm extends BaseBackendForm
public function init() public function init()
{ {
$this->setName('form_config_authentication_autologin'); $this->setName('form_config_authentication_autologin');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**

View File

@ -11,23 +11,6 @@ use Icinga\Web\Form;
*/ */
abstract class BaseBackendForm extends Form abstract class BaseBackendForm extends Form
{ {
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Return whether the given values are complete/valid and check whether it is possible to connect to the backend * Return whether the given values are complete/valid and check whether it is possible to connect to the backend
* *

View File

@ -31,6 +31,7 @@ class DbBackendForm extends BaseBackendForm
public function init() public function init()
{ {
$this->setName('form_config_authentication_db'); $this->setName('form_config_authentication_db');
$this->setSubmitLabel(t('Save Changes'));
$dbResources = array_keys( $dbResources = array_keys(
ResourceFactory::getResourceConfigs('db')->toArray() ResourceFactory::getResourceConfigs('db')->toArray()

View File

@ -31,6 +31,7 @@ class LdapBackendForm extends BaseBackendForm
public function init() public function init()
{ {
$this->setName('form_config_authentication_ldap'); $this->setName('form_config_authentication_ldap');
$this->setSubmitLabel(t('Save Changes'));
$ldapResources = array_keys( $ldapResources = array_keys(
ResourceFactory::getResourceConfigs('ldap')->toArray() ResourceFactory::getResourceConfigs('ldap')->toArray()

View File

@ -17,20 +17,6 @@ class ConfirmRemovalForm extends Form
public function init() public function init()
{ {
$this->setName('form_confirm_removal'); $this->setName('form_confirm_removal');
} $this->setSubmitLabel(t('Confirm Removal'));
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Confirm Removal')
)
);
} }
} }

View File

@ -23,6 +23,7 @@ class GeneralForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_general'); $this->setName('form_config_general');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -43,23 +44,6 @@ class GeneralForm extends Form
); );
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Populate this form with the given configuration * Populate this form with the given configuration
* *

View File

@ -21,6 +21,7 @@ class ResourceForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_resource'); $this->setName('form_config_resource');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -74,23 +75,6 @@ class ResourceForm extends Form
} }
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Return whether the given values are complete/valid and check whether it is possible to connect to the resource * Return whether the given values are complete/valid and check whether it is possible to connect to the resource
* *

View File

@ -19,6 +19,7 @@ class AddUrlForm extends Form
public function init() public function init()
{ {
$this->setName('form_dashboard_addurl'); $this->setName('form_dashboard_addurl');
$this->setSubmitLabel(t('Add To Dashboard'));
} }
/** /**
@ -104,23 +105,6 @@ class AddUrlForm extends Form
return $elements; return $elements;
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Add To Dashboard')
)
);
return $this;
}
/** /**
* Return the names and titles of the available dashboard panes as key-value array * Return the names and titles of the available dashboard panes as key-value array
* *

View File

@ -20,6 +20,7 @@ class GeneralForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_preferences'); $this->setName('form_config_preferences');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -123,23 +124,6 @@ class GeneralForm extends Form
return $elements; return $elements;
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Populate the form with the given preferences * Populate the form with the given preferences
* *

View File

@ -23,6 +23,13 @@ class Form extends Zend_Form
*/ */
protected $created = false; protected $created = false;
/**
* Label to use for the standard submit button
*
* @var string
*/
protected $submitLabel;
/** /**
* The view script to use when rendering this form * The view script to use when rendering this form
* *
@ -46,6 +53,29 @@ class Form extends Zend_Form
*/ */
protected $tokenElementName = 'CSRFToken'; protected $tokenElementName = 'CSRFToken';
/**
* Set the label to use for the standard submit button
*
* @param string $label The label to use for the submit button
*
* @return self
*/
public function setSubmitLabel($label)
{
$this->submitLabel = $label;
return $this;
}
/**
* Return the label being used for the standard submit button
*
* @return string
*/
public function getSubmitLabel()
{
return $this->submitLabel;
}
/** /**
* Set the view script to use when rendering this form * Set the view script to use when rendering this form
* *
@ -162,12 +192,24 @@ class Form extends Zend_Form
/** /**
* Add a submit button to this form * Add a submit button to this form
* *
* Intended to be implemented by concrete form classes. * Uses the label previously set with Form::setSubmitLabel(). Overwrite this
* method in order to add multiple submit buttons or one with a custom name.
* *
* @return self * @return self
*/ */
public function addSubmitButton() public function addSubmitButton()
{ {
if ($this->submitLabel !== null) {
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => $this->submitLabel
)
);
}
return $this; return $this;
} }
@ -261,6 +303,22 @@ class Form extends Zend_Form
return empty($missingValues); return empty($missingValues);
} }
/**
* Return whether the submit button of this form was pressed
*
* When overwriting Form::addSubmitButton() be sure to overwrite this method as well.
*
* @return bool True in case it was pressed, False otherwise or no submit label was set
*/
public function isSubmitted()
{
if ($this->submitLabel !== null) {
return $this->getElement('btn_submit')->isChecked();
}
return false;
}
/** /**
* Return whether the given values (possibly incomplete) are valid * Return whether the given values (possibly incomplete) are valid
* *

View File

@ -18,6 +18,7 @@ class BackendForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_monitoring_backends'); $this->setName('form_config_monitoring_backends');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -73,23 +74,6 @@ class BackendForm extends Form
); );
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Return the backend configuration values and its name * Return the backend configuration values and its name
* *

View File

@ -20,6 +20,7 @@ class InstanceForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_monitoring_instances'); $this->setName('form_config_monitoring_instances');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -120,23 +121,6 @@ class InstanceForm extends Form
return $elements; return $elements;
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
/** /**
* Return the instance configuration values and its name * Return the instance configuration values and its name
* *

View File

@ -17,6 +17,7 @@ class SecurityForm extends Form
public function init() public function init()
{ {
$this->setName('form_config_monitoring_security'); $this->setName('form_config_monitoring_security');
$this->setSubmitLabel(t('Save Changes'));
} }
/** /**
@ -38,21 +39,4 @@ class SecurityForm extends Form
) )
); );
} }
/**
* @see Form::addSubmitButton()
*/
public function addSubmitButton()
{
$this->addElement(
'submit',
'btn_submit',
array(
'ignore' => true,
'label' => t('Save Changes')
)
);
return $this;
}
} }