Monday, April 13, 2015

Re: [Cake 2.6.3] Cake's FileUpload Validation not working; what is wrong?

It worked thanks!

On Monday, April 13, 2015 at 6:18:36 PM UTC+2, John Andersen wrote:
The validation in your model points to a field named "image", but the view you have points to a field named "submittedFile" :)
Which is the correct one?
Enjoy, John

On Monday, 13 April 2015 11:17:13 UTC+3, Willem wrote:
I have a model "Attachment" which Belongs to another model (hasMany Attachment). 

In model Attachment I put a validator (from the Cake helpbook) to check against filetypes. After saveAssociated i would expect an error if uploaded a .txt file, but the model is save successfully?

Add.ctp 

for($i=0;$i<1;$i++)
{
    ?>
    <div class="row">
        <?php
        echo $this->Form->file('Attachment.'.$i.'.submittedfile');
        ?>
    </div>
<?php
}



Attachment model:

<?php
App::uses('AppModel', 'Model');

class Attachment extends AppModel {

    public $belongsTo = 'CollectionRequest';

    public $validate = array(
        'image' => array(
            'rule' => array(
                'extension',
                array('gif', 'jpeg', 'png', 'jpg')
            ),
            'message' => 'Please supply a valid image.'
        )
    );


    public function beforeSave($options = array()) {

        parent::beforeSave($options);
    }

    public function beforeValidate($options = array()) {

        // ignore empty file - causes issues with form validation when file is empty and optional
        if (!empty($this->data[$this->alias]['error']) &&
            $this->data[$this->alias]['error']==4 &&
            $this->data[$this->alias]['size']==0) {
            unset($this->data[$this->alias]);
        }


        parent::beforeValidate($options);
    }

}


?>

Controller

 if ($this->CollectionRequest->saveAssociated($this->request->data)) {
                // All good
            
                $this->Session->setFlash(__('CollectionRequest saved'));
                return $this->redirect(array('action' => 'index'));
            }


thanks


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

No comments: