创建一个自定义验证器以针对多个表单字段进行验证

时间:2019-11-12 09:25:48

标签: angular angular-reactive-forms angular-validation angular-validator

我具有以下表单设置:

this.form = new FormGroup({
  ...
  year: new FormControl("", validateDateMethod(year, month, day)),
  month: new FormControl("", validateDateMethod(year, month, day)),
  day: new FormControl("", validateDateMethod(year, month, day))
});

我想创建一个自定义验证器以根据3个字段验证日期。

是否可以基于多个表单值创建验证器?

普通海关验证器仅接受当前控制和任何参数值。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以将所有三个日期分组到一个表单组中,并向其中添加验证器。然后,您可以在所有三个字段中进行验证。

def operation_1():
    divisions = [(('Asia',x1_p,"%"), x1), (('North America',x2_p,"%"), x2), (('South America',x4_p,"%"), x4), (('Africa',x5_p,"%"), x5), (('Europe',x6_p,"%"), x6), (('Oceania',x7_p,"%"), x7)]
    COLORS = cycle(['red', 'blue', 'green', 'yellow', 'orange', 'brown', 'lightgreen'])
    RADIUS = 200
    CONTINENT_RADIUS = RADIUS * 1.35
    PERCENTAGE_RADIUS = RADIUS * 1.35
    FONTSIZE = 15
    FONT = ("Arial", FONTSIZE, "bold")

    total = sum(fraction for fillcolor, fraction in divisions)
    screen = turtle.getscreen()
    screen.title("Pie Chart")
    screen.setworldcoordinates(-400, -400, 400,400 )
    piechart = Turtle()
    piechart.penup()
    piechart.sety(-RADIUS)
    piechart.pendown()
    piechart.speed(0)

    for fillcolor, fraction in divisions:
        piechart.fillcolor(next(COLORS))
        piechart.begin_fill()
        piechart.circle(RADIUS, fraction * 360 / total)
        position = piechart.position()
        piechart.goto(0,0)
        piechart.end_fill()
        piechart.setposition(position)

    piechart.penup()
    piechart.sety(-CONTINENT_RADIUS)

    for continent, fraction in divisions:
        piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)
        piechart.write(continent, align="center", font=FONT)
        piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)

    piechart.hideturtle()


    x = input("Please input x to initiate another operation: ")
    if x.lower() == 'x':
        screen.clearscreen()