Browse files

Revert "ENHANCEMENT Support numeric array values in CheckboxSetField …

…(?Field[]=val1&Field[]=val2 instead of ?Field[val1]=1&Field[val2]=1)" - data handling with numeric/associative arrays too clumsy, fixed up ListboxField instead

This reverts commit 8fa2664.
  • Loading branch information...
1 parent e8ad2c2 commit 344899ab77234b95c56afee509505a59ad1c8658 @chillu chillu committed Mar 5, 2012
Showing with 17 additions and 47 deletions.
  1. +5 −10 forms/CheckboxSetField.php
  2. +1 −35 tests/forms/CheckboxSetFieldTest.php
  3. +11 −2 tests/forms/CheckboxSetFieldTest.yml
View
15 forms/CheckboxSetField.php
@@ -184,16 +184,10 @@ function saveInto(DataObject $record) {
$fieldname = $this->name ;
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
$idList = array();
- // Works for both <select multiple> style - array(0 => 'val1', 1 => 'val2')
- // and <input type="checkbox"> style - array('val1' => true, 'val2' => true).
- // The <select multiple> element doesn't allow for individual keys in parameter names.
- $valuesInKeys = (ArrayLib::is_associative($this->value));
- if($this->value) foreach($this->value as $k => $v) {
- if($valuesInKeys) {
- if($v) $idList[] = $k;
- } else {
- $idList[] = $v;
- }
+ if($this->value) foreach($this->value as $id => $bool) {
+ if($bool) {
+ $idList[] = $id;
+ }
}
$record->$fieldname()->setByIDList($idList);
} elseif($fieldname && $record) {
@@ -220,6 +214,7 @@ function dataValue() {
$filtered[] = str_replace(",", "{comma}", $item);
}
}
+
return implode(',', $filtered);
}
View
36 tests/forms/CheckboxSetFieldTest.php
@@ -65,7 +65,7 @@ function testSaveWithNothingSelected() {
);
}
- function testSaveWithAssociativeArrayValueSet() {
+ function testSaveWithArrayValueSet() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
@@ -98,40 +98,6 @@ function testSaveWithAssociativeArrayValueSet() {
'Data shold be saved into CheckboxSetField manymany relation table on the "left end"'
);
}
-
- function testSaveWithNumericArrayValueSet() {
- $article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
- $articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
- $tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
- $tag2 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
-
- /* Create a CheckboxSetField with 2 items selected. Note that the array is in the format (key) => (selected) */
- $field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
- $field->setValue(array(
- $tag1->ID,
- $tag2->ID
- ));
-
- /* Saving should work */
- $field->saveInto($article);
-
- $this->assertEquals(
- array($tag1->ID,$tag2->ID),
- DB::query("SELECT \"CheckboxSetFieldTest_TagID\"
- FROM \"CheckboxSetFieldTest_Article_Tags\"
- WHERE \"CheckboxSetFieldTest_Article_Tags\".\"CheckboxSetFieldTest_ArticleID\" = $article->ID
- ")->column(),
- 'Data shold be saved into CheckboxSetField manymany relation table on the "right end"'
- );
- $this->assertEquals(
- array($articleWithTags->ID,$article->ID),
- DB::query("SELECT \"CheckboxSetFieldTest_ArticleID\"
- FROM \"CheckboxSetFieldTest_Article_Tags\"
- WHERE \"CheckboxSetFieldTest_Article_Tags\".\"CheckboxSetFieldTest_TagID\" = $tag1->ID
- ")->column(),
- 'Data shold be saved into CheckboxSetField manymany relation table on the "left end"'
- );
- }
function testLoadDataFromObject() {
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
View
13 tests/forms/CheckboxSetFieldTest.yml
@@ -3,8 +3,17 @@ CheckboxSetFieldTest_Tag:
Title: Tag 1
tag2:
Title: Tag 2
- tag3:
- Title: Tag 3
+CheckboxSetFieldTest_Article:
+ articlewithouttags:
+ Content: Article 1
+ articlewithtags:
+ Content: Article 2
+ Tags: =>CheckboxSetFieldTest_Tag.tag1,=>CheckboxSetFieldTest_Tag.tag2
+CheckboxSetFieldTest_Tag:
+ tag1:
+ Title: Tag 1
+ tag2:
+ Title: Tag 2
CheckboxSetFieldTest_Article:
articlewithouttags:
Content: Article 1

0 comments on commit 344899a

Please sign in to comment.