1
votes

im creating a form for user management in admincp and i want it to include an image uploader for user avatars (in the user edit form)...
and want to declare its rules ... like being in a jpg or jpeg format
but it forces the attribute to be required! which i dont want to!
i want it to validate only if a file is uploaded and its in one of types of jpg gif png...
and if the file is not sent ... just leave it and validate another form data
here is my rules function:

public function rules() {
    return array(
        array('avatarpic', 'file', 'types'=>'jpg, gif, png'),
    );
}


is there any validation alias like not_required?

1

1 Answers

0
votes

Yes you can set image validation without attribute must required. Try this way

Set the file validator's "allowEmpty" to be true.

Unlike other validators, the default value for this option in file validator is false.

public function rules() {
   return array(
     array('avatarpic', 'file','allowEmpty'=>true, 'types'=>'jpg, gif, png'),
   );
 }