【问题标题】:Getting error while implementing multiple validation for input field in Angular4在Angular4中为输入字段实现多重验证时出错
【发布时间】:2018-03-14 10:45:11
【问题描述】:

我在使用 Angular4 对单个输入字段进行多重验证时遇到以下错误。

错误:

ERROR in src/app/about/about.component.ts(30,7): error TS1117: An object literal cannot have multiple properties with the same name in strict mode.
src/app/about/about.component.ts(30,7): error TS2300: Duplicate identifier 'url

这是我的代码:

关于.component.html:

<form [formGroup]="textForm" (ngSubmit)="onTextFormSubmit()">
<input type="text" placeholder="please enter url" formControlName="url" id="weburl"><label *ngIf="textForm.get('url').invalid && processValidation" [ngClass] = "'error'"> Url is required. </label>
<button type="submit">ADD</button>
</form>

关于.component.ts:

export class AboutComponent implements OnInit {
  private headers = new Headers({'Content-Type':'application/json'});
  aboutData = [];
  processValidation = false;
  pattern="/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/";
  filePath:string;
  filelist: Array<{filename: string, intkey: string}> = [{
      filename: 'http://oditek.in/jslib/jslib.js',
      intkey: 'aboutlib'
      },{
      filename: 'http://oditek.in/jslib/aboutjs.js',
      intkey: 'aboutjs'
  }];
  textForm = new FormGroup({
      url: new FormControl('', Validators.required),
      url: new FormControl('', Validators.pattern(this.pattern))
  });
  constructor(private router:Router,private route:ActivatedRoute,private http:Http) { }
  ngAfterViewChecked() {
    $('#title').attr('style','font-weight:bold');
    /*$.getScript(this.filePath,function(){
      setTimeout(function(){
        checkJS();
      }, 5000);
    })*/
  }
  ngOnInit() {
    this.route.params.subscribe(params=>{
      this.filelist.forEach(item => {
        let parampath=atob(params['filepath']);
        if(item.intkey==parampath)
          this.filePath = item.filename;
        else
          return;
      });
    });
    this.http.get('http://localhost:3000/articles').subscribe(
      (res:Response)=>{
        this.aboutData = res.json();
      }
    )
  }
  onTextFormSubmit(){
    this.processValidation = true;
    if (this.textForm.invalid) {
      return;
    } 
    let url = this.textForm.value;
  }
}

我在这里需要单个输入字段的空白字段和​​模式验证。所有相应的错误消息都将显示在输入字段下方,但我收到此错误。

【问题讨论】:

  • 注意:这个问题about.componet.* 中的文件名似乎拼错了,我将它们改成了about.component.*。如果它们确实正确,请修改它们。

标签: javascript angular


【解决方案1】:

问题出在你的这段代码中:

textForm = new FormGroup({
      url: new FormControl('', Validators.required),
      url: new FormControl('', Validators.pattern(this.pattern))
});

您不需要添加 2 个同名的控件来放置 2 个验证。您可以插入验证器数组,如下所示:

textForm = new FormGroup({
      url: new FormControl('', [Validators.required, Validators.pattern(this.pattern)])
});

【讨论】:

  • 是的,但我认为该模式无法验证 URL 类型输入,因为如果我提供像 http//example.com 这样的输入,仍然会显示错误消息。
  • 这将花费太多时间来制作 angular4。
  • 验证器不工作。我做了一个 plnkr 之类的东西。 stackblitz.com/edit/angular-wqz9sb?file=app%2Fapp.component.ts
【解决方案2】:

你创建urlFormControl 是错误的,因为你不需要创建两个控件。你应该结合你的验证器:

解决方案 1:

textForm = new FormGroup({
      url: new FormControl('', Validators.compose([Validators.required, Validators.pattern(this.pattern)]))
});

解决方案 2:

textForm = new FormGroup({
          url: new FormControl('', [Validators.required, Validators.pattern(this.pattern)])
    });

【讨论】:

  • 您的修改显示错误i.e-expected 1 argument but got 2
  • @satya 我更新了第一个解决方案的代码。我忘了加括号 []
  • 是的,但是错误将如何在 html 页面中显示?
  • @satya 检查这个答案(stackoverflow.com/questions/48658368/…)关于角度反应形式的错误处理。在这个答案中,您将找到一个现场演示。
  • @satya 这是一个带有验证的演示链接:stackblitz.com/edit/angular-6gnxfx
【解决方案3】:

其实你是新建一个url的控件,不能在一个表单中创建两个控件

this.formName.group({
      title: [null,[Validators.required,Validators.pattern(this.pattern)]],
})

【讨论】:

  • 我确实喜欢textForm = new FormGroup({ url: new FormControl('', Validators.required,Validators.pattern(this.pattern)) });,因为您仍然有一些错误。
  • 传入数组不是函数 textForm = new FormGroup({ url: new FormControl('', [Validators.required,Validators.pattern(this.pattern))] });
  • 请测试可能是这样的
  • 我喜欢textForm = new FormGroup({ url: new FormControl('', [Validators.required, Validators.pattern(this.pattern))] });,但显示错误, expected
猜你喜欢
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 2020-09-28
  • 2017-06-09
  • 1970-01-01
相关资源
最近更新 更多