Yii Framework Forum: [EXTENSION] EPhoto Validator - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

[EXTENSION] EPhoto Validator Rate Topic: -----

#1 User is offline   emix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 02-March 09
  • Location:Poland

Posted 03 May 2009 - 04:53 PM

This class extends CFileValidator to validate uploaded images so it has its basic functionality plus:
we can validate its mime type, min height, max height, min width and max width.

Version 1.0 alpha, but works :)

Attached File(s)


0

#2 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 03 May 2009 - 05:31 PM

I think I may need/use this! thanks!
0

#3 User is offline   ouhman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 118
  • Joined: 26-December 08

Posted 10 May 2009 - 03:47 PM

Hi thanks for your extension.

I have a try, seems to doesn't work:

I tried :

	
<?php public function rules()
{
return array(
array('image', 'EPhotoValidator', 'maxWidth'=>800, 'maxHeight'=>600,'mimeType'=>array('image/jpeg')),
);
}
?>

And I always have the following error message:
    * This mime type of the photo is not allowed
Any ideas?

And in the form :
<?php echo CHtml::activeFileField($couleur,'image'); ?>

0

#4 User is offline   emix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 02-March 09
  • Location:Poland

Posted 10 May 2009 - 05:19 PM

Yeah there was a bug,

please download correct version
http://www.yiiframew...toValidator.zip
0

#5 User is offline   juban_ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 09-February 09

Posted 22 May 2009 - 05:05 AM

This is a nice extension idea. However, I found many issues using it.
Here is a corrected version of the validator:

/**
* EPhotoValidator class file.
*
* @author emix
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2009 emix
* @license
*
* Copyright © 2008 by emix
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of emix nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

class EPhotoValidator extends CFileValidator {

/**
* @var string Allowed mime type of the image, ie: image/jpeg
*/
public $mimeType;

/**
* @var string Mime type error message
*/
public $mimeTypeError;

/**
* @var int Minimum width of the image required.
*/
public $minWidth;
/**
* @var string Mime type error message
*/
public $minWidthError;

/**
* @var int Maximum width of the image allowed
*/
public $maxWidth;
/**
* @var string Mime type error message
*/
public $maxWidthError;

/**
* @var int Minimum height of the image required.
*/
public $minHeight;
/**
* @var string Mime type error message
*/
public $minHeightError;

/**
* @var int Maximum height of the image allowed.
*/
public $maxHeight;
/**
* @var string Mime type error message
*/
public $maxHeightError;

/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel the object being validated
* @param string the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{
parent::validateAttribute($object, $attribute);
$file=$object->$attribute;
if(!($file instanceof CUploadedFile))
$file=CUploadedFile::getInstance($object,$attribute);

if($this->allowEmpty && ($file===null || $file->getError()==UPLOAD_ERR_NO_FILE))
return;

$info=getimagesize($file->getTempName());

if (isset($this->minWidth)) {
if ($info[0] < $this->minWidth)
{
$message=$this->minWidthError ? $this->minWidthError : Yii::t('yii','Photo should be at least {width}px in width');
$this->addError($object,$attribute,$message,array('{width}'=>$this->minWidth));
}
}
if (isset($this->maxWidth)) {
if ($info[0] > $this->maxWidth)
{
$message=$this->maxWidthError ? $this->maxWidthError : Yii::t('yii','Photo should be at max {width}px in width');
$this->addError($object,$attribute,$message,array('{width}'=>$this->maxWidth));
}
}
if (isset($this->minHeight)) {
if ($info[1] < $this->minHeight)
{
$message=$this->minHeightError ? $this->minHeightError : Yii::t('yii','Photo should be at least {height}px in height');
$this->addError($object,$attribute,$message,array('{height}'=>$this->minHeight));
}
}
if (isset($this->maxHeight)) {
if ($info[1] > $this->maxHeight)
{
$message=$this->maxHeightError ? $this->maxHeightError : Yii::t('yii','Photo should be at max {height}px in height');
$this->addError($object,$attribute,$message,array('{height}'=>$this->maxHeight));
}
}
if (isset($this->mimeType))
{
$mime=is_scalar($this->mimeType) ? array($this->mimeType) : $this->mimeType;
if (!in_array($file->getType(), $mime))
{
$message=$this->mimeTypeError ? $this->mimeTypeError : Yii::t('yii','This mime type of the photo is not allowed');
$this->addError($object,$attribute,$message);
}
}
}
}


Regards.
1

#6 User is offline   thyseus 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 300
  • Joined: 18-April 09
  • Location:Leipzig, Germany

Posted 01 July 2009 - 07:27 PM

There seems to be one Problem left:

My Models uses the EPhoto Validator. It works without problems; but when i want to _update_ a model, it always fails, because the Validation fails. Does anyone have a solution for this?
0

#7 User is offline   juban_ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 09-February 09

Posted 08 July 2009 - 03:50 AM

Quote

There seems to be one Problem left:



My Models uses the EPhoto Validator. It works without problems; but when i want to _update_ a model, it always fails, because the Validation fails. Does anyone have a solution for this?


You should only affect your model attribute if you detect that a file was uploaded and if not, the attribute would be left unchanged (EPhotoValidator will be ignored because it is a string value in this case).


0

#8 User is offline   vladm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-December 09

Posted 14 January 2010 - 11:27 PM

Hi All,

Here is my revision of this extension.

Version 0.21alpha_vladm *NEW*
Attached File  EPhotoValidator.0.21alpha_vladm.zip (4.45K)
Number of downloads: 216

Version 0.2alpha_vladm
Attached File  EPhotoValidator.0.2alpha_vladm.zip (4.46K)
Number of downloads: 7

Old version 0.1alpha_vladm:
Attached File  EPhotoValidator.vladm.zip (3.91K)
Number of downloads: 11

Release notes:

Quote

Version 0.21alpha_vladm:

What were changed:
  • Typo bug was fixed (tanks to TiBe)

Version 0.2alpha_vladm:

What were changed:
  • Some copy/paste bugs were fixed
  • Some new validation rules were added (see documentation)

Version 0.1alpha_vladm:

What were changed:
  • Was used the "phiras" version as a base, because it's more stable and has more validation options.
  • Image validation code was moved from validateAttribute to validateFile method, thus at first the file will validated by standard CFileValidator validators and after by additional validators from extension.
  • Unnecessary method getFile was removed. The uploaded file instance (as well as multiple instances) arrives by standard CFileValidator way.
  • Default error messages was rewrited and a new "core Yii framework like" translation mechanism was implemented, thus the extension uses coreMessages application component with 'en_us' language as source (does not depend on application source language), but all translations should be placed in 'messages' folder near extension class file (see this link for more details). Custom error messages still available.
  • All default error messages was translated to Ukrainian and Russian languages (see in package).


Small documentation:

Quote

  • You can use the following options to validate image file:
    • all CFileValidator options (see this link for details)
    • width integer the image width should be equal to this value
    • minWidth integer the image width should be at least this value
    • maxWidth integer the image width should be at most his value
    • height integer the image height should be equal to this value
    • minHeight integer the image height should be at least this value
    • maxHeight integer the image height should be at most this value
    • mimeType string the image mime type should corespond to this value
    • smallSideSize integer the image small side (height or width) should be equal to this value (0.2alpha)*
    • smallSideMinSize integer the image small side (height or width) should be at least this value (0.2alpha)*
    • smallSideMaxSize integer the image small side (height or width) should be at most this value (0.2alpha)*
    • bigSideSize integer the image big side (height or width) should be equal to this value (0.2alpha)**
    • bigSideMinSize integer the image big side (height or width) should be at least this value (0.2alpha)**
    • bigSideMaxSize integer the image big side (height or width) should be at most this value (0.2alpha)**
    * it can be useful if you don't want to allow an upload of images with too small dimensions
    ** it can be useful if you don't want to allow an upload of images with too big dimensions

  • You can use the following options to override default error messages:
    • widthError - dynamic values: {file} and {width}
    • minWidthError - dynamic values: {file} and {width}
    • maxWidthError - dynamic values: {file} and {width}
    • heightError - dynamic values: {file} and {height}
    • minHeightError - dynamic values: {file} and {height}
    • maxHeightError - dynamic values: {file} and {height}
    • mimeTypeError - dynamic values: {file} and {mime}
    • smallSideSizeError - dynamic values: {file} and {side} (0.2alpha)
    • smallSideMinSizeError - dynamic values: {file} and {side} (0.2alpha)
    • smallSideMaxSizeError - dynamic values: {file} and {side} (0.2alpha)
    • bigSideSizeError - dynamic values: {file} and {side} (0.2alpha)
    • bigSideMinSizeError - dynamic values: {file} and {side} (0.2alpha)
    • bigSideMaxSizeError - dynamic values: {file} and {side} (0.2alpha)


Instalation notes:

Quote

  • Extract package contents to 'components' folder of your Yii application
  • Enjoy! 8)



All previous copyrights and licenses were reserved. You are free for use and change it.

Have a nice validations! ;)
2

#9 User is offline   phiras 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 10-September 09
  • Location:Amman

Posted 14 February 2010 - 06:26 AM

Good work vladm,

Yii ppl, would you please upgrade http://www.yiiframew...photovalidator/ with the vladm version?
0

#10 User is offline   vladm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-December 09

Posted 14 February 2010 - 08:43 AM

View Postphiras, on 14 February 2010 - 06:26 AM, said:

Good work vladm,

Yii ppl, would you please upgrade http://www.yiiframew...photovalidator/ with the vladm version?

Thanks, please see updates in my previous post.
0

#11 User is offline   TiBe 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-January 10

Posted 27 February 2010 - 03:03 PM

Hi, great extension I'm testing it and it seems wonder, thanks vladm, phiras and emix :)
However, there's a typo error on the 0.2alpha_vladm version, it's on line: 222, it says:

$message = $this->heighError ? $this->heightError : 


And it should be:

$message = $this->heightError ? $this->heightError : 


(The 't' is missing ;) )

Keep doing a great job :lol:
0

#12 User is offline   vladm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-December 09

Posted 28 February 2010 - 05:49 AM

View PostTiBe, on 27 February 2010 - 03:03 PM, said:

However, there's a typo error on the 0.2alpha_vladm version, it's on line: 222

Thanks for report, please see updates in my previous post.
0

#13 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 15 October 2010 - 07:36 AM

great work and great forum!

i was about to create one my self and... fantastic guys! thank you very much
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#14 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 15 October 2010 - 10:03 AM

View Postjuban_, on 08 July 2009 - 03:50 AM, said:

<p class='citation'>Quote</p><div class="blockquote"><div class='quote'>There seems to be one Problem left:



My Models uses the EPhoto Validator. It works without problems; but when i want to _update_ a model, it always fails, because the Validation fails. Does anyone have a solution for this?</div></div>


I have the same problem and it is very weird. When I create the model -no problem, when I update it fails to update to the DB.

I have seen the other reply and I have, of course, double checked if a file was uploaded or not! I spent quite a lot of time searching on the net for a solution but... at the end I see that I will have to create my own validation...

My last comment was too quick...

If anybody knows the solution please help me; my rules are ok, i double check whether there is a file submission or not (validators do that too!)... Why I cannot update the object to the DDBB? Any good example of implementing this validation object?

Thanks
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#15 User is offline   vladm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-December 09

Posted 16 October 2010 - 04:11 AM

View PostAntonio Ramirez, on 15 October 2010 - 10:03 AM, said:

I have the same problem and it is very weird. When I create the model -no problem, when I update it fails to update to the DB.

I have seen the other reply and I have, of course, double checked if a file was uploaded or not! I spent quite a lot of time searching on the net for a solution but... at the end I see that I will have to create my own validation...

My last comment was too quick...

If anybody knows the solution please help me; my rules are ok, i double check whether there is a file submission or not (validators do that too!)... Why I cannot update the object to the DDBB? Any good example of implementing this validation object?

Thanks

Hey Antonio,
You should use scenarios and manipulate allowEmpty property for successfull updating without file uploading. See a rules' example below. I have used 2 rules with "allowEmpty = false" for inser scenario and "allowEmpty = true" otherwise. So in 'create' action I just use "$model->setScenario('insert');" (I know that the model can do this automatically, just set it manually for a guaranted switching) and in this case my image uploading is required, but for any other cases, e.g. 'upload' action, the second rule will used and model will updated without any validation errors concerning EPhotoValidator.

array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => false, 'minWidth' => 120, 'minHeight' => 120, 'on' => 'insert'),
array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => true, 'minWidth' => 120, 'minHeight' => 120), 

Hope that helps.
Vlad
0

#16 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 16 October 2010 - 11:56 AM

View Postvladm, on 16 October 2010 - 04:11 AM, said:

Hey Antonio,
You should use scenarios and manipulate allowEmpty property for successfull updating without file uploading. See a rules' example below. I have used 2 rules with "allowEmpty = false" for inser scenario and "allowEmpty = true" otherwise. So in 'create' action I just use "$model->setScenario('insert');" (I know that the model can do this automatically, just set it manually for a guaranted switching) and in this case my image uploading is required, but for any other cases, e.g. 'upload' action, the second rule will used and model will updated without any validation errors concerning EPhotoValidator.

array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => false, 'minWidth' => 120, 'minHeight' => 120, 'on' => 'insert'),
array('image', 'EPhotoValidator', 'types'=>'jpg, gif, png', 'allowEmpty' => true, 'minWidth' => 120, 'minHeight' => 120), 

Hope that helps.
Vlad


Vlad!

Thanks for your quick reply, the problem was not the allowEmpty as you proposed above. The problem is that, even uploading the picture, my object wasn't updating accordingly at all. Maybe I am wrong, this is the scenario I was using the validator for:

a) i have a DB property object that has a 'picture' field (building a real estate agency at the moment) and the CMS allows the uploading of one picture that will represent the property along the site (the main picture, after they will upload as many as they want).

B) this field only has the file name of the picture not the picture itself.

c) I upload the picture (I double check it in client and server for creation, and allow update to be empty)

d) Once uploaded I get an instance of the file and save it to a temporary folder, then with PHPThumb resize the picture accordingly to my application needs; if all is correct then i set the object attributes' picture to the name of the file save the object (and commit a transaction -All is within a transaction, I do a bunch of stuff before and after).

e) Everything is fine but: it does not validates anything, and I cannot update the property object. It doesn't fail; it just does nothing at all...

Nevertheless, I will try your examples again as I see that the mime types are different. I used 'image/jpg, image/gif, image/png' (not just the extensions)

My application is working through a custom function but I give it a try and let you know!

Thanks!!!
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#17 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 16 October 2010 - 12:22 PM

Just tried Vlad,

And nothing; I am back with my own solution... Maybe I am wrong using the validator in this scenario.

Thanks anyway
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#18 User is offline   vladm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 26-December 09

Posted 16 October 2010 - 03:55 PM

View PostAntonio Ramirez, on 16 October 2010 - 12:22 PM, said:

Just tried Vlad,

And nothing; I am back with my own solution... Maybe I am wrong using the validator in this scenario.

Thanks anyway

Antonio,
Thanks for your feedback. Could you do something for me? The EPhotoValidator extends CFileValidator class from framework. Could you try to use CFileValidator instead of EPhotoValidator and repeat your error again. And please write here your result.
Thanks.
0

#19 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,447
  • Joined: 04-October 10

Posted 16 October 2010 - 07:21 PM

View Postvladm, on 16 October 2010 - 03:55 PM, said:

Antonio,
Thanks for your feedback. Could you do something for me? The EPhotoValidator extends CFileValidator class from framework. Could you try to use CFileValidator instead of EPhotoValidator and repeat your error again. And please write here your result.
Thanks.


I will my friend but now is 3 AM and I think is quite enough for programming today... :)

First thing in the morning when I awake up will be to try it out. Again, maybe it is not the scenario to be use the validator for -should the DB field be of other type than varchar? i am not saving but the name of the file to the field at the end, but, could that be the cause?
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#20 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 10 February 2011 - 08:25 AM

Hi, it's a great extension. I'm using it on my project but I don't know how can I translate the error message.

Can anyone help me, or show me an example?

Thanks
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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