Saturday, February 28, 2015

Re: File upload validation

You can get rid of the $unset_photo variable.... I didn't end up using it.

--Charles

On Sat, Feb 28, 2015 at 6:48 PM, Charles Beasley <carmelchas@gmail.com> wrote:
I'm sorry.. I replied quickly via cell phone.  A better response is as follows.

Try the following code:

<?php


class Outlet extends CoasterCmsAppModel

{

    public $validate = array(

        'name' => array(

            'rule' => 'notEmpty', // verplicht

            'message' => 'Nameis required.',

            'allowEmpty' => true

        ),

        'intro' => array(

            'rule' => 'notEmpty', // verplicht

            'message' => 'Intro is required.'

        ),

        'photo' => array(

            'validFileSize' => array( // zelf gekozen naam van de regel

                'rule' => array('filesize', '>', 0), // verplicht

                'on' => 'create',

                'message' => 'Photo is required.'

            )

        ),

        'photoTemp' => array(

            'validExtension' => array( // zelf gekozen naam van de regel

                'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),

                'on' => 'create',

                'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'

            ),

            'validExtension' => array( // zelf gekozen naam van de regel

                'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),

                'allowEmpty' => true,

                'on' => 'update',

                'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'

            )

        )

    );

    

    public function beforeValidate()

    {

        $unset_photo = false;

        if ( is_array( $this->data['Outlet']['photo'] ) ) {

            $filename = false;

            $tmp_filename = false;

            $unset_photo = true;

            $upload_path = WWW_ROOT .'img' . DS . 'outlets' . DS;

            if ( isset( $this->data['Outlet']['photo']['name'] ) ) {

                $filename = trim($this->data['Outlet']['photo']['name']);

            }

            if ( isset( $this->data['Outlet']['photo']['tmp_name'] ) ) {

                $tmp_filename = trim($this->data['Outlet']['photo']['tmp_name']);

            }

            if ( ! empty( $tmp_filename ) && ! empty( $filename ) ) {

                // Move the uploaded file to the new directory

                if(!move_uploaded_file($tmp_filename,  $upload_path  basename($filename))) {

                    /*

                     * Handle upload error and return FALSE; e.g.

                     *     $this->validationErrors['Outlet']['photo'] =  'Failed to upload file ' . $filename;

                     */

                } else {

                    $this->data['Outlet']['photo'] = $filename;  // SUCCESS

                }

            } else if ( ! empty( $this->data['Outlet']['photo'] ) ) ) {

                /*

                 * Handle Invalid input error and return FALSE; e.g.

                 *

                 *   $this->validationErrors['Outlet']['photo'] =  'Invalid input: photo tmp_name required';

                 *   $this->validationErrors['Outlet']['photo'] =  'Invalid input: photo name required';

                 */

            } else {

                /*

                 * Silently unset and ignore an empty array.

                 */

                unset($this->data['Outlet']['photo']);

            }

        }

    }


?>



On Sat, Feb 28, 2015 at 6:24 PM, Charles Beasley <carmelchas@gmail.com> wrote:

I think you should change your function to beforeValidate

On Feb 28, 2015 5:54 PM, "Sam Clauw" <sam.clauw@gmail.com> wrote:
Okay Charles , that makes sense so I changed my code:

<?php


class Outlet extends CoasterCmsAppModel
{
   
public $validate = array(
       
'name' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Nameis required.',
           
'allowEmpty' => true
       
),
       
'intro' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Intro is required.'
       
),
       
'photo' => array(
           
'validFileSize' => array( // zelf gekozen naam van de regel
               
'rule' => array('filesize', '>', 0), // verplicht
               
'on' => 'create',
               
'message' => 'Photo is required.'
           
)
       
),
       
'photoTemp' => array(
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'on' => 'create',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'allowEmpty' => true,
               
'on' => 'update',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
)
       
)
   
);
   
   
public function beforeValidate()
   
{
        $this
->data['Outlet']['photoTemp'] = $this->data['Outlet']['photo'];
        $this
->data['Outlet']['photo'] = $this->data['Outlet']['photoTemp']['name'];
   
}
   
   
public function afterValidate()
   
{
        $filename
= $this->data['Outlet']['photo'];
       
       
if (!empty($filename)) {
            move_uploaded_file
($this->data['Outlet']['photoTemp']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
       
} else {
            unset
($this->data['Outlet']['photo']);
       
}
       
        unset
($this->data['Outlet']['photoTemp']);
   
}
}


However, I still get the error message that I should upload a file with a correct extension. It's just like 'allowEmpty' => true" isn't working at all.
I quess in my case it's checking the value in $this->data['Outlet']['photoTemp']['type'] so what could possibly be the problem?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Re: File upload validation

I'm sorry.. I replied quickly via cell phone.  A better response is as follows.

Try the following code:

<?php


class Outlet extends CoasterCmsAppModel

{

    public $validate = array(

        'name' => array(

            'rule' => 'notEmpty', // verplicht

            'message' => 'Nameis required.',

            'allowEmpty' => true

        ),

        'intro' => array(

            'rule' => 'notEmpty', // verplicht

            'message' => 'Intro is required.'

        ),

        'photo' => array(

            'validFileSize' => array( // zelf gekozen naam van de regel

                'rule' => array('filesize', '>', 0), // verplicht

                'on' => 'create',

                'message' => 'Photo is required.'

            )

        ),

        'photoTemp' => array(

            'validExtension' => array( // zelf gekozen naam van de regel

                'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),

                'on' => 'create',

                'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'

            ),

            'validExtension' => array( // zelf gekozen naam van de regel

                'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),

                'allowEmpty' => true,

                'on' => 'update',

                'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'

            )

        )

    );

    

    public function beforeValidate()

    {

        $unset_photo = false;

        if ( is_array( $this->data['Outlet']['photo'] ) ) {

            $filename = false;

            $tmp_filename = false;

            $unset_photo = true;

            $upload_path = WWW_ROOT .'img' . DS . 'outlets' . DS;

            if ( isset( $this->data['Outlet']['photo']['name'] ) ) {

                $filename = trim($this->data['Outlet']['photo']['name']);

            }

            if ( isset( $this->data['Outlet']['photo']['tmp_name'] ) ) {

                $tmp_filename = trim($this->data['Outlet']['photo']['tmp_name']);

            }

            if ( ! empty( $tmp_filename ) && ! empty( $filename ) ) {

                // Move the uploaded file to the new directory

                if(!move_uploaded_file($tmp_filename,  $upload_path  basename($filename))) {

                    /*

                     * Handle upload error and return FALSE; e.g.

                     *     $this->validationErrors['Outlet']['photo'] =  'Failed to upload file ' . $filename;

                     */

                } else {

                    $this->data['Outlet']['photo'] = $filename;  // SUCCESS

                }

            } else if ( ! empty( $this->data['Outlet']['photo'] ) ) ) {

                /*

                 * Handle Invalid input error and return FALSE; e.g.

                 *

                 *   $this->validationErrors['Outlet']['photo'] =  'Invalid input: photo tmp_name required';

                 *   $this->validationErrors['Outlet']['photo'] =  'Invalid input: photo name required';

                 */

            } else {

                /*

                 * Silently unset and ignore an empty array.

                 */

                unset($this->data['Outlet']['photo']);

            }

        }

    }


?>



On Sat, Feb 28, 2015 at 6:24 PM, Charles Beasley <carmelchas@gmail.com> wrote:

I think you should change your function to beforeValidate

On Feb 28, 2015 5:54 PM, "Sam Clauw" <sam.clauw@gmail.com> wrote:
Okay Charles , that makes sense so I changed my code:

<?php


class Outlet extends CoasterCmsAppModel
{
   
public $validate = array(
       
'name' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Nameis required.',
           
'allowEmpty' => true
       
),
       
'intro' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Intro is required.'
       
),
       
'photo' => array(
           
'validFileSize' => array( // zelf gekozen naam van de regel
               
'rule' => array('filesize', '>', 0), // verplicht
               
'on' => 'create',
               
'message' => 'Photo is required.'
           
)
       
),
       
'photoTemp' => array(
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'on' => 'create',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'allowEmpty' => true,
               
'on' => 'update',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
)
       
)
   
);
   
   
public function beforeValidate()
   
{
        $this
->data['Outlet']['photoTemp'] = $this->data['Outlet']['photo'];
        $this
->data['Outlet']['photo'] = $this->data['Outlet']['photoTemp']['name'];
   
}
   
   
public function afterValidate()
   
{
        $filename
= $this->data['Outlet']['photo'];
       
       
if (!empty($filename)) {
            move_uploaded_file
($this->data['Outlet']['photoTemp']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
       
} else {
            unset
($this->data['Outlet']['photo']);
       
}
       
        unset
($this->data['Outlet']['photoTemp']);
   
}
}


However, I still get the error message that I should upload a file with a correct extension. It's just like 'allowEmpty' => true" isn't working at all.
I quess in my case it's checking the value in $this->data['Outlet']['photoTemp']['type'] so what could possibly be the problem?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Re: File upload validation

I think you should change your function to beforeValidate

On Feb 28, 2015 5:54 PM, "Sam Clauw" <sam.clauw@gmail.com> wrote:
Okay Charles , that makes sense so I changed my code:

<?php


class Outlet extends CoasterCmsAppModel
{
   
public $validate = array(
       
'name' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Nameis required.',
           
'allowEmpty' => true
       
),
       
'intro' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Intro is required.'
       
),
       
'photo' => array(
           
'validFileSize' => array( // zelf gekozen naam van de regel
               
'rule' => array('filesize', '>', 0), // verplicht
               
'on' => 'create',
               
'message' => 'Photo is required.'
           
)
       
),
       
'photoTemp' => array(
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'on' => 'create',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'allowEmpty' => true,
               
'on' => 'update',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
)
       
)
   
);
   
   
public function beforeValidate()
   
{
        $this
->data['Outlet']['photoTemp'] = $this->data['Outlet']['photo'];
        $this
->data['Outlet']['photo'] = $this->data['Outlet']['photoTemp']['name'];
   
}
   
   
public function afterValidate()
   
{
        $filename
= $this->data['Outlet']['photo'];
       
       
if (!empty($filename)) {
            move_uploaded_file
($this->data['Outlet']['photoTemp']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
       
} else {
            unset
($this->data['Outlet']['photo']);
       
}
       
        unset
($this->data['Outlet']['photoTemp']);
   
}
}


However, I still get the error message that I should upload a file with a correct extension. It's just like 'allowEmpty' => true" isn't working at all.
I quess in my case it's checking the value in $this->data['Outlet']['photoTemp']['type'] so what could possibly be the problem?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Re: File upload validation

Okay Charles , that makes sense so I changed my code:

<?php


class Outlet extends CoasterCmsAppModel
{
   
public $validate = array(
       
'name' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Nameis required.',
           
'allowEmpty' => true
       
),
       
'intro' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Intro is required.'
       
),
       
'photo' => array(
           
'validFileSize' => array( // zelf gekozen naam van de regel
               
'rule' => array('filesize', '>', 0), // verplicht
               
'on' => 'create',
               
'message' => 'Photo is required.'
           
)
       
),
       
'photoTemp' => array(
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'on' => 'create',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'allowEmpty' => true,
               
'on' => 'update',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
)
       
)
   
);
   
   
public function beforeValidate()
   
{
        $this
->data['Outlet']['photoTemp'] = $this->data['Outlet']['photo'];
        $this
->data['Outlet']['photo'] = $this->data['Outlet']['photoTemp']['name'];
   
}
   
   
public function afterValidate()
   
{
        $filename
= $this->data['Outlet']['photo'];
       
       
if (!empty($filename)) {
            move_uploaded_file
($this->data['Outlet']['photoTemp']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
       
} else {
            unset
($this->data['Outlet']['photo']);
       
}
       
        unset
($this->data['Outlet']['photoTemp']);
   
}
}


However, I still get the error message that I should upload a file with a correct extension. It's just like 'allowEmpty' => true" isn't working at all.
I quess in my case it's checking the value in $this->data['Outlet']['photoTemp']['type'] so what could possibly be the problem?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Re: File upload validation

Sam:

Based on your code the value for $this->data['Outlet']['photo'] is an array instead of a string field indicated by $validate and "$this->set('photo', $filename)".  This suggests that the type for  $this->data['Outlet']['photo'] is transitioning from an array to a string.  You would have to make sure that $this->data['Outlet']['photo'] contains a string value when validate() is executed.  Is it possible that your afterValidate function is supposed to be beforeValidate?

--Charles

On Sat, Feb 28, 2015 at 2:42 PM, Sam Clauw <sam.clauw@gmail.com> wrote:
I have a problem with my file upload validation in CakePHP.

When I add a new record with an image upload field...
  • ... image should be required.
  • ... image file extension sould be jpg, png or gif.
When I edit an existing record with an image upload field...
  • ... image is not required.
  • ... when image is choosen: image file extension sould be jpg, png or gif.
Here's my best model code attempt so far:

<?php


class Outlet extends CoasterCmsAppModel
{
   
public $validate = array(
       
'name' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Name is required.',
           
'allowEmpty' => true
       
),
       
'intro' => array(
           
'rule' => 'notEmpty', // verplicht
           
'message' => 'Intro is required.'
       
),
       
'photo' => array(
           
'validFileSize' => array( // zelf gekozen naam van de regel
               
'rule' => array('filesize', '>', 0), // verplicht
               
'on' => 'create',
               
'message' => 'Photo is required.'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'on' => 'create',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
),
           
'validExtension' => array( // zelf gekozen naam van de regel
               
'rule' => array('extension', array('jpg', 'jpeg', 'png', 'gif')),
               
'allowEmpty' => true,
               
'on' => 'update',
               
'message' => 'Photo has to contain a valid extension (jpg, jpeg, png or gif).'
           
)
       
)
   
);
   
   
public function afterValidate()
   
{
        $filename
= $this->data['Outlet']['photo']['name'];
       
       
if (!empty($filename)) {
            move_uploaded_file
($this->data['Outlet']['photo']['tmp_name'], WWW_ROOT . 'img' . DS . 'outlets' . DS . $filename);
           
            $this
->set('photo', $filename);
       
} else {
            unset
($this->data['Outlet']['photo']);
       
}
   
}
}


The "add" validation works fine for me. But strange as it is, when I edit a record, I keep getting the error message "Photo has to contain a valid extension (jpg, jpeg, png or gif)."
Somebody who can help me out of this? ;)

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.