[EXTENSION] EPhoto Validator

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 :)

I think I may need/use this! thanks!

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'); ?>

Yeah there was a bug,

please download correct version

http://www.yiiframew…toValidator.zip

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.

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?

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).

Hi All,

Here is my revision of this extension.

Version 0.21alpha_vladm [color="#FF0000"]NEW[/color]

561

EPhotoValidator.0.21alpha_vladm.zip

Version 0.2alpha_vladm

532

EPhotoValidator.0.2alpha_vladm.zip

Old version 0.1alpha_vladm:

444

EPhotoValidator.vladm.zip

Release notes:

Small documentation:

Instalation notes:

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

Have a nice validations! ;)

Good work vladm,

Yii ppl, would you please upgrade http://www.yiiframework.com/extension/ephotovalidator/ with the vladm version?

Thanks, please see updates in my previous post.

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:

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

great work and great forum!

i was about to create one my self and… fantastic guys! thank you very much

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

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!!!

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.

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?

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