Skip to content

Commit

Permalink
Possibility to restore original value of the field when upload failed.
Browse files Browse the repository at this point in the history
It helps to keep original value if we just have file field on form and don't want to upload it but keep original file.
  • Loading branch information
satanTime committed Jun 9, 2016
1 parent b2b9ff6 commit 66f092d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ passed in under each field in your behavior configuration.
- ``keepFilesOnDelete``: Keep *all* files when deleting a record.

- Default: (boolean) ``true``

- ``restoreValueOnFailure``: Restores original value of the current field when uploaded file has error

- Defaults: (boolean) ``true``
4 changes: 4 additions & 0 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
foreach ($this->config() as $field => $settings) {
if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) {
if (Hash::get($settings, 'restoreValueOnFailure', true)) {
$entity->set($field, $entity->getOriginal($field));
$entity->dirty($field, false);
}
continue;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,29 @@ public function testBeforeMarshalEmptyAllowed()

public function testBeforeSaveUploadError()
{
$originalValue = rand(1000, 9999);

$methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']);
$behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $this->settings]);
$behavior->config($this->settings);
$this->entity->expects($this->any())
->method('get')
->with('field')
->will($this->returnValue($this->dataError['field']));
$this->entity->expects($this->any())
->method('get')
->with('field')
->will($this->returnValue($this->dataError['field']));
$this->entity->expects($this->any())
->method('getOriginal')
->with('field')
->will($this->returnValue($originalValue));
$this->entity->expects($this->once())
->method('set')
->with('field', $originalValue);
$this->entity->expects($this->once())
->method('dirty')
->with('field', false);
$this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject));
}

Expand Down Expand Up @@ -228,6 +244,19 @@ public function testBeforeSaveOk()
$this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject));
}

public function testBeforeSaveDoesNotRestoreOriginalValue()
{
$settings = $this->settings;
$settings['field']['restoreValueOnFailure'] = false;

$methods = array_diff($this->behaviorMethods, ['config', 'beforeSave']);
$behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$this->table, $this->settings]);
$behavior->config($settings);
$this->entity->expects($this->never())->method('getOriginal');
$this->entity->expects($this->never())->method('set');
$behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject);
}

public function testAfterDeleteOk()
{
$methods = array_diff($this->behaviorMethods, ['config', 'afterDelete']);
Expand Down

0 comments on commit 66f092d

Please sign in to comment.