我有一個表單用於在我的博客引擎中上傳圖片。這些文件上傳到網頁/上傳,但我想添加一個「選擇」小工具,讓用戶從文件夾列表中選擇,例如'照片','剪貼畫','標誌'。如何讓用戶選擇上傳目錄?
這裏是我的形式
class ImageForm extends BaseForm
{
public function configure()
{
$this->widgetSchema->setNameFormat('image[%s]');
$this->setWidget('file', new sfWidgetFormInputFileEditable(
array(
'edit_mode'=>false,
'with_delete' => false,
'file_src' => '',
)
));
$this->setValidator('file', new mysfValidatorFile(
array(
'max_size' => 500000,
'mime_types' => 'web_images',
'path' => 'uploads',
'required' => true
)
));
$this->setWidget('folder', new sfWidgetFormChoice(array(
'expanded' => false,
'multiple' => false,
'choices' => array('photos', 'cliparts', 'logos')
)
));
$this->setValidator('folder', new sfValidatorChoice(array(
'choices' => array(0,1,2)
)));
}
}
,這裏是我的行動:
public function executeAjout(sfWebRequest $request)
{
$this->form = new ImageForm();
if ($request->isMethod('post'))
{
$this->form->bind(
$request->getParameter($this->form->getName()),
$request->getFiles($this->form->getName())
);
if ($this->form->isValid())
{
$this->form->getValue('file')->save();
$this->image = $this->form->getValue('file');
}
}
我使用的是自定義文件驗證:
class mySfValidatorFile extends sfValidatorFile
{
protected function configure($options = array(), $messages =
array())
{
parent::configure();
$this->addOption('validated_file_class',
'sfValidatedFileFab');
}
}
class sfValidatedFileFab extends sfValidatedFile
{
public function generateFilename()
{
return $this->getOriginalName();
}
}
那麼如何告訴文件上傳小部件將圖像保存在不同的文件夾中?
在ImageForm?但是在創建表單時我還沒有$ path ... – Manu 2011-02-09 15:21:09