Hi there,
I have a file upload which can be updated in the update view, but at the moment there’s no facility to simply delete the file. What’s the best way to do this?
Thanks in advance!
Hi there,
I have a file upload which can be updated in the update view, but at the moment there’s no facility to simply delete the file. What’s the best way to do this?
Thanks in advance!
I would do this:
display an uploaded image
somewhere near display a link ‘remove’ that will hide the image and set a hidden field’s value to something
next to it display a file upload control
if there is no new file uploaded and the hidden field was set, remove the image, otherwise replace it or do nothing
This could be simplified by just using a checkbox like ‘remove this image’.
OK cool thanks, I’ll give that a try.
OK Noob question: How do I get the POST data from the checkbox in actionUpdate()?
$model->attributes->checkboxName doesn’t seem to work
Show us the view fragment that renders the checkbox and code that tries to read it.
Ah, OK I don’t think I’m rendering the checkbox correctly.
OK, so I got it working like this:
Added a checkbox to _form.php
<div class="row">
<?php echo $form->labelEx($model,'myFile'); ?>
<!--if it exists, display the current myFile file name-->
<?php if($model->myFile) {
echo 'Current myFile file:' . $model->myFile;
}?>
<br />
<?php echo $form->fileField($model,'myFile'); ?>
<?php echo $form->error($model,'myFile'); ?>
<!--if myFile exists, display the delete myFile checkbox-->
<?php if($model->myFile) {
echo $form->checkBox($model,'myFile');
} ?>
</div>
Then added this to the controller actionUpdate():
//delete old myFile file if the checkbox is checked in the update view
if($model->attributes->myFile !== 1) {
$model->myFile = '';
//file delete code goes here
}
OK forget that answer! It works but not for the reasons I want it to.
Can anyone give me a better solution please?
Replaced controller actionUpdate() code with this for more intuitive solution:
//delete old myFile file if the checkbox is checked in the update view
if($_POST['MyModel']['myFile'] == 1) {
$model->myFile = '';
}
Anyone got any better solutions? I’m guessing this is functionality that people have to add on pretty much every project.