Symfony add Avatar field to sfGuardUser model

806 Views Asked by At

I have a project in symfony that I would like to let my users upload an image for their "avatar" field. I have found many posts about how to "extend" the table which I have with the schema below:

Member:
  inheritance:
    type:             column_aggregation
    extends:          sfGuardUser  
columns:
    idmember:    { type: integer }
    birthday:    { type: date }
    avatar:      { type: string(255) }
    bio:         { type: string(255) }

The columns get added to the table just fine, but when I go to change the widget to a sfWidgetFormInputFileEditable it breaks. Here is the Form.class file:

  $file_src = $this->getObject()->getAvatar();
  if ($file_src == '')
  {
    $file_src = 'default_image.png';
  }

  $this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array(
    'label'     => ' ',
    'file_src'  => '/uploads/avatar/'.$file_src,
    'is_image'  => true,
    'edit_mode' => true,
    'template'  => '<div>%file%<br />%input%</div>',
  ));

and "save" function of the form:

if($this->isModified())
    {
      $uploadDir = sfConfig::get('sf_upload_dir');
      $thumbnail = new sfThumbnail(150, 150);
      $thumbnail2 = new sfThumbnail(800, 800);
      if($this->getAvatar())
      {
        $thumbnail->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
        $thumbnail->save($uploadDir.'/avatar/thumbnail/'. $this->getAvatar());
        $thumbnail2->loadFile($uploadDir.'/avatar/'.$this->getAvatar());
        $thumbnail2->save($uploadDir.'/avatar/big/'. $this->getAvatar());
      }
   }  

When I submit the form, I get this error message:

This form is multipart, which means you need to supply a files array as the bind() method second argument.

1

There are 1 best solutions below

0
Michal Trojanowski On

In the action where you bind the form you should use something like this:

$form->bind($request->getParamater($form->getName()), $request->getFiles($form->getName()));

So you need to pass the uploaded files as the second parameter to the bind method.