Hello,
How to validate the file upload field in yii...
The uploaded image is save id in folder not in db.
Page 1 of 1
How to validate file upload field Validate File Upload field
#2
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')
#3
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
Just Add
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
For validation
#4
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.
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.
#5
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
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.
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.
Share this topic:
Page 1 of 1

Help












