0

很抱歉有很多问题,但我是 Symfony 的新手。我做了一个表格,一切都很好。我有几个图片字段(图片1、图片2、图片3等)。我使用了 te sfWidgetFormInputFile 并且一切都很好。它将文件上传到需要的位置,并给它一个唯一的名称。

我遇到的问题是这些字段在 mySQL 中设置为具有默认值“no-pic.png”,因此如果用户不上传图片,它仍然会在帖子中显示一些内容。但是每次有人上传图片时,它都会删除我的 no-pic.png 文件,并且没有图片的帖子会显示为空白方块。我在想这是因为它在上传新图片之前设置为“删除以前的图片”,以减少服务器上多余的未使用图片。

我想知道是否有办法让它在从文件夹中删除它之前检查该值是否为“no-pic.png”。如果有帮助,这是我在 form.class 文件中使用的代码。

      $this->widgetSchema['picture1'] = new sfWidgetFormInputFile(array('label' => 'Pictures', ));
  $this->validatorSchema['picture1'] = new sfValidatorFile(array('required' => false, 'path' => sfConfig::get('sf_upload_dir').'/car', 'mime_types' => 'web_images', ));
  $this->widgetSchema['picture1'] = new sfWidgetFormInputFileEditable(array(
    'label' => 'Pictures',
    'file_src' => '/uploads/car/'.$this->getObject()->getPicture1(),
    'is_image' => true,
    'edit_mode' => !$this->isNew(),
    'template' => '<div>%file%<br />%input%<br />%delete%%delete_label%</div>',
    ));
  $this->validatorSchema['picture_delete'] = new sfValidatorPass();
4

2 回答 2

1

仅供参考这里是我使用的代码。

$this->setWidget('logo_image', new sfWidgetFormInputFileEditable(array(
  'file_src'  => 'uploads/logos/'.$this->getObject()->getFilename(),
  'edit_mode' => !$this->isNew()
)));

$validatorOptions = array(
  'max_size'           => 1024*1024*10,
  'path'               => sfConfig::get('sf_upload_dir').'/logos',
  'mime_type_guessers' => array(),
  'required'           => false
);

if ($this->isNew())
{
  $validatorOptions['required'] = true;
}

$this->setValidator('logo_image', new sfValidatorFile($validatorOptions));
$this->setValidator('logo_image_delete', new sfValidatorString(array('required' => false)));
于 2012-07-09T18:44:39.403 回答
0

我找到了解决方法。我检查了页面是否设置为 no-pic.png,然后它在另一个位置显示 no-pic.png。这样当它上传图片时,它不会删除它。:) 感谢您的帮助。

于 2012-07-09T01:59:41.800 回答