0

I am working with Angular Pattern Validation template driven form for my Angular 9 project. I have the list of properties which I need to validate in my form, so I am trying to put that html code in *ngFor loop to reduce my code:

<form #securityForm="ngForm" class="validation-form adeco-form" >
    <ul lass="text-left" >
        <div *ngFor="let data of items[0]; let i = index;">
            <li class="col-xs-12 p0 pt-sm">
                <div class="col-xs-10 col-lg-11 ph0 pt">
                    {{securitySetting.currentSecuritySettings[data].displayName}}
                </div>
                <div class="col-xs-2 col-lg-1">
                    <input class="k-textbox mb-sm col-xs-12" name="{{data}}"
                           [ngModel]="securitySetting.currentSecuritySettings[data].value"
                           required [pattern]="securitySetting.currentSecuritySettings[data].validationExpression" ref-data="ngModel">
                </div>
                <div *ngIf="data.errors?.pattern || data.errors?.required" class="validation-msg col-xs-12">
                    {{securitySetting.currentSecuritySettings[data].validationErrorMessage}}
                </div>
            </li>
        </div>
    </ul>
</form> 

and my controller looks like:

@Component({
  selector: 'app-settingssecurity',
  templateUrl: './settingssecurity.component.html',
  styleUrls: ['./settingssecurity.component.css']
})
export class SettingssecurityComponent implements OnInit {

    items = [
        ['validate_password_regexp_minlength', 'validate_password_regexp_minlower', 'validate_password_regexp_minupper', 'validate_password_regexp_minnumeric', 'validate_password_regexp_minspecial'],
        ['password_expiration', 'password_expiration_notification_enabled', 'password_expiration_notification'],
        ['distinct_password_history'],
        ['secret_answers_attempts', 'password_reset_expiration_delay', 'secret_questions_enabled'],
        ['login_tamper_mode_attempts', 'login_tamper_mode_attempts_delay', 'login_tamper_mode_attempts_reset_delay', 'login_block_attempts'],
        ['accunt_inactivity_block'],
        ['session_inactivity_logout']
    ];
    
    securitySetting = {
        isPCIDSS: true,
        securitySettingsPCI: {},
        securitySettingsCustom: {},
        currentSecuritySettings: {},
        munitPMSsections: {},
        lockAccess: {}
    };
    
    ...
}

When I run this code my browser don't return any errors and my form looks like this: enter image description here

but unfortunately my validate error of a pattern code doesn't work.

Anybody have an idea what can I put to my input's name, ref-data="ngModel" or maybe I need to change something else to be able to see validation error of my form?

Please let me know if I need to provide more details

4

2 回答 2

0

我要做的第一件事是仔细检查 ngIf 是否通过:

 <div *ngIf="data.errors?.pattern || data.errors?.required" ...>put something here, like foobar</div>

从外观上看,您正在尝试引用“data.errors”,这不是一回事。在这种情况下,数据是数组中的一个字符串:

let data of items[0]


items[0] = 
 ['validate_password_regexp_minlength', 'validate_password_regexp_minlower', 'validate_password_regexp_minupper', 'validate_password_regexp_minnumeric', 'validate_password_regexp_minspecial'],

要访问表单中的元素,您通常会执行类似的操作 #name="ngModel",然后在其他地方说*ngIf="name.errors".

在您的情况下,它可能仍然有效,因为 ngfor 中的所有内容都是它自己的模板,但可能需要一些时髦(也许您可以做类似的事情#name{{i}}="{{data}}",我不确定)

听起来像是一个反复试验的事情,发回你最终的地方!

于 2020-08-28T03:55:05.033 回答
0

实际上,我只是通过摆脱循环找到了解决方法:

<ul [hidden]="tabCollapseFlag[0]" class="text-left" >
    <li class="col-xs-12 p0 pt-sm">
        <div class="col-xs-10 col-lg-11 ph0 pt">
            {{securitySetting.currentSecuritySettings[items[0][0]]?.displayName}}
        </div>
        <div class="col-xs-2 col-lg-1">
            <input class="k-textbox mb-sm col-xs-12" name="items[0][0]"
                   [ngModel]="securitySetting.currentSecuritySettings[items[0][0]].value"
                   required [pattern]="securitySetting.currentSecuritySettings[items[0][0]].validationExpression" #input1="ngModel">
        </div>
        <div *ngIf="input1.errors?.pattern || input1.errors?.required" class="validation-msg col-xs-12">
            {{securitySetting.currentSecuritySettings[items[0][0]].validationErrorMessage}}
        </div>
    </li>

    <li class="col-xs-12 p0 pt-sm">
        <div class="col-xs-10 col-lg-11 ph0 pt">
            {{securitySetting.currentSecuritySettings[items[0][1]]?.displayName}}
        </div>
        <div class="col-xs-2 col-lg-1">
            <input class="k-textbox mb-sm col-xs-12" name="items[0][1]"
                   [ngModel]="securitySetting.currentSecuritySettings[items[0][1]].value"
                   required [pattern]="securitySetting.currentSecuritySettings[items[0][1]].validationExpression" #input2="ngModel">
        </div>
        <div *ngIf="input2.errors?.pattern || input2.errors?.required" class="validation-msg col-xs-12">
            {{securitySetting.currentSecuritySettings[items[0][1]].validationErrorMessage}}
        </div>
    </li>

    <li class="col-xs-12 p0 pt-sm">
        <div class="col-xs-10 col-lg-11 ph0 pt">
            {{securitySetting.currentSecuritySettings[items[0][2]]?.displayName}}
        </div>
        <div class="col-xs-2 col-lg-1">
            <input class="k-textbox mb-sm col-xs-12" name="items[0][2]"
                   [ngModel]="securitySetting.currentSecuritySettings[items[0][2]].value"
                   required [pattern]="securitySetting.currentSecuritySettings[items[0][2]].validationExpression" #input3="ngModel">
        </div>
        <div *ngIf="input3.errors?.pattern || input3.errors?.required" class="validation-msg col-xs-12">
            {{securitySetting.currentSecuritySettings[items[0][2]].validationErrorMessage}}
        </div>
    </li>

    <li class="col-xs-12 p0 pt-sm">
        <div class="col-xs-10 col-lg-11 ph0 pt">
            {{securitySetting.currentSecuritySettings[items[0][3]]?.displayName}}
        </div>
        <div class="col-xs-2 col-lg-1">
            <input class="k-textbox mb-sm col-xs-12" name="items[0][3]"
                   [ngModel]="securitySetting.currentSecuritySettings[items[0][3]].value"
                   required [pattern]="securitySetting.currentSecuritySettings[items[0][3]].validationExpression" #input4="ngModel">
        </div>
        <div *ngIf="input4.errors?.pattern || input4.errors?.required" class="validation-msg col-xs-12">
            {{securitySetting.currentSecuritySettings[items[0][3]].validationErrorMessage}}
        </div>
    </li>

    <li class="col-xs-12 p0 pt-sm">
        <div class="col-xs-10 col-lg-11 ph0 pt">
            {{securitySetting.currentSecuritySettings[items[0][4]]?.displayName}}
        </div>
        <div class="col-xs-2 col-lg-1">
            <input class="k-textbox mb-sm col-xs-12" name="items[0][4]"
                   [ngModel]="securitySetting.currentSecuritySettings[items[0][4]].value"
                   required [pattern]="securitySetting.currentSecuritySettings[items[0][4]].validationExpression" #input5="ngModel">
        </div>
        <div *ngIf="input5.errors?.pattern || input5.errors?.required" class="validation-msg col-xs-12">
            {{securitySetting.currentSecuritySettings[items[0][4]].validationErrorMessage}}
        </div>
    </li>
</ul>

使用此 html 代码,我的表单会适当地对表单上的错误输入数据做出反应: 在此处输入图像描述

在此处输入图像描述

于 2020-08-30T00:04:35.190 回答