3

How to make file uploading as optional with validation? The code below validates even if i didn't selected any file. I want to check the extension only if i selected the the file. If i am not selecting any file it should not return any validation error.

class Catalog extends AppModel{
    var $name = 'Catalog';
    var $validate = array(
        'name' => array(
            'rule' => '/^[a-z0-9 ]{0,}$/i',
            'allowEmpty' => false,
            'message' => 'Invalid Catalog name'
        ),
        'imageupload' => array(
            'rule' => array('extension',array('jpeg','jpg','png','gif')),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'Invalid file'
        ),
       );
}

thanks in advance

3
  • Are you pre-processing the imageupload field in the controller in any way? Commented Jun 30, 2010 at 7:09
  • @deceze : sorry what do u mean by pre-processing??? i need to save only the imagename so i assign $this->data['Catalog']['image'] = $this->data['Catalog']['imageupload']['name']; then only i will call the save Commented Jun 30, 2010 at 8:29
  • That's what I mean by pre-processing. Commented Jun 30, 2010 at 9:45

2 Answers 2

2

"I assign $this->data['Catalog']['image'] = $this->data['Catalog']['imageupload']['name'];"

So by the time you save your data array, it looks something like this I assume:

array(
    'image' => 'foobar',
    'imageupload' => array(
        'name' => 'foobar',
        'size' => 1234567,
        'error' => 0,
        ...
     )
)

Which means, the imageupload validation rule is trying to work on this data:

array(
    'name' => 'foobar',
    'size' => 1234567,
    'error' => 0,
    ...
 )

I.e. the value it's trying to validate is an array of stuff, not just a string. And that is unlikely to pass the specified validation rule. It's also probably never "empty".

Either you create a custom validation rule that can handle this array, or you need to do some more processing in the controller before you try to validate it.

Sign up to request clarification or add additional context in comments.

2 Comments

which is the best practice???custom validation or some more processing in controller
Personally I would tinker with $this->data before saving it, although I think a custom rule would be better practise.
1

Concept:

In Controller, before validating, or saving (which does validation automatically by default) check if any file is uploaded. If not uploaded, then unset validator for the file field.

Sample code:

Controller

// is any image uploaded?
$isNoFileUploaded = ($this->request->data['Model']['field_name']['error'] == UPLOAD_ERR_NO_FILE) ? true : false ;
if ($isNoFileUploaded) {
    $this->Model->validator()->remove('field_name');
}

Notes:

This solution comes under preprocessing as one of the two alternative approaches (preprocessing in controller, custom validation in model) suggested by @deceze's answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.