English 中文(简体)
symfony setPostValidator with sfValidatorFile
原标题:

I am going through an issue to setup a file upload validator on callback.

I want to achieve this:

I have a form, where user choose the type of the file they are uploading and upload the file. So I want to set the validator to handle images in case they chose "img" as type, and pdf if they chose "pdf".

Moreover I want to specify the mime type and path and validatedFileClass according to the type.

I tried this.. but i can t get it to work

$this->validatorSchema->setPostValidator( new sfValidatorCallback(array( callback => array($this, validateUploadedFiles ))) );

the function:

      public function validateUploadedFiles($form_validator, $values){

    $this_year = date("Y");
    $this_month = date("m");

    $basic_validator = array(
     required  => true,
     path  => sfConfig::get( sf_upload_dir ). restaurant/media/ .$this_year. / .$this_month. / 
    );

    $doc_validator = $video_validator = $img_validator = $pdf_validator = $basic_validator;
    $pdf_validator[ mime_types ] = array ( application/pdf );
    $doc_validator[ mime_types ] = array ( application/msword ,  application/vnd.openxmlformats );
    $img_validator[ mime_types ] =  web_images ;
    //$img_validator[ validated_file_class ] =  imgHandling ;
    $video_validator[ mime_types ] = array( video/mpeg ,  video/3gpp ,  video/mp4 ,  video/quicktime );

    switch( $values[ type ] ):
       case  pdf  : $validator = $pdf_validator; break;
       case  img  : $validator = $img_validator; break;
       case  word  : $validator = $doc_validator; break;
       case  video  : $validator = $video_validator; break;
    endswitch; 


    //$form->getValidatorSchema()->offsetUnset( url ) ;
    //print_r($validator_fields);
    $validator = new sfValidatorFile( $validator );
    $validator_schema = new sfValidatorSchema();
    $validator_schema->offsetSet( url , $validator);
   //$validator_fields = $form->getValidatorSchema()->getFields();    


    $schema = parent::getValidatorSchema();
    $schema->offsetUnset( url ) ;
    $schema->offsetSet( url , $validator);


   // $path = sfConfig::get( sf_upload_dir ). restaurant/media/ .$this_year. / .$this_month. / ;
   // $values[ url ] = new sfValidatedFile( $values[ url ][ name ], $values[ url ][ type ], $values[ url ][ tmp_name ], $values[ url ][ size ] , $path);

     //TODO get this url value run through the new added validator
    //$values[ url ] = $validator_schema->clean(array(  url  => $values[ url ] ));

    return $values;

  }

the problem i am facing is that , this function receives the url value as array, and even if I update the validators schema, it s not validating the url and keeps on sending it as array to the object saving method. So how to make something like

url.validateFile() from inside this function

问题回答

Not sure about the best solution, but I d prefer to split validation process into 2 parts:

  1. Validate mime type
  2. If mime is ok, then validate URL

Or vise versa, your choice.

The glue would be sfValidatorAnd.

Did I understand you right?





相关问题
Using a file form field with a Java servlet

I am tring to retrieve a filename or the file itself for use in a java servlet (from a web form). I have a file form field: <form enctype="multipart/form-data" method="post" action="...

Streaming uploaded files directly into a database table

I have several applications that allow users to upload attachments that are stored inside a database table. This has worked fine for several years because it was initially intended for smallish image ...

Asynchronus File Upload with StateServer Mode

I want to implement Asynchronus File Upload. I ve tried ajax:AsyncFileUpload control. It s working fine only with InProc Mode. But I m using StateServer Mode. Any help from anybody? Thanks in ...

How do CMS upload images?

I am just playing around and trying to make a very simple CMS. Right now I what I do right now is I use "FtpWebRequest" to get the file that they want to change around and stick it into a jquery ...

File does not upload from web form in Django

Howdy - I ve written a very simple app to accept job applications including a resume upload. Running the bundled server for development locally, I can successfully upload files via the web form on ...

热门标签