Yii Framework Forum: How to validate file upload field - Yii Framework Forum

Jump to content

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

How to validate file upload field Validate File Upload field

#1 User is offline   dharmalingam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-August 11

Posted 16 August 2011 - 07:09 AM

Hello,

How to validate the file upload field in yii...

The uploaded image is save id in folder not in db.
0

#2 User is offline   rei 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 190
  • Joined: 10-November 10
  • Location:Cimahi

Posted 20 August 2011 - 08:23 AM

Did you check CFileValidator? You can add validation to your model like this:

array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg,jpeg,gif,png')

0

#3 User is offline   shailesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-May 11
  • Location:Ahmedabad(India)

Posted 02 November 2011 - 01:18 AM

For validation File

Just Add

public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
For validation


0

#4 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 12-January 13
  • Location:Mohali

Posted 14 February 2013 - 05:29 AM

hi friend you can also validate your upload image by creating a function in controller
like
public function imageValidate($img, $maxwidth = 1024, $maxheight = 1024)
{

$imagehw = getimagesize($img);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
$imagetype = $imagehw[2];
$valid_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP);
if (in_array($imagetype, $valid_types))
{
if($imagewidth < $maxwidth || $imageheight < $maxheight )
return 1;
}

return 0;
}
and checked it in your model by passing the value.
0

#5 User is offline   Blizz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 14-March 10

Posted 09 April 2013 - 05:18 AM

Rajesh: Don't do stuff like that in the controller, it doesn't belong there as it should know as little as possible of what your model/form needs to work.

You can easily call a custom validation function by using a validation rule like
array('attribute', 'myFunctionName');


It will call the function in the model during validation. You can do that verification either there, or create some sort of file-helper component and call that.
It's a lot cleaner.
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