validation - Symfony2: how to validate UploadedFile without form? -
i need upload download file url server , persist uploadable (doctrineextensions). works fine, approach is:
- download file
curl
temp folder on server - create
uploadedfile
method , fill property values - insert uploadable entity
media
- do validation
- persist , flush
the simplified code:
// ... download file curl // create uploadedfile object $fileinfo = new file($tpath); $file = new uploadedfile($tpath, basename($url), $fileinfo->getmimetype(), $fileinfo->getsize(), null); // insert file media entity $media = new media(); $media = $media->setfile($file); $uploadablemanager->markentitytoupload($media, $file); // validate file (by annotations in entity) $errors = $validator->validate($media); // if no errors, persist , flush if(empty($errors)) { $em->persist($this->parententity); $em->flush(); }
if skip validation, ok. file succesfully moved right path (configured uploadable extension in config.yml) , persisted database. validation manualy created uploadedfile
return error:
the file not uploaded.
i can disable validator upload url , validate file custom method, doing symfony validator object seem cleaner solution me.
is there way make work?
in symfony validation component, constraint uploadedfile internally uses functin http://php.net/manual/es/function.is-uploaded-file.php
you can see here https://github.com/symfony/httpfoundation/blob/master/file/uploadedfile.php#l213
/** * returns whether file uploaded successfully. * * @return bool true if file has been uploaded http , no error occurred. * * @api */ public function isvalid() { $isok = $this->error === upload_err_ok; return $this->test ? $isok : $isok && is_uploaded_file($this->getpathname()); }
you have create own downloadvalidator (yes, downloading file curl)
Comments
Post a Comment