Yii Framework Forum: CFileValidator validate issue - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CFileValidator validate issue Rate Topic: -----

#1 User is offline   Mez 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 06-September 14

Posted 16 November 2014 - 10:13 PM

Is it possible to validate an uploaded file using the CFileValidator without using a model? I have been trying to use this validator for dynamic upload file type purposes but i keep getting erros such as "Property "CFileValidator.0" is not defined." or some attribute is not defined in the CFileValidator Class model.

I can use vanilla php to validate the file types on upload, but i would prefer using a Yii defined method which satisfies the consumption of the framework.

Sample Code
//Using a  Model Sends the CFileValidator.0" is not defined. Error
$params = array('image', 'file', 'types'=>'jpg,png,jpeg,gif,pjpeg','allowEmpty'=>true,'maxSize'=>10240,'wrongType'=>'File Type Not Accepted','tooLarge'=>'File too large! 10MB is the limit');
            $validator= CValidator::createValidator('file', $personForm, 'image', $params);
$personForm->image = $_FILES['image'];
$file = CUploadedFile::getInstance($personForm,'image');


I made a complete change to the approach for executing the validation but for some strange reason The validator will not go into the validated true block.

$model = new Person();
$validator = CValidator::createValidator('file', $model, 'image',array('safe'=>true, 'types'=>array('jpg','jpeg'),'maxSize'=>10240)); 
$model->image = CUploadedFile::getInstanceByName('image');
if($validator->validate($model,'image')){
    print_r(123);       
}
else{
     print_r($model->image);    
}

This post has been edited by Mez: 17 November 2014 - 01:37 AM

0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,102
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 17 November 2014 - 01:39 AM

take out the attribute name and validator from params like so


array('types'=>'jpg,png,jpeg,gif,pjpeg','allowEmpty'=>true,'maxSize'=>10240,'wrongType'=>'File Type Not Accepted','tooLarge'=>'File too large! 10MB is the limit');

0

#3 User is offline   Mez 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 06-September 14

Posted 17 November 2014 - 07:17 AM

View Postalirz23, on 17 November 2014 - 01:39 AM, said:

take out the attribute name and validator from params like so


array('types'=>'jpg,png,jpeg,gif,pjpeg','allowEmpty'=>true,'maxSize'=>10240,'wrongType'=>'File Type Not Accepted','tooLarge'=>'File too large! 10MB is the limit');



I removed the attribute and sent the params just like you indicated and it still will not execute the validated block.

Not sure what else can be done.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users