【问题标题】:Symfony 1.4 sfWidgetFormInputFileEditable customizationSymfony 1.4 sfWidgetFormInputFileEditable 自定义
【发布时间】:2012-07-10 22:53:30
【问题描述】:

我正在使用 sfWidgetFormInputFileEditable 小部件让我的用户上传图片。

我想看看是否有办法改变它的默认工作方式。当用户添加“新”对象时,我希望它显示通用图片,当它是“编辑”时,它可以显示现有图片。我尝试编写一个 PHP 条件语句,但这对我不起作用,因为当它是一个“新”项目时,我无法提取参数“getPicture1”,因为它不存在。

我当前的小部件:

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

【问题讨论】:

    标签: image file generics symfony1 upload


    【解决方案1】:

    你有两个选择(第二个更简单)。

    第一个选项:创建您自己的sfWidgetFormInputFileEditable 并扩展原始版本。

    在文件lib/widget/myWidgetFormInputFileEditable.class.php

    class myWidgetFormInputFileEditable extends sfWidgetFormInputFileEditable
    {
      protected function getFileAsTag($attributes)
      {
        if ($this->getOption('is_image'))
        {
          if (false !== $src = $this->getOption('file_src'))
          {
            // check if the given src is empty of image (like check if it has a .jpg at the end)
            if ('/uploads/car/' === $src)
            {
              $src = '/uploads/car/default_image.jpg';
            }
            $this->renderTag('img', array_merge(array('src' => $src), $attributes))
          }
        }
        else
        {
          return $this->getOption('file_src');
        }
      }
    }
    

    那么你需要调用它:

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

    第二个选项:检查对象是否是新的,然后使用默认图像。

    $file_src = $this->getObject()->getPicture1();
    if ($this->getObject()->isNew())
    {
      $file_src = 'default_image.jpg';
    }
    
    $this->widgetSchema['picture1'] = new sfWidgetFormInputFileEditable(array(
      'label'     => ' ',
      'file_src'  => '/uploads/car/'.$file_src,
      'is_image'  => true,
      'edit_mode' => true,
      'template'  => '<div>%file%<br />%input%</div>',
    ));
    

    【讨论】:

    • 谢谢j0k!!你是一个救生员。第二个选项更符合我的要求,但我没有意识到您可以在 form.class 文件中编写“if”语句。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多