Check if file is uploaded in validation

I need to check wether a file is uploaded and if so require a title which isnt working. uploading is fine
// rules
[[‘portfolio3’], ‘validateCredential’],

public function validateCredential($attribute, $params)
{
 	
	

	if (is_object($this->imageFile) && $this->$attribute == '') {
		$this->addError($attribute, "Enter Certificate #$no name's");
	}
   }

Problem is no error is showing even when the image is uploaded and the title isn’t filled up

The purpose of validation is to check values submitted if they fit the validation criteria.
For that you should do it on the controller. Before saving the model to the database, check if file was uploaded. If not set flash message/addError and redisplay form or redirect to specific action as necessary

2 Likes

More about uploading files

https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload

https://github.com/samdark/yii2-cookbook/blob/master/book/forms-uploading-files.md