Checking if a file has been uploaded

Notice: This thread is very old.
Member | 2

Hi, here's one super trivial question. I'm slowly growing more confident with Nette but this struck me:

I have an add/edit form in my admin module for editing products and there is one optional file upload for images.

public function createComponentAddEditForm()
	$form = new Form();
	$form->addText('name', 'Name');
	$form->addUpload('main_img', 'Main img')
	$form->addSubmit('send', 'Send!');
	$form->onSuccess[] = array($this, 'processProductForm');
	return $form;

Everything works as it should when I specify file to upload, but when I'm only editing other fields and I want to leave the image unchanged, it always overwrites it with a new upload (even when empty).

Now I check the file after uploading using isOk() method

$file = $values['main_img'];

if ($file->isOk()) {
	// Doing stuff

which apparently returns true even if the file is empty. However, I found a method isFilled() in Nette\Forms\Controls\UploadControl class, which should do the trick. But where do I call this function? In the createComponentAddEditForm() function? That looks ugly and out of place. And when I do, I check if the field was left blank and then what? Where and how do I pass the information further?

EDIT: My bad. The function isOk() actually correctly returns false, so there's no need for hacking the app into using anything else. The issue in my case was that if there's no file selected for upload, the form field would just return an empty string that gets written into the database, overwriting the path for an original image, which I've taken care of already.

Last edited by ddx (2014-07-13 20:38)