角度2中的多个自定义验证

时间:2017-08-25 11:13:42

标签: angular validation angular2-forms

我在向表单添加多个自定义验证时遇到问题。我只能在表单中添加一个自定义验证。如何添加多个验证。

例如:

this.user = this.fb.group({
  name: ['', Validators.required],
  account: this.fb.group({
    email: ['', Validators.required],
    confirm: ['', Validators.required]
  }, { validator: emailMatcher })
});

我想添加更多验证,例如:{validator:[emailMatcher,matchPassword,othervalidation]}

角度2中是否有某些东西可以帮助我。

1 个答案:

答案 0 :(得分:3)

而不是:

{ Validators:  [emailMatcher, matchPassword, othervalidation]}

使用语法:

/*
To replicate:
type SuperArrow<'a, 'b> = 
    | Fn of (('a -> 'b) * string)
    | Compose of SuperArrowCompose<'a,'b>
and SuperArrowCompose<'a,'b> = 
    abstract member eval : SuperArrowComposeEvaluator<'a, 'b, 'r> -> 'r
and SuperArrowComposeEvaluator<'a, 'b, 'r> = 
    abstract member apply<'c> : SuperArrow<'a, 'c> -> SuperArrow<'c, 'b> -> 'r
*/

//#[derive(Debug)]
struct MyFnWrapper<A, B> {
    f: Box<Fn(A) -> B>,
    n: String,
}

impl<A, B> MyFnWrapper<A, B> {
    pub fn new<F: 'static + Fn(A) -> B>(f: F, s: String) -> Self {
        MyFnWrapper {
            f: Box::new(f),
            n: s,
        }
    }
}

trait SuperArrowComposeEval<A, B, R> {
    fn apply<C>(&self, &SuperArrow<A, C>, &SuperArrow<C, B>) -> R;
}

trait SuperArrowCompose<A, B> {
    fn eval<R, E: SuperArrowComposeEval<A, B, R>>(&self, &E) -> R;
}

struct SuperArrowComposeImpl<A, B, C> {
    fnA: Box<SuperArrow<A, C>>,
    fnB: Box<SuperArrow<C, B>>,
}

impl<A, B, C> SuperArrowComposeImpl<A, B, C> {
    fn new(fnA: SuperArrow<A, C>, fnB: SuperArrow<B, C>) {
        SuperArrowComposeImpl {
            fnA: Box::new(fnA),
            fnB: Box::new(fnB),
        }
    }
}

impl<A, B, C> SuperArrowCompose<A, B> for SuperArrowComposeImpl<A, B, C> {
    fn eval<R, E: SuperArrowComposeEval<A, B, R>>(&self, evaler: &E) -> R {
        evaler.eval::<C>(self.fnA, self.fnB)
    }
}

//#[derive(Debug)]
enum SuperArrow<A, B> {
    Function(MyFnWrapper<A, B>),
    Compose(Box<SuperArrowCompose<A, B>>),
}
祝你好运。