Skip to content

Commit

Permalink
Merge pull request #78 from SilbinaryWolf/fix-editdataobject
Browse files Browse the repository at this point in the history
fix(Validation): Fix bug where saving an existing DataObject via a Form causes validate() to have the error:  "Invalid file ID sent."
  • Loading branch information
Aaron Carlino authored May 30, 2017
2 parents c92f9f6 + a9b7370 commit bd14e95
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions code/FileAttachmentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,39 @@ public function validate($validator) {
}

/**
* @param int|array $val
* @param array|DataObject $data
* @return $this
*/
public function setValue($val, $data = array()) {
if (!$val && $data && $data instanceof DataObject && $data->exists()) {
// NOTE: This stops validation errors from occuring when editing
// an already saved DataObject.
$fieldName = $this->getName();
$ids = array();
if ($data->hasOneComponent($fieldName)) {
$id = $data->{$fieldName.'ID'};
if ($id) {
$ids[] = $id;
}
} else if ($data->hasManyComponent($fieldName) || $data->manyManyComponent($fieldName)) {
$files = $data->{$fieldName}();
if ($files) {
foreach ($files as $file) {
if (!$file->exists()) {
continue;
}
$ids[] = $file->ID;
}
}
}
if ($ids) {
$this->addValidFileIDs($ids);
}
}
if ($data && is_array($data) && isset($data[$this->getName()])) {
// Prevent Form::loadDataFrom() from loading invalid File IDs
// that may have been passed.
$isInvalid = false;
$validIDs = $this->getValidFileIDs();
// NOTE(Jake): If the $data[$name] is an array, its coming from 'loadDataFrom'
Expand Down

0 comments on commit bd14e95

Please sign in to comment.