Form::createElements() should add elements instead of returning them

In case createElements() would still return the elements while requiring
the caller to add them to the form all form dependent configurations get
lost. (displaygroups, belongTo, ...) Wizards or parent forms can still
retrieve only input relevant fields by just calling createElements() and
getElements().

refs #5525
This commit is contained in:
Johannes Meyer 2014-09-03 12:21:31 +02:00
parent dd3901e891
commit 54a834266c
15 changed files with 337 additions and 337 deletions

View File

@ -26,34 +26,34 @@ class LoginForm extends Form
*/ */
public function createElements($formData) public function createElements($formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'username',
'username', array(
array( 'required' => true,
'required' => true, 'label' => t('Username'),
'label' => t('Username'), 'placeholder' => t('Please enter your username...'),
'placeholder' => t('Please enter your username...'), 'class' => false === isset($formData['username']) ? 'autofocus' : ''
'class' => false === isset($formData['username']) ? 'autofocus' : ''
)
),
$this->createElement(
'password',
'password',
array(
'required' => true,
'label' => t('Password'),
'placeholder' => t('...and your password'),
'class' => isset($formData['username']) ? 'autofocus' : ''
)
),
$this->createElement(
'hidden',
'redirect',
array(
'value' => Url::fromRequest()->getParam('redirect')
)
) )
); );
$this->addElement(
'password',
'password',
array(
'required' => true,
'label' => t('Password'),
'placeholder' => t('...and your password'),
'class' => isset($formData['username']) ? 'autofocus' : ''
)
);
$this->addElement(
'hidden',
'redirect',
array(
'value' => Url::fromRequest()->getParam('redirect')
)
);
return $this;
} }
} }

View File

@ -25,52 +25,52 @@ class AutologinBackendForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'name',
'name', array(
array( 'required' => true,
'required' => true, 'label' => t('Backend Name'),
'label' => t('Backend Name'), 'description' => t('The name of this authentication backend'),
'description' => t('The name of this authentication backend'), 'validators' => array(
'validators' => array( array(
'Regex',
false,
array( array(
'Regex', 'pattern' => '/^[^\\[\\]:]+$/',
false, 'messages' => array(
array( 'regexNotMatch' => 'The backend name cannot contain \'[\', \']\' or \':\'.'
'pattern' => '/^[^\\[\\]:]+$/',
'messages' => array(
'regexNotMatch' => 'The backend name cannot contain \'[\', \']\' or \':\'.'
)
) )
) )
) )
) )
), )
$this->createElement( );
'text', $this->addElement(
'strip_username_regexp', 'text',
array( 'strip_username_regexp',
'required' => true, array(
'label' => t('Backend Domain Pattern'), 'required' => true,
'description' => t('The domain pattern of this authentication backend'), 'label' => t('Backend Domain Pattern'),
'value' => '/\@[^$]+$/', 'description' => t('The domain pattern of this authentication backend'),
'validators' => array( 'value' => '/\@[^$]+$/',
new Zend_Validate_Callback(function ($value) { 'validators' => array(
return @preg_match($value, '') !== false; new Zend_Validate_Callback(function ($value) {
}) return @preg_match($value, '') !== false;
) })
)
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'autologin'
) )
) )
); );
$this->addElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'autologin'
)
);
return $this;
} }
/** /**

View File

@ -48,37 +48,37 @@ class DbBackendForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'name',
'name', array(
array( 'required' => true,
'required' => true, 'label' => t('Backend Name'),
'label' => t('Backend Name'), 'description' => t('The name of this authentication provider'),
'description' => t('The name of this authentication provider'),
)
),
$this->createElement(
'select',
'resource',
array(
'required' => true,
'label' => t('Database Connection'),
'description' => t('The database connection to use for authenticating with this provider'),
'multiOptions' => false === empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
)
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'db'
)
) )
); );
$this->addElement(
'select',
'resource',
array(
'required' => true,
'label' => t('Database Connection'),
'description' => t('The database connection to use for authenticating with this provider'),
'multiOptions' => false === empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
)
);
$this->addElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'db'
)
);
return $this;
} }
/** /**

View File

@ -47,57 +47,57 @@ class LdapBackendForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'name',
'name', array(
array( 'required' => true,
'required' => true, 'label' => t('Backend Name'),
'label' => t('Backend Name'), 'description' => t('The name of this authentication backend')
'description' => t('The name of this authentication backend')
)
),
$this->createElement(
'select',
'resource',
array(
'required' => true,
'label' => t('LDAP Resource'),
'description' => t('The resource to use for authenticating with this provider'),
'multiOptions' => false === empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
)
),
$this->createElement(
'text',
'user_class',
array(
'required' => true,
'label' => t('LDAP User Object Class'),
'description' => t('The object class used for storing users on the ldap server'),
'value' => 'inetOrgPerson'
)
),
$this->createElement(
'text',
'user_name_attribute',
array(
'required' => true,
'label' => t('LDAP User Name Attribute'),
'description' => t('The attribute name used for storing the user name on the ldap server'),
'value' => 'uid'
)
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'ldap'
)
) )
); );
$this->addElement(
'select',
'resource',
array(
'required' => true,
'label' => t('LDAP Resource'),
'description' => t('The resource to use for authenticating with this provider'),
'multiOptions' => false === empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
)
);
$this->addElement(
'text',
'user_class',
array(
'required' => true,
'label' => t('LDAP User Object Class'),
'description' => t('The object class used for storing users on the ldap server'),
'value' => 'inetOrgPerson'
)
);
$this->addElement(
'text',
'user_name_attribute',
array(
'required' => true,
'label' => t('LDAP User Name Attribute'),
'description' => t('The attribute name used for storing the user name on the ldap server'),
'value' => 'uid'
)
);
$this->addElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'ldap'
)
);
return $this;
} }
/** /**

View File

@ -299,8 +299,7 @@ class AuthenticationBackendConfigForm extends ConfigForm
$backendTypes['autologin'] = t('Autologin'); $backendTypes['autologin'] = t('Autologin');
} }
$elements = array(); $this->addElement(
$elements[] = $this->createElement(
'select', 'select',
'type', 'type',
array( array(
@ -315,9 +314,11 @@ class AuthenticationBackendConfigForm extends ConfigForm
if (isset($formData['force_creation']) && $formData['force_creation']) { if (isset($formData['force_creation']) && $formData['force_creation']) {
// In case another error occured and the checkbox was displayed before // In case another error occured and the checkbox was displayed before
$elements[] = $this->getForceCreationCheckbox(); $this->addElement($this->getForceCreationCheckbox());
} }
return array_merge($elements, $this->getBackendForm($backendType)->createElements($formData)); $this->addElements($this->getBackendForm($backendType)->createElements($formData)->getElements());
return $this;
} }
} }

View File

@ -27,14 +27,12 @@ class ApplicationConfigForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
$elements = array();
$languages = array(); $languages = array();
foreach (Translator::getAvailableLocaleCodes() as $language) { foreach (Translator::getAvailableLocaleCodes() as $language) {
$languages[$language] = $language; $languages[$language] = $language;
} }
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'global_language', 'global_language',
array( array(
@ -52,7 +50,7 @@ class ApplicationConfigForm extends Form
$tzList[$tz] = $tz; $tzList[$tz] = $tz;
} }
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'global_timezone', 'global_timezone',
array( array(
@ -67,7 +65,7 @@ class ApplicationConfigForm extends Form
) )
); );
$elements[] = $this->createElement( $this->addElement(
'text', 'text',
'global_modulePath', 'global_modulePath',
array( array(
@ -82,7 +80,7 @@ class ApplicationConfigForm extends Form
) )
); );
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'preferences_type', 'preferences_type',
array( array(
@ -104,7 +102,7 @@ class ApplicationConfigForm extends Form
} }
} }
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'preferences_resource', 'preferences_resource',
array( array(
@ -115,6 +113,6 @@ class ApplicationConfigForm extends Form
); );
} }
return $elements; return $this;
} }
} }

View File

@ -23,9 +23,7 @@ class LoggingConfigForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
$elements = array(); $this->addElement(
$elements[] = $this->createElement(
'select', 'select',
'logging_level', 'logging_level',
array( array(
@ -41,7 +39,7 @@ class LoggingConfigForm extends Form
) )
) )
); );
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'logging_type', 'logging_type',
array( array(
@ -57,7 +55,7 @@ class LoggingConfigForm extends Form
); );
if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') { if (false === isset($formData['logging_type']) || $formData['logging_type'] === 'syslog') {
$elements[] = $this->createElement( $this->addElement(
'text', 'text',
'logging_application', 'logging_application',
array( array(
@ -79,7 +77,7 @@ class LoggingConfigForm extends Form
) )
) )
); );
$elements[] = $this->createElement( $this->addElement(
'select', 'select',
'logging_facility', 'logging_facility',
array( array(
@ -92,7 +90,7 @@ class LoggingConfigForm extends Form
) )
); );
} elseif ($formData['logging_type'] === 'file') { } elseif ($formData['logging_type'] === 'file') {
$elements[] = $this->createElement( $this->addElement(
'text', 'text',
'logging_target', 'logging_target',
array( array(
@ -105,7 +103,7 @@ class LoggingConfigForm extends Form
); );
} }
return $elements; return $this;
} }
/** /**

View File

@ -31,11 +31,10 @@ class GeneralConfigForm extends ConfigForm
{ {
$appConfigForm = new ApplicationConfigForm(); $appConfigForm = new ApplicationConfigForm();
$loggingConfigForm = new LoggingConfigForm(); $loggingConfigForm = new LoggingConfigForm();
$this->addElements($appConfigForm->createElements($formData)->getElements());
$this->addElements($loggingConfigForm->createElements($formData)->getElements());
return array_merge( return $this;
$appConfigForm->createElements($formData),
$loggingConfigForm->createElements($formData)
);
} }
/** /**

View File

@ -29,31 +29,31 @@ class DbResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'select',
'select', 'db',
'db', array(
array( 'required' => true,
'required' => true, 'label' => t('Database Type'),
'label' => t('Database Type'), 'description' => t('The type of SQL database'),
'description' => t('The type of SQL database'), 'multiOptions' => array(
'multiOptions' => array( 'mysql' => 'MySQL',
'mysql' => 'MySQL', 'pgsql' => 'PostgreSQL'
'pgsql' => 'PostgreSQL' //'oracle' => 'Oracle'
//'oracle' => 'Oracle'
)
) )
), )
$this->createElement( );
'text', $this->addElement(
'host', 'text',
array ( 'host',
'required' => true, array (
'label' => t('Host'), 'required' => true,
'description' => t('The hostname of the database'), 'label' => t('Host'),
'value' => 'localhost' 'description' => t('The hostname of the database'),
) 'value' => 'localhost'
), )
);
$this->addElement(
new Number( new Number(
array( array(
'required' => true, 'required' => true,
@ -62,36 +62,38 @@ class DbResourceForm extends Form
'description' => t('The port to use'), 'description' => t('The port to use'),
'value' => 3306 'value' => 3306
) )
),
$this->createElement(
'text',
'dbname',
array(
'required' => true,
'label' => t('Database Name'),
'description' => t('The name of the database to use')
)
),
$this->createElement(
'text',
'username',
array (
'required' => true,
'label' => t('Username'),
'description' => t('The user name to use for authentication')
)
),
$this->createElement(
'password',
'password',
array(
'required' => true,
'renderPassword' => true,
'label' => t('Password'),
'description' => t('The password to use for authentication')
)
) )
); );
$this->addElement(
'text',
'dbname',
array(
'required' => true,
'label' => t('Database Name'),
'description' => t('The name of the database to use')
)
);
$this->addElement(
'text',
'username',
array (
'required' => true,
'label' => t('Username'),
'description' => t('The user name to use for authentication')
)
);
$this->addElement(
'password',
'password',
array(
'required' => true,
'renderPassword' => true,
'label' => t('Password'),
'description' => t('The password to use for authentication')
)
);
return $this;
} }
/** /**

View File

@ -25,26 +25,26 @@ class FileResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'filename',
'filename', array(
array( 'required' => true,
'required' => true, 'label' => t('Filepath'),
'label' => t('Filepath'), 'description' => t('The filename to fetch information from'),
'description' => t('The filename to fetch information from'), 'validators' => array(new ReadablePathValidator())
'validators' => array(new ReadablePathValidator())
)
),
$this->createElement(
'text',
'fields',
array(
'required' => true,
'label' => t('Pattern'),
'description' => t('The regular expression by which to identify columns')
)
) )
); );
$this->addElement(
'text',
'fields',
array(
'required' => true,
'label' => t('Pattern'),
'description' => t('The regular expression by which to identify columns')
)
);
return $this;
} }
} }

View File

@ -29,17 +29,17 @@ class LdapResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'hostname',
'hostname', array(
array( 'required' => true,
'required' => true, 'label' => t('Host'),
'label' => t('Host'), 'description' => t('The hostname or address of the LDAP server to use for authentication'),
'description' => t('The hostname or address of the LDAP server to use for authentication'), 'value' => 'localhost'
'value' => 'localhost' )
) );
), $this->addElement(
new Number( new Number(
array( array(
'required' => true, 'required' => true,
@ -48,36 +48,38 @@ class LdapResourceForm extends Form
'description' => t('The port of the LDAP server to use for authentication'), 'description' => t('The port of the LDAP server to use for authentication'),
'value' => 389 'value' => 389
) )
),
$this->createElement(
'text',
'root_dn',
array(
'required' => true,
'label' => t('Root DN'),
'description' => t('The path where users can be found on the ldap server')
)
),
$this->createElement(
'text',
'bind_dn',
array(
'required' => true,
'label' => t('Bind DN'),
'description' => t('The user dn to use for querying the ldap server')
)
),
$this->createElement(
'password',
'bind_pw',
array(
'required' => true,
'renderPassword' => true,
'label' => t('Bind Password'),
'description' => t('The password to use for querying the ldap server')
)
) )
); );
$this->addElement(
'text',
'root_dn',
array(
'required' => true,
'label' => t('Root DN'),
'description' => t('The path where users can be found on the ldap server')
)
);
$this->addElement(
'text',
'bind_dn',
array(
'required' => true,
'label' => t('Bind DN'),
'description' => t('The user dn to use for querying the ldap server')
)
);
$this->addElement(
'password',
'bind_pw',
array(
'required' => true,
'renderPassword' => true,
'label' => t('Bind Password'),
'description' => t('The password to use for querying the ldap server')
)
);
return $this;
} }
/** /**

View File

@ -29,18 +29,18 @@ class LivestatusResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'socket',
'socket', array(
array( 'required' => true,
'required' => true, 'label' => t('Socket'),
'label' => t('Socket'), 'description' => t('The path to your livestatus socket used for querying monitoring data'),
'description' => t('The path to your livestatus socket used for querying monitoring data'), 'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/rw/livestatus')
'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/rw/livestatus')
)
) )
); );
return $this;
} }
/** /**

View File

@ -26,29 +26,29 @@ class StatusdatResourceForm extends Form
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array( $this->addElement(
$this->createElement( 'text',
'text', 'status_file',
'status_file', array(
array( 'required' => true,
'required' => true, 'label' => t('Filepath'),
'label' => t('Filepath'), 'description' => t('Location of your icinga status.dat file'),
'description' => t('Location of your icinga status.dat file'), 'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/status.dat'),
'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/status.dat'), 'validators' => array(new ReadablePathValidator())
'validators' => array(new ReadablePathValidator())
)
),
$this->createElement(
'text',
'object_file',
array(
'required' => true,
'label' => t('Filepath'),
'description' => t('Location of your icinga objects.cache file'),
'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/objects.cache'),
'validators' => array(new ReadablePathValidator())
)
) )
); );
$this->addElement(
'text',
'object_file',
array(
'required' => true,
'label' => t('Filepath'),
'description' => t('Location of your icinga objects.cache file'),
'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/objects.cache'),
'validators' => array(new ReadablePathValidator())
)
);
return $this;
} }
} }

View File

@ -224,35 +224,35 @@ class ResourceConfigForm extends ConfigForm
$resourceTypes['db'] = t('SQL Database'); $resourceTypes['db'] = t('SQL Database');
} }
$elements = array( $this->addElement(
$this->createElement( 'text',
'text', 'name',
'name', array(
array( 'required' => true,
'required' => true, 'label' => t('Resource Name'),
'label' => t('Resource Name'), 'description' => t('The unique name of this resource')
'description' => t('The unique name of this resource') )
) );
), $this->addElement(
$this->createElement( 'select',
'select', 'type',
'type', array(
array( 'required' => true,
'required' => true, 'autosubmit' => true,
'autosubmit' => true, 'label' => t('Resource Type'),
'label' => t('Resource Type'), 'description' => t('The type of resource'),
'description' => t('The type of resource'), 'multiOptions' => $resourceTypes,
'multiOptions' => $resourceTypes, 'value' => $resourceType
'value' => $resourceType
)
) )
); );
if (isset($formData['force_creation']) && $formData['force_creation']) { if (isset($formData['force_creation']) && $formData['force_creation']) {
// In case another error occured and the checkbox was displayed before // In case another error occured and the checkbox was displayed before
$elements[] = $this->getForceCreationCheckbox(); $this->addElement($this->getForceCreationCheckbox());
} }
return array_merge($elements, $this->getResourceForm($resourceType)->createElements($formData)); $this->addElements($this->getResourceForm($resourceType)->createElements($formData)->getElements());
return $this;
} }
} }

View File

@ -269,8 +269,8 @@ class Form extends Zend_Form
public function create(array $formData = array()) public function create(array $formData = array())
{ {
if (false === $this->created) { if (false === $this->created) {
$this->addElements($this->createElements($formData)); $this->createElements($formData)
$this->addFormIdentification() ->addFormIdentification()
->addCsrfCounterMeasure() ->addCsrfCounterMeasure()
->addSubmitButton(); ->addSubmitButton();
@ -287,17 +287,17 @@ class Form extends Zend_Form
} }
/** /**
* Create and return the elements to add to this form * Create and add elements to this form
* *
* Intended to be implemented by concrete form classes. * Intended to be implemented by concrete form classes.
* *
* @param array $formData The data sent by the user * @param array $formData The data sent by the user
* *
* @return array * @return self
*/ */
public function createElements(array $formData) public function createElements(array $formData)
{ {
return array(); return $this;
} }
/** /**