2009年7月21日 星期二

Backend Admin FileUpload for symfony 1.2

This approach is to provide a path option to sfValidatorFile...
--------------------------------------------------------------
// lib/form/StudentForm.class.php
class StudentForm extends BaseStudentForm
{
    public function configure()
    {
        $this->widgetSchema['photo'] = new sfWidgetFormInputFile();
        $this->validatorSchema['photo'] = new sfValidatorFile(array(
        'path' => sfConfig::get('sf_web_dir').'/uploads/students',
        ));
    }
}
--------------------------------------------------------------
And write a generatePhotoFilename() method on Student (assuming "Photo" is the phpName for the field name "photo")
--------------------------------------------------------------

// lib/model/Student.php
class Student extends BaseStudent
{
    public function generatePhotoFilename(sfValidatedFile $file)
    {
        return $file->getOriginalName();
    }
}

--------------------------------------------------------------

This way is probably the preferred approach as it makes for a thinner controller.

0 意見: