Проверка флажка углового материала

Мне интересно, как проверить требуемый флажок в том же стиле, что и угловой материал, который обрабатывает проверку для других элементов управления - при отправке формы элемент управления отображается красным, если его не коснуться.

Я думаю, что могу сделать это с помощью ng-style, однако я не могу придумать способ проверить, была ли форма отправлена

Вот фрагмент HTML

 <form [formGroup]="frmFinish" name="frmFinish">
        <mat-checkbox required formControlName="check1">Discussed Confidentiality and Information Sharing with Service User</mat-checkbox>
        <mat-checkbox required formControlName="check2">Discussed Disposal and Storage of Injecting Equipment and Substances</mat-checkbox>
        <mat-checkbox required formControlName="check3">Discussed Overdose Risk and Prevention</mat-checkbox>

        <mat-form-field>
            <textarea formControlName="note" [(ngModel)]="stepFive.note" matInput placeholder="Any Additional Notes" matTextareaAutosize matAutosizeMinRows="2" matAutosizeMaxRows="5"></textarea>
            <mat-hint align="end">This will be added to the profiles note's</mat-hint>
        </mat-form-field>

        <mat-card-actions>
            <button mat-raised-button type="button" class="nav-btn pull-right" (click)="fillForm()" style="background: red">Fill Form</button>
            <button mat-raised-button class="nav-btn pull-right" style="margin-left:5px;" (click)="createProfile()" type="submit">Finish</button>
            <button mat-raised-button matStepperPrevious class="nav-btn pull-right">Previous</button>
        </mat-card-actions>
    </form>

А вот и машинопись:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CreateProfileComponent } from '../create-profile.component';

@Component({
    selector: 'step-five-component',
    templateUrl: './step-five.component.html',
    styleUrls: ['../create-profile.component.css']
})
export class StepFiveComponent {
    frmFinish: FormGroup;

    stepFive = {
        note: null
    };

    constructor(private formBuilder: FormBuilder) {
        this.frmFinish = this.formBuilder.group({});
    }

    ngOnInit() {
        this.frmFinish = this.formBuilder.group({
            note: ['', Validators.required],
            check1: ['', Validators.required],
            check2: ['', Validators.required],
            check3: ['', Validators.required],
        });
    }

    createProfile(){
        if(this.frmFinish.valid){
            console.log('Creating profile..');
        }else{

        }
    }

    fillForm(){
        this.stepFive.note = 'asdasd';
    }

}

person Ben Donnelly    schedule 30.01.2018    source источник


Ответы (2)


«однако я не могу придумать способ проверить, была ли форма отправлена». Вы могли бы просто установить для поля isSubmitted значение true при вызове createProfile?

  createProfile(){
        this.isSubmitted = true;
        if(this.frmFinish.valid){
            console.log('Creating profile..');
        }else{

        }
    }

Затем вы можете использовать ngStyle или ngClass:

[ngClass]="{'has-error': checkForError() }"
person trees_are_great    schedule 30.01.2018

Это было решено в Angular версии 2.3.0.

Просто используйте: Validators.requiredTrue вместо Validators.required.

(См. выпуск GitHub #11459. См. документы Angular requiredTrue)

person Lindauson    schedule 28.09.2018
comment
Это работа, хороший ответ - person Inline Instructions.; 05.05.2021