Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dokuwiki
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BRIC
dokuwiki
Commits
8638ead5
Commit
8638ead5
authored
8 years ago
by
Andreas Gohr
Browse files
Options
Downloads
Patches
Plain Diff
added dropdown type to new Form system #1312
parent
96146a0b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
_test/tests/inc/form/dropdownelement.test.php
+69
-0
69 additions, 0 deletions
_test/tests/inc/form/dropdownelement.test.php
inc/Form/DropdownElement.php
+113
-0
113 additions, 0 deletions
inc/Form/DropdownElement.php
inc/Form/Form.php
+13
-0
13 additions, 0 deletions
inc/Form/Form.php
with
195 additions
and
0 deletions
_test/tests/inc/form/dropdownelement.test.php
0 → 100644
+
69
−
0
View file @
8638ead5
<?php
use
dokuwiki\Form
;
class
form_dropdownelement_test
extends
DokuWikiTest
{
function
test_defaults
()
{
$form
=
new
Form\Form
();
// basic tests
$options
=
array
(
'first'
,
'second'
,
'third'
);
$element
=
$form
->
addDropdown
(
'foo'
,
$options
,
'label text'
);
$this
->
assertEquals
(
'first'
,
$element
->
val
());
$element
->
val
(
'second'
);
$this
->
assertEquals
(
'second'
,
$element
->
val
());
$element
->
val
(
'nope'
);
$this
->
assertEquals
(
'first'
,
$element
->
val
());
// associative array
$options
=
array
(
'first'
=>
'A first Label'
,
'second'
=>
'The second Label'
,
'third'
=>
'Just 3'
);
$element
->
options
(
$options
);
$this
->
assertEquals
(
'first'
,
$element
->
val
());
$element
->
val
(
'second'
);
$this
->
assertEquals
(
'second'
,
$element
->
val
());
$element
->
val
(
'nope'
);
$this
->
assertEquals
(
'first'
,
$element
->
val
());
// HTML
$html
=
$form
->
toHTML
();
$pq
=
phpQuery
::
newDocumentXHTML
(
$html
);
$select
=
$pq
->
find
(
'select[name=foo]'
);
$this
->
assertTrue
(
$select
->
length
==
1
);
$this
->
assertEquals
(
'first'
,
$select
->
val
());
$options
=
$pq
->
find
(
'option'
);
$this
->
assertTrue
(
$options
->
length
==
3
);
$option
=
$pq
->
find
(
'option[selected=selected]'
);
$this
->
assertTrue
(
$option
->
length
==
1
);
$this
->
assertEquals
(
'first'
,
$option
->
val
());
$this
->
assertEquals
(
'A first Label'
,
$option
->
text
());
$label
=
$pq
->
find
(
'label'
);
$this
->
assertTrue
(
$label
->
length
==
1
);
$this
->
assertEquals
(
'label text'
,
$label
->
find
(
'span'
)
->
text
());
}
/**
* check that posted values overwrite preset default
*/
function
test_prefill
()
{
global
$INPUT
;
$INPUT
->
post
->
set
(
'foo'
,
'second'
);
$form
=
new
Form\Form
();
$options
=
array
(
'first'
=>
'A first Label'
,
'second'
=>
'The second Label'
,
'third'
=>
'Just 3'
);
$element
=
$form
->
addDropdown
(
'foo'
,
$options
,
'label text'
)
->
val
(
'third'
);
$this
->
assertEquals
(
'third'
,
$element
->
val
());
$html
=
$form
->
toHTML
();
$pq
=
phpQuery
::
newDocumentXHTML
(
$html
);
$option
=
$pq
->
find
(
'option[selected=selected]'
);
$this
->
assertTrue
(
$option
->
length
==
1
);
$this
->
assertEquals
(
'second'
,
$option
->
val
());
$this
->
assertEquals
(
'The second Label'
,
$option
->
text
());
}
}
This diff is collapsed.
Click to expand it.
inc/Form/DropdownElement.php
0 → 100644
+
113
−
0
View file @
8638ead5
<?php
namespace
dokuwiki\Form
;
/**
* Class DropdownElement
*
* Represents a HTML select. Please note that this does not support multiple selected options!
*
* @package dokuwiki\Form
*/
class
DropdownElement
extends
InputElement
{
protected
$options
=
array
();
protected
$value
=
''
;
/**
* @param string $name The name of this form element
* @param string $options The available options
* @param string $label The label text for this element (will be autoescaped)
*/
public
function
__construct
(
$name
,
$options
,
$label
=
''
)
{
parent
::
__construct
(
'dropdown'
,
$name
,
$label
);
$this
->
options
(
$options
);
}
/**
* Get or set the options of the Dropdown
*
* Options can be given as associative array (value => label) or as an
* indexd array (label = value).
*
* @param null|array $options
* @return $this|array
*/
public
function
options
(
$options
=
null
)
{
if
(
$options
===
null
)
return
$this
->
options
;
if
(
!
is_array
(
$options
))
throw
new
\InvalidArgumentException
(
'Options have to be an array'
);
$this
->
options
=
array
();
foreach
(
$options
as
$key
=>
$val
)
{
if
(
is_int
(
$key
))
{
$this
->
options
[
$val
]
=
(
string
)
$val
;
}
else
{
$this
->
options
[
$key
]
=
(
string
)
$val
;
}
}
$this
->
val
(
''
);
// set default value (empty or first)
return
$this
;
}
/**
* Gets or sets an attribute
*
* When no $value is given, the current content of the attribute is returned.
* An empty string is returned for unset attributes.
*
* When a $value is given, the content is set to that value and the Element
* itself is returned for easy chaining
*
* @param string $name Name of the attribute to access
* @param null|string $value New value to set
* @return string|$this
*/
public
function
attr
(
$name
,
$value
=
null
)
{
if
(
strtolower
(
$name
)
==
'multiple'
)
{
throw
new
\InvalidArgumentException
(
'Sorry, the dropdown element does not support the "multiple" attribute'
);
}
return
parent
::
attr
(
$name
,
$value
);
}
/**
* Get or set the current value
*
* When setting a value that is not defined in the options, the value is ignored
* and the first option's value is selected instead
*
* @param null|string $value The value to set
* @return $this|string
*/
public
function
val
(
$value
=
null
)
{
if
(
$value
===
null
)
return
$this
->
value
;
if
(
isset
(
$this
->
options
[
$value
]))
{
$this
->
value
=
$value
;
}
else
{
// unknown value set, select first option instead
$keys
=
array_keys
(
$this
->
options
);
$this
->
value
=
(
string
)
array_shift
(
$keys
);
}
return
$this
;
}
/**
* Create the HTML for the select it self
*
* @return string
*/
protected
function
mainElementHTML
()
{
if
(
$this
->
useInput
)
$this
->
prefillInput
();
$html
=
'<select '
.
buildAttributes
(
$this
->
attrs
())
.
'>'
;
foreach
(
$this
->
options
as
$key
=>
$val
)
{
$selected
=
(
$key
==
$this
->
value
)
?
' selected="selected"'
:
''
;
$html
.
=
'<option'
.
$selected
.
' value="'
.
hsc
(
$key
)
.
'">'
.
hsc
(
$val
)
.
'</option>'
;
}
$html
.
=
'</select>'
;
return
$html
;
}
}
This diff is collapsed.
Click to expand it.
inc/Form/Form.php
+
13
−
0
View file @
8638ead5
...
...
@@ -221,6 +221,19 @@ class Form extends Element {
return
$this
->
addElement
(
new
CheckableElement
(
'checkbox'
,
$name
,
$label
),
$pos
);
}
/**
* Adds a dropdown field
*
* @param $name
* @param array $options
* @param string $label
* @param int $pos
* @return DropdownElement
*/
public
function
addDropdown
(
$name
,
$options
,
$label
=
''
,
$pos
=
-
1
)
{
return
$this
->
addElement
(
new
DropdownElement
(
$name
,
$options
,
$label
),
$pos
);
}
/**
* Adds a textarea field
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment