Monday, July 27, 2009

validates() not working on all functions...

I'm using a validates function in a model in order to validate image
uploads. I want to validate the filetype, and also make it required.

I know cake has built in validation for these, but I could never get
it to work. The image upload component I am using uses an array,
accessible via $this->data['Img']['pic'].

The problem I am having, is that the /add/ function works great, but
when I use the image field (Img.pic) on another function (/
edit_image/) the validation function isn't applied.

Here is the model code:

function validates()
{

$image = $this->data['Img']['pic']['name'];
// Grab the file extension:
$path = pathinfo($image);
$filetype = $path['extension'];

if(!empty($image)) {

if (($filetype != 'JPG') && ($filetype != 'jpg') && ($filetype !=
'GIF') && ($filetype != 'gif') && ($filetype != 'JPEG') && ($filetype !
= 'jpeg'))
{
$this->invalidate('Img.pic');
}
}
else {
$this->invalidate('Img.pic');
}


$errors = $this->invalidFields();
return count($errors) == 0;
}

Is there something I have to include in the controller to make the
above happen automatically?

I would appreciate any guidance on this, I can include more code if
requested.

Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: