does someone know a way to find out for an Angular2 FormControl if the required validor is registered for the control.
this.form = builder.group({name: [\'\'
With the assumption that the only error registered initially is the required error
// in your component you'll have access to `this.myFormGroup`
const formGroup = {
controls: {
email: {
errors: {
required: true
}
},
username: {
errors: null
}
}
}
// required by default
let required = {
email: '*',
username: '*',
};
// in `ngOnInit`
required = Object.entries(formGroup.controls)
.map(([key, control]) => [key, control.errors])
.filter(([, errors]) => !errors)
.map(([key]) => [key, ''])
.reduce((_required, [key, isRequired]) => Object.assign(_required, { [key]: isRequired }), required)
// in your template you may write ``
console.log(required)