Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCEMENT Support numeric array values in CheckboxSetField (?Field[…
…]=val1&Field[]=val2 instead of ?Field[val1]=1&Field[val2]=1)
- Loading branch information
Showing
3 changed files
with
47 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,10 +184,16 @@ function saveInto(DataObject $record) { | |
$fieldname = $this->name ; | ||
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) { | ||
$idList = array(); | ||
if($this->value) foreach($this->value as $id => $bool) { | ||
if($bool) { | ||
$idList[] = $id; | ||
} | ||
// 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)); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
chillu
Author
Member
|
||
if($this->value) foreach($this->value as $k => $v) { | ||
if($valuesInKeys) { | ||
if($v) $idList[] = $k; | ||
} else { | ||
$idList[] = $v; | ||
} | ||
} | ||
$record->$fieldname()->setByIDList($idList); | ||
} elseif($fieldname && $record) { | ||
|
@@ -214,7 +220,6 @@ function dataValue() { | |
$filtered[] = str_replace(",", "{comma}", $item); | ||
} | ||
} | ||
|
||
return implode(',', $filtered); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This looks dodgy. What if the data just happens to meet the
ArrayLib::is_associative
criteria but isn't supposed to be used that way? Perhaps you should do check the field name instead - if it ends with[]
then you know that the values are supposed to be in theOf course, this need to deal with two completely different data formats is a result of the fact that you want to butcher
CheckboxSetField
to turn it into aMultiItemSelectField
. Perhaps it would be better to separate these two field types explicitly? Yes, the data is similar but there's a lot about the two fields that are different.The dev cycle appears to have been "oh, hey, it's just a template update! oh, wait, there's also a data format change." I'm not sure what the value in keeping these two fields within a single class is, but I'd suggest you reconsider.