Skip to content

Commit

Permalink
fix(Validation): Fix bug where saving an existing DataObject via a Fo…
Browse files Browse the repository at this point in the history
…rm causes validate() to have the error: "Invalid file ID sent."
  • Loading branch information
Jake Bentvelzen committed May 1, 2017
1 parent 4232822 commit a9b7370
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 a9b7370

Please sign in to comment.