ngControl
with a value of new Control('', Validators.required)
didn't work even when the file is valid.
(and actually, I found it difficult to validate radio buttons as well...)
ngControl
with a value of new Control('', Validators.required)
didn't work even when the file is valid.
(and actually, I found it difficult to validate radio buttons as well...)
Validators.required
depends on the value of the field.
Input type file
does not have a value, therefore is considered as undefined
or null
.
That is why it is invalid. Better write you own validation.
For custom file validation example refer
An example of validator to use with the normal require attribute could be something like this
import { Provider, forwardRef, Directive } from '@angular/core';
import { NG_VALIDATORS, Validator, Control } from '@angular/common';
export const NO_ATTACHMENT_VALIDATOR = new Provider(NG_VALIDATORS,{
useExisting: forwardRef(() => noAttachmentValidator ),
multi: true
});
@Directive({
selector: '[noAttachmentValidator][ngControl],[noAttachmentValidator][ngFormControl],[noAttachmentValidator][ngModel]',
providers: [NO_ATTACHMENT_VALIDATOR]
})
export class noAttachmentValidator implements Validator{
public validate(control: Control) : { required: { [key: string]: boolean } | null } {
let state,
value = control.value,
alreadyUsed = control.dirty;
if(alreadyUsed && value.length == 0){
state = true;
}
return state ? { required : { 'required' : false } } : null
}
}
Basically required attribute is checking the first time, this every time after that you have used the input the first time,even if you remove the file (that was my problem ) cause the control of the value is for null. And when it's untouched it's null,then it's become filled when you add a file, but if you remove it it's just an empty array so [] !== null . I hope this helps follow this you can build you own type of validation if you need to.
One great solution to resolve this issue is to create a custom element which wrap your and implements a ValueAccessor. This way you can create a custom component which works exactly like the other form's elements. Instead of using the input value you can use another variable inside your component.
More information here : Applying angular2 form directives to custom input form elements