If i understand correctly you want to make the file required on create and not on update.
If this is correct you will need to use scenarios.
public function rules() {
return [
['filefield', 'required', 'message' => '{attribute} can\'t be blank', 'on'=>'create'],
['filefield', 'file', 'extensions' => ['pdf', 'doc', 'docx', 'txt'], 'maxSize' => 1024 * 1024 * 2, 'tooBig' => 'A resume can\'t be larger than 2 mb.', 'wrongExtension' => 'Only {extensions} types are allowed for {attribute}.'],
];
}
notice on the required rule there is an on= create. "Create" is the scenario that the required should be applied to. The second rule would be applied for all scenarios and instances of the model since a specific scenario is not stated.
Note: you can name the scenarios whatever you like them to be named. You can also use as many as you like.
You need to specify your scenarios in your model (under the rules would be an ideal place). They need to have all of the attributes that need to be validated in them.
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['create'] = ['filefield', 'someotherfield', 'anotherattribute'];
$scenarios['update'] = ['filefield', 'someotherfield'];
return $scenarios;
}
To declare a scenario you have two ways; both would be when or right after you declare your model instance.
if it’s a new instance (i.e. create) you should call them like
$model = new SomeModel(['scenario' => 'create']);
The above wouldn’t work for an existing record so you can also declare a scenario like
$model = $this->findModel($id);
$model->scenario = 'update';
this also works for create
$model = new SomeModel();
$model->scenario = 'create';
update:
Here is some more information about scenarios