2012-07-11 3 views
3

사용자가 이미지를 업로드 할 수 있도록 sfWidgetFormInputFileEditable 위젯을 사용하고 있습니다.Symfony 1.4 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>', 
)); 

답변

3

두 가지 옵션이 있습니다 (두 번째가 더 쉽다).

첫 번째 옵션 : 고객님의 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>', 
)); 
+0

감사합니다. j0k !! 너는 생명의 은인이야. 두 번째 옵션은 내가하고자하는 것 이상이지만 form.class 파일에 "if"문을 작성할 수 있다는 것을 인식하지 못했습니다. 다시 한 번 감사드립니다! – djcloud23

관련 문제