Ask your Symfony questions! Pay money and get answers fast! (more info)

File Upload in Symfony 1.4 Doctrine Admin Generator Symfony

  • SOLVED

I'd like to upload files via a generated backend.

I've changed the Form class to

class AnnouncingBoxForm extends BaseAnnouncingBoxForm {
public function configure() {
$this->widgetSchema['image_file'] = new sfWidgetFormInputFile();
}
}


When I try to upload a file through the generated backend, an error message flicks up for a very short time:

Warning: strlen() expects parameter 1 to be string, array given in ...../src/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php on line 198
Call Stack:
0.0001 660744 1. {main}() ...../src/web/backend_dev.php:0
0.0577 15450024 2. sfContext->dispatch() ...../src/web/backend_dev.php:13
0.0577 15450104 3. sfFrontWebController->dispatch() ...../src/lib/vendor/symfony/lib/util/sfContext.class.php:170
0.0578 15481816 4. sfController->forward() ...../src/lib/vendor/symfony/lib/controller/sfFrontWebController.class.php:48
0.0624 16687360 5. sfFilterChain->execute() ...../src/lib/vendor/symfony/lib/controller/sfController.class.php:238

Notice: Array to string conversion in ...../src/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection/Statement.php on line 252
Call Stack:
0.0001 660744 1. {main}() ...../src/web/backend_dev.php:0
0.0577 15450024 2. sfContext->dispatch() ...../src/web/backend_dev.php:13
0.0577 15450104 3. sfFrontWebController->dispatch() ...../src/lib/vendor/symfony/lib/util/sfContext.class.php:170
0.0578 15481816 4. sfController->forward() ...../src/lib/vendor/symfony/lib/controller/sfFrontWebController.class.php:48
0.0624 16687360 5. sfFilterChain->execute() ...../src/lib/vendor/symfony/lib/controller/sfController.class.php:238
0.0625 16689440 6. sfRenderingFilter->execute() ...../src/lib/vendor/symfony/lib/filter/sfFilterChain.class.php:53
0.0625 16689440 7. sfFilterChain->execute() ...../src/lib/vendor/symfony/lib/filter/sfRenderingFilter.class.php:33
0.0627 16691448 8. sfBasicSecurityFilter->execute() ...../src/lib/vendor/symfony/lib/filter/sfFilterChain.class.php:53

Warning: Cannot modify header information - headers already sent by (output started at ...../src/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php:198) in ...../src/lib/vendor/symfony/lib/response/sfWebResponse.class.php on line 357
Call Stack:
0.0001 660744 1. {main}() ...../src/web/backend_dev.php:0
0.0577 15450024 2. sfContext->dispatch() ...../src/web/backend_dev.php:13
0.0577 15450104 3. sfFrontWebController->dispatch() ...../src/lib/vendor/symfony/lib/util/sfContext.class.php:170
0.0578 15481816 4. sfController->forward() ...../src/lib/vendor/symfony/lib/controller/sfFrontWebController.class.php:48
0.0624 16687360 5. sfFilterChain->execute() ...../src/lib/vendor/symfony/lib/controller/sfController.class.php:238
0.0625 16689440 6. sfRenderingFilter->execute() ...../src/lib/vendor/symfony/lib/filter/sfFilterChain.class.php:53
0.0625 16689440 7. sfFilterChain->execute() ...../src/lib/vendor/symfony/lib/filter/sfRenderingFilter.class.php:33


before I get redirected to the "success" page claiming "The item was created successfully."

The database contains the String "Array" in the image_file column that's defined like this:


columns:
...
image_file: string(255)

Answers (3)

2011-09-23

Benjamin Lévêque answers:

You must defined a validator for the image file widget

$this->validatorSchema['image_file'] = new sfValidatorFile(array('path' => 'upload dir '));

Path option allow you to choose where the file is upload.


sn comments:

Thanks! This works. However, the error message still appears.

2011-09-23

Alex Zgorzhelsky answers:

You have to validate your image field with sfValidatorFile, like this

$this->widgetSchema['image_file'] = new sfWidgetFormInputFile();
$this->validatorSchema['image_file'] = new sfValidatorFile(array(
'path' => sfConfig::get('sf_upload_dir').'/'.sfConfig::get('sf_media_upload_dir'),
'mime_types' => 'web_images'
)
);

2011-09-23

Jimish Gamit answers:

$this->validatorSchema['image'] = new sfValidatorFile(array(
'path' => sfConfig::get('sf_web_dir') .'/images/XXX/',
'required' => false,
));

$this->validatorSchema['image_delete'] = new sfValidatorPass();


$this->widgetSchema['image'] = new sfWidgetFormInputFileEditable(array(
'file_src' => '/images/XXX/'.$this->getObject()->getImage(),
'is_image' => true,
'edit_mode' => !$this->isNew(),
'template' => '<div>%file%<br />%input%<br />%delete% %delete_label%</div>',
))