在一个指令中格式化和验证

问题描述 投票:1回答:1

我正在尝试建立一个指令来处理涉及工期的表格。我希望该表单与一个文本框一起显示,该文本框的格式类似于1:24,但总分钟数-在这种情况下为144(这样以后以后可以增加很多时间!)。

这是我当前的代码:

@Directive({
...
)
export class DurationFormatDirective implements ControlValueAccessor {
  @HostBinding('value') inputValue;

  mins: number;
  onChange;
  onTouched;

  constructor() {}

  @HostListener('blur', ['$event.target.value'])
  onBlur(value: string) {
    let hrs: number, mins: number;

    if (value === '' || value === '0') {
      hrs = 0;
      mins = 0;
    } else {
      if (value.indexOf(':') === -1) { // there is no colon
        if (value.length <= 2) {
          hrs = 0;
          mins = +value;
        } else {
          hrs = +value.substring(0, value.length - 2);
          mins = +value.substring(value.length - 2);
        }
      } else { // There is a colon
        const arr = value.split(':');
        hrs = +arr[0];
        mins = +arr[1];
      }
    }

    this.mins = hrs * 60 + mins;
    this.inputValue = `${hrs}:${mins < 10 ? '0' : ''}${mins}`;
    this.onChange(this.mins);
    this.onTouched();
  }

  writeValue(val) {
    this.mins = val;

    const hrs = Math.floor(val / 60);
    const mins = val % 60;

    this.inputValue = `${hrs}:${mins < 10 ? '0' : ''}${mins}`;
  }

  registerOnChange(fn) {
    this.onChange = fn;
  }

  registerOnTouched(fn) {
    this.onTouched = fn;
  }
}

通常,此方法运行良好,但是例如,如果用户输入2:zw,则会中断,因为zw不是数字。如果输入了无效的(IE Not 0-9或::),则应声明该字段无效,并且不要尝试对其进行格式设置或更新该值。我是否还可以使此指令同时更改有效属性。如果有所作为,我正在使用反应形式。

谢谢

angular angular-forms
1个回答
0
投票

所以我实际上以不同的方式解决了这个问题。

我添加了一个RegExp测试来查找字母,并添加了一个if语句来查找分钟>60。在任何一种情况下,该值都设置为NaN。

然后我创建了一个非常简单的验证器,以检查父表单上的NaN。

这也使我以后可以进行另一次验证,在此我检查输入的持续时间是否大于另一个字段的总持续时间。

指令的完整代码:

@Directive({
  selector: '[appTimeFormat]',
})
export class TimeFormatDirective implements ControlValueAccessor {
  @HostBinding('value') inputValue;

  onChange;
  onTouched;

  private regex = /^\d{1,2}:?\d{0,2}$/;

  constructor() {}

  @HostListener('blur', ['$event.target.value'])
  onBlur(value: string) {
    this.onTouched();

    if (!this.regex.test(value)) {
      this.onChange(NaN);
      return;
    }

    let hrs: number, mins: number;

    if (value === '' || value === '0') {
      hrs = 0;
      mins = 0;
    } else {
      if (value.indexOf(':') === -1) {
        // There is no colon
        if (value.length <= 2) {
          hrs = 0;
          mins = +value;
        } else {
          hrs = +value.substring(0, value.length - 2);
          mins = +value.substring(value.length - 2);
        }
      } else {
        // There is a colon
        const arr = value.split(':');
        hrs = +arr[0];
        mins = +arr[1];
      }
    }

    this.inputValue = `${hrs}:${mins < 10 ? '0' : ''}${mins}`;

    if (mins > 60) {
      this.onChange(NaN);
      return;
    }

    this.onChange(hrs * 60 + mins);
  }

  writeValue(val) {
    if (val) {
      const hrs = Math.floor(val / 60);
      const mins = val % 60;

      this.inputValue = `${hrs}:${mins < 10 ? '0' : ''}${mins}`;
    } else {
      this.inputValue = '';
    }
  }

  registerOnChange(fn) {
    this.onChange = fn;
  }

  registerOnTouched(fn) {
    this.onTouched = fn;
  }
}

验证人:

  nanValidator(control: FormControl) {
    if (isNaN(control.value)) {
      return { invalidNumber: true };
    }
    return null;
  }
© www.soinside.com 2019 - 2024. All rights reserved.