Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add "preset" attribute to form fields and adapt list/checkboxes multiple default/preset values #1257

Closed
wants to merge 1 commit into from

3 participants

@chdemko

It's a solution for Joomla CMS [#28402] JFormFieldList (and derivatives) do not utilize multiple default values when multiple=true (http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=28402)

It has been extended for supporting preset values in form fields

in an single valued field, you can now use

<field name="text1" type="text" preset="Preset value" />
<field name="text2" type="text" preset="KEY_TO_PRESET_VALUE" translate_preset="true" />

This will fill the field the first time the form is edited

in a list/checkboxes

<field name="list1" type="list" default="1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</field>

This will fill the field the with 1 if the current value is empty

<field name="list2" type="list" preset="1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</field>

This will fill the field the with 1 the first time the form is edited

<field name="list3" type="list">
    <option value="1" default="true">1</option>
    <option value="2">2</option>
    <option value="3" default="true">3</option>
</field>

This will fill the field the with 1,3 if the current value is empty

<field name="list4" type="list">
    <option value="1" preset="true">1</option>
    <option value="2">2</option>
    <option value="3" preset="true">3</option>
</field>

This will fill the field the with 1,3 the first time the form is edited

@realityking realityking commented on the diff
libraries/joomla/form/fields/list.php
((33 lines not shown))
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
- $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
- $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
@realityking Collaborator

How does this work? If they all have the same name I'd think only one would get send to the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@elinw

We've had quite a big discussion on the platform list about how to store multiselect values, does this mean that the maintainers have reached consensus on this being the right approach?

@Roveun
I think in a list you would not have the same control name for all of the values, that's only for things like checkboxes and radio buttons.

I would think you need a test with this.

@chdemko

@realityking , for multiple field the name is 'myname[]' (note the []) this is why you can store multiple value

@elinw I don't see these posts. Can you post the urls here?

@elinw

https://groups.google.com/forum/?fromgroups#!searchin/joomla-dev-general/multiple$20field/joomla-dev-general/SVZxhtQN_II/7kI2u4w0Si8J is one place.

As far as I understand what Sam said, the platform wants to stay neutral in deciding whether multiple values are stored as normal strings, json strings or something else and basically it's up to developers to implement. So I'm not against making it possible to do this, I just want to be clear on what is being done here. This is just about the rendered form, not about what is saved.
I do think that it would be good to allow us to use the native html to use the selected element for multiple options but i'm not sure why we should call it preset when it is called selected in the standard and also why we wouldn't just add selected so that it matches conventional use rather than make up our own term.
http://www.w3.org/wiki/HTML/Elements/select#Example_B
http://www.w3.org/TR/html401/interact/forms.html#h-17.6.1

@chdemko

No problem to change

preset="value|true" to selected="value|true"

but for text fields selected="something" is not accurate

@elinw

Check boxes and lists are extremely different

In text/textarea the idea of selected doesn't make sense since there is no select.
In a list you just should really put selected as an element of the option. The problem with that is that you have lists that are generated from the database or something like the integer field which generates its own dropdown so that's why you really want to have it in the field definition or maybe let the form developer choose one way or the other.
I'm not sure what you mean by selected="true" it is a boolean so it is either present or not present if you are using it in that way. If you said selected = "true" it would preselect a value equal to true.
What happens if the selected value(s) are not present in the list? Are they just ignored?

So the proper rendered html is:

<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3" selected>3</option>

Checkboxes shoudl render
checked="checked" rather than selected.

http://www.w3.org/TR/html401/interact/forms.html
http://www.w3.org/WAI/UA/TS/html401/cp0101/0101-CHECKBOX.html

For check boxes we already have the checked element .
https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/form/fields/checkboxes.php#L67

So to me the only real issue is in lists.

@chdemko

In text/textarea, the idea of preset="Enter your text here..." can be used

the preset attribute is only for fields edited the first time (text/list/checkboxes...)

For examples, look at the test code

If values are not present in the list, they will be ignored

@chdemko chdemko closed this
@chdemko

Closing this one (need to play with it)

@elinw

I did a pull request to fix the preset (checked) issues in checkboxes specificially. #1268
Also I fixed a problem with the checked in checkbox. #1270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 7, 2012
  1. @chdemko
This page is out of date. Refresh to see the latest.
View
50 libraries/joomla/form/fields/checkboxes.php
@@ -58,13 +58,55 @@ protected function getInput()
// Get the field options.
$options = $this->getOptions();
+ // Get the value(s)
+ if (empty($this->value))
+ {
+ // Filter the option using the preset flag
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->preset;
+ }
+ );
+
+ // If the data exists or there is no preset
+ if ($this->form->existValue($this->fieldname, $this->group) || empty($filter))
+ {
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->default;
+ }
+ );
+ }
+
+ // Extract the values from the filter
+ $values = array_map(
+ function ($option)
+ {
+ return $option->value;
+ },
+ $filter
+ );
+ }
+ elseif (is_array($this->value))
+ {
+ $values = $this->value;
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+
// Build the checkbox field output.
$html[] = '<ul>';
foreach ($options as $i => $option)
{
// Initialize some option attributes.
- $checked = (in_array((string) $option->value, (array) $this->value) ? ' checked="checked"' : '');
+ $checked = (in_array((string) $option->value, $values) ? ' checked="checked"' : '');
$class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
$disabled = !empty($option->disable) ? ' disabled="disabled"' : '';
@@ -119,6 +161,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
View
64 libraries/joomla/form/fields/list.php
@@ -59,16 +59,68 @@ protected function getInput()
// Get the field options.
$options = (array) $this->getOptions();
+ // Get the value(s)
+ if ($this->multiple)
+ {
+ if (empty($this->value))
+ {
+ // Filter the option using the preset flag
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->preset;
+ }
+ );
+
+ // If the data exists or there is no preset
+ if ($this->form->existValue($this->fieldname, $this->group) || empty($filter))
+ {
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->default;
+ }
+ );
+ }
+
+ // Extract the values from the filter
+ $values = array_map(
+ function ($option)
+ {
+ return $option->value;
+ },
+ $filter
+ );
+ }
+ elseif (is_array($this->value))
+ {
+ $values = $this->value;
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
- $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
- $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
@realityking Collaborator

How does this work? If they all have the same name I'd think only one would get send to the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
}
// Create a regular list.
else
{
- $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
+ $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
}
return implode($html);
@@ -108,6 +160,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
View
56 libraries/joomla/form/form.php
@@ -622,6 +622,31 @@ public function getValue($name, $group = null, $default = null)
}
/**
+ * Method to test if the value of a field exists.
+ *
+ * @param string $name The name of the field for which to get the value.
+ * @param string $group The optional dot-separated form group path on which to get the value.
+ *
+ * @return bool TRUE on success, FALSE on failure.
+ *
+ * @since 12.2
+ */
+ public function existValue($name, $group = null)
+ {
+ // If a group is set use it.
+ if ($group)
+ {
+ $return = $this->data->exists($group . '.' . $name);
+ }
+ else
+ {
+ $return = $this->data->exists($name);
+ }
+
+ return $return;
+ }
+
+ /**
* Method to load the form description from an XML string or object.
*
* The replace option works per field. If a field being loaded already exists in the current
@@ -1667,6 +1692,9 @@ protected function loadField($element, $group = null, $value = null)
return false;
}
+ // Get the field name.
+ $name = (string) $element['name'];
+
// Get the field type.
$type = $element['type'] ? (string) $element['type'] : 'text';
@@ -1687,6 +1715,7 @@ protected function loadField($element, $group = null, $value = null)
*/
if ($value === null)
{
+ // Get the default value
$default = (string) $element['default'];
if (($translate = $element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1'))
{
@@ -1702,7 +1731,32 @@ protected function loadField($element, $group = null, $value = null)
$default = JText::_($default);
}
}
- $value = $this->getValue((string) $element['name'], $group, $default);
+
+ // Get the value
+ if ($this->existValue($name, $group) || !isset($element['preset']))
+ {
+ $value = $this->getValue($name, $group, $default);
+ }
+ else
+ {
+ // Get the preset value
+ $preset = (string) $element['preset'];
+ if (($translate = $element['translate_preset']) && ((string) $translate == 'true' || (string) $translate == '1'))
+ {
+ $lang = JFactory::getLanguage();
+ if ($lang->hasKey($default))
+ {
+ $debug = $lang->setDebug(false);
+ $preset = JText::_($preset);
+ $lang->setDebug($debug);
+ }
+ else
+ {
+ $preset = JText::_($preset);
+ }
+ }
+ $value = $this->getValue((string) $element['name'], $group, $preset);
+ }
}
// Setup the JFormField object.
View
5 tests/suites/unit/joomla/form/JFormDataHelper.php
@@ -249,6 +249,11 @@ class JFormDataHelper
name="title"
type="text"
description="The title." />
+ <field
+ name="alias"
+ type="text"
+ description="The alias."
+ preset="preset-alias" />
<fields
name="params">
<field
View
49 tests/suites/unit/joomla/form/JFormTest.php
@@ -814,10 +814,17 @@ public function testGetField()
'Line:'.__LINE__.' Prior to binding data, the defaults in the field should be used.'
);
+ $this->assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo('preset-alias'),
+ 'Line:'.__LINE__.' Prior to binding data, the presets in the field should be used.'
+ );
+
// Check values after binding.
$data = array(
'title' => 'The title',
+ 'alias' => '',
'show_title' => 3,
'params' => array(
'show_title' => 2,
@@ -837,6 +844,12 @@ public function testGetField()
);
$this->assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo(''),
+ 'Line:'.__LINE__.' Check the field value bound correctly.'
+ );
+
+ $this->assertThat(
$form->getField('show_title', 'params')->value,
$this->equalTo(2),
'Line:'.__LINE__.' Check the field value bound correctly.'
@@ -1138,6 +1151,42 @@ public function testGetValue()
}
/**
+ * Test for JForm::existValue method.
+ */
+ public function testExistValue()
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load(JFormDataHelper::$loadFieldDocument),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(false),
+ 'Line:'.__LINE__.' The value should not exist.'
+ );
+
+ $data = array(
+ 'title' => 'Avatar',
+ );
+
+ $this->assertThat(
+ $form->bind($data),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(true),
+ 'Line:'.__LINE__.' The value should exist.'
+ );
+ }
+
+ /**
* Test for JForm::getFieldset method.
*/
public function testGetFieldset()
View
123 tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php
@@ -48,4 +48,127 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no default, no preset, existing value
+ array(
+ '',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, no preset, existing value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, no preset, empty value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '3',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, empty value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ array('2', '3'),
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load('<form><field name="checkboxes" type="checkboxes" ' . $attributes . ' >' . $options . '</field></form>'),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('checkboxes' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}
View
289 tests/suites/unit/joomla/form/fields/JFormFieldListTest.php
@@ -53,4 +53,293 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no multiple, no default, no preset, existing value
+ array(
+ '',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // multiple, no default, no preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, no preset, existing value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, no preset, empty value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, no default, no preset, existing value, readonly
+ array(
+ 'readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="" disabled="disabled">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list" value=""/>',
+'<select id="list" name="" disabled="disabled">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list" value="2"/>'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list[]" value="2"/>'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ array('1', '2'),
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list[]" value="1"/><input type="hidden" name="list[]" value="2"/>'
+ ),
+
+ // no multiple, one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '3',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, empty value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '2',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ array('2', '3'),
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load('<form><field name="list" type="list" ' . $attributes . ' >' . $options . '</field></form>'),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('list' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}
Something went wrong with that request. Please try again.