angular - Disabled input validation in dynamic form -
i have dynamic form (made example using angular.io dynamic form live example plunkr) , want disable input of form, display readonly information.
so decided add disabled
attribute question model:
export class questionbase<t>{ value: t; key: string; label: string; required: boolean; order: number; controltype: string; disabled?:boolean; constructor(options: { value?: t, key?: string, label?: string, required?: boolean, order?: number, controltype?: string, disabled?:boolean } = {}) { this.value = options.value; this.key = options.key || ''; this.label = options.label || ''; this.required = !!options.required; this.order = options.order === undefined ? 1 : options.order; this.controltype = options.controltype || ''; this.disabled = options.disabled || false; } }
and bind disabled input:
<input *ngswitchcase="'textbox'" [disabled]="question.disabled" [formcontrolname]="question.key" [id]="question.key" [type]="question.type">
i warning, , input not disabled:
it looks you're using disabled attribute reactive form directive. if set disabled true when set control in component class, disabled attribute set in dom you. recommend using approach avoid 'changed after checked' errors. example: form = new formgroup({ first: new formcontrol({value: 'nancy', disabled: true}, validators.required), last: new formcontrol('drew', validators.required) });
so did it's written in warning , problem, validator seems not disabled field, if it's not marked required.
here changed new questioncontrolservice
class:
@injectable() export class questioncontrolservice { constructor() { } toformgroup(questions: questionbase<any>[] ) { let group: = {}; questions.foreach(question => { group[question.key] = question.required ? new formcontrol({value: question.value || '', disabled: question.disabled}, validators.required) : new formcontrol({value: question.value || '', disabled: question.disabled}); }); return new formgroup(group); } }
problem
the disabled test
field disabled, not valid, should not possible since has not been modified @ all.
plunkr issue: http://plnkr.co/edit/qsdnd2xwwuwafytodnx1?p=preview
i submitted issue on github , turns out desired behaviour.
my error here check each field validity instead of checking whole form.
Comments
Post a Comment