2

i m trying to validate file field in cakephp in model with valid extension on create and on update try to validate file only if field is not empty.On Create the validation works fine, But on update it validates the if field is empty.I want to validate extensions only when the field is not empty here is my validation rule in model validation array

'image' => array(
        'rule1'=>array(
            'rule' => array('extension',array('jpeg','jpg','png','gif')),
            'required' => 'create',
            'allowEmpty' => true,
            'message' => 'Select Valid Image',
            'on' => 'create',
            'last'=>true
        ),
        'rule2'=>array(
            'rule' => array('extension',array('jpeg','jpg','png','gif')),
            //'required' => 'create',
            'allowEmpty' => true,
            'message' => 'Select Valid Image',
            'on' => 'update',
        ),
    ),
2
  • Are you using a plugin? If so, include that info in your question. If not, consider using one, such as github.com/josegonzalez/upload Commented May 16, 2013 at 9:18
  • No, I am not using any plugin,but i use models beforeSave method to upload and process files Commented May 16, 2013 at 9:28

2 Answers 2

4

Here is the correct way to validate image field with required on create and can allow empty on update of image field

Image Field Validation Array

'image' => array(
    'rule1'=>array(
        'rule' => array('extension',array('jpeg','jpg','png','gif')),
        'required' => 'create',
        'allowEmpty' => true,
        'message' => 'Select Valid Image',
        'on' => 'create',
        'last'=>true
    ),
    'rule2'=>array(
        'rule' => array('extension',array('jpeg','jpg','png','gif')),
        'message' => 'Select Valid Image',
        'on' => 'update',
    ),
),

And unset the image field on in beforevalidation in update action

function beforeValidate($options = array()){
    if(empty($this->data[$this->alias]['id']))
    {
        return true;
    }
    else
    {
        if(empty($this->data[$this->alias]["image"]["name"])){
        unset($this->data[$this->alias]["image"]);
        }
        return true; //this is required, otherwise validation will always fail
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

We can do it by Custom validation as follows

public $validate =array(
    'image' => array(
        'rule' => array('checkValidImage'),
        )
    );


 public function checkValidImage($field)
      {

        $extension = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/jpg');
        $isValidFile = in_array($field['new_image']['type'], $extension);
        $errors = array();
        $editMethod = false;
        if(!empty($this->data['ModelName']['id'])) // It will work for Update Method
        {
          if(!empty($this->data['ModelName']['image']['name'] ))
          {
            if (($field['image']['error'] == 1)) 
            {
                $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
            }
            else if (empty($field['image']['name']))
            {
                $errors [] = "Please upload image";
            } 
            else if ($field['image']['size'] >= 2097152) {
                $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
            }
            else if ($isValidFile !=1)
            {

                $errors [] = "Please select file in gif,jpeg,png format.";
            }

          }else
                {
                    $errors [] = "Please select file in gif,jpeg,png format.";
                }

        }
        else
        {
            if(!empty($this->data['ModelName']['image']['name'] )) // It will work for Create Method
            {
                if (($field['image']['error'] == 1)) 
                {
                    $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
                }
                else if (empty($field['ModelName']['name']))
                {
                    $errors [] = "Please upload image";
                } 
                else if ($field['ModelName']['size'] >= 2097152) {
                    $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
                }
                else if (!(in_array($field['image']['type'], $extension)))
                {
                    $errors [] = "Please select file in gif,jpeg,png format.";
                }

            }



        }

        if (!empty($errors))
        {
            return implode("\n", $errors);
        }
        return true;
    }

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.