1

I am using a template-driven form with a custom validator on one of the fields :

<button type="button" (click)="myForm.submit()" [disabled]="!myForm.valid" class="btn">Save</button>
<form #myForm="ngForm" (ngSubmit)="submit()">
   ...
   <input type="text"
                 class="validate"
                 [(ngModel)]="myDate"
                 name="myDate"
                 ngControl="myDate"
                 myValidator/>
</form>

myValidator :

import { Directive, forwardRef } from '@angular/core'
import { NG_VALIDATORS, FormControl, Validator } from '@angular/forms'

function check(s: string) {
   ...
}

function customValidation() {
  return (c: FormControl) => {
    return check(c.value) ? null : {
      myValidator: false
    }
  }
}


@Directive({
  selector: '[myValidator ][ngModel],[myValidator ][formControl]',
  providers: [
    { provide: NG_VALIDATORS, useExisting: forwardRef(() => MyValidator), multi: true }
  ]
})
export class MyValidator implements Validator {

  validator: Function

  constructor() {
    this.validator = customValidation()
  }

  validate(c: FormControl) {
    return this.validator(c)
  }
}

Everything is working just fine on the field. The only issues comes when the validator set the field to invalid, the form isn't set to invalid and thus the save button isn't disabled. I can't exactly get why. I guess I forgot a link between the validator and the form.

I am using angular_forms 0.3.0

Here is a plunkr : http://plnkr.co/edit/qUKYGFNLyjh6mNiqYY5I?p=preview which really seems to work... (rc.4 though)

4
  • Have you tried with RC.6? Commented Sep 7, 2016 at 6:47
  • I have not yet since some of my dependencies (ng2-select by instance) are still not compatible in rc.6. So I am stuck in rc.5 for a few more days. Is there anything which seems odd in here ? Commented Sep 7, 2016 at 6:47
  • You could try in a Plunker. There were several changes and fixes related to forms. Commented Sep 7, 2016 at 6:48
  • Sorry, I have just edited Commented Sep 7, 2016 at 7:01

1 Answer 1

3

I have put your code in plunkr.

http://plnkr.co/edit/qUKYGFNLyjh6mNiqYY5I?p=preview

And it works just fine. You might check with other parts of your code. Specifically I have my own check function.

function check(s: string) {
  if(s.length > 0){
    return true;
  }else{
    return false;
  }
}

Have you initialized the myDate value? If it's not initialized i got a valid form on start.

ngOnInit() {
    this.myDate = ''
  }
Sign up to request clarification or add additional context in comments.

6 Comments

Are there any differences between your code and plunkr?
There is only one difference, which is that the plunkr has disableDeprecatedForms(), provideForms(), and I am using FormsModule
I now see that it is in RC4 maybe that's the reason.
I tried it in rc.6 with angular_forms in 2.0.0 rc.6 and it worked as well. I am a bit confused.
For the time being, is there any way to force a form to be invalid ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.