You can just set the value to a control as soon as you get it or you can reset the control to and initial value (release 4.0.0)
// `fb.group` is just a syntax sugar, you can simple use FormGroup if you want.finalform=fb.group({'name':'some init value'});_executeFirebaseAsyncMethod().then((newValue){// sets the control init valueform.value={'name':newValue};});
or
finalform=fb.group({'name':'some init value'});_executeFirebaseAsyncMethod().then((newValue){// resets the control to untouched and set init valueform.reset(value:{'name':newValue});});
One more thing it is possible to untouch a reactiveTextfield upon editing?
For example, I have an async validation that will check if an email is existing then, when I go to the editing profile screen it automatically validates the value of that field which I don't like. I want to validate it when the value of that field changes.
Lastly, is it possible to add an additional parameter to custom validator?
Async validators executes after every Sync validator executes without validation errors. That means that if you add for example Validators.required or Validators.email to your ReactiveTextField only when those validators marks the input as valid then the Async Validator excecutes.
But I think you are right about the issue, I will release soon a new version that let you ask inside your validator if the control is "dirty" or not, so you can check if the value has changed or not and request the server in the async validator. Mean while you can implement a workaround by asking inside your async validator if the value of the FormControl is different from the value of your model.
About the Second question, yes you can create a custom validator with additional params, there are two ways of doing this:
1-) Create a function/method that receives arguments and returns a ValidatorFunction. An example of this is the Validators.mustMatch. You can check the README.md.
2-) Extend Validator class.
Creating a function/method
// controlName and matchingControlName as additional argumentsMap<String,dynamic>_mustMatch(StringcontrolName,StringmatchingControlName){// returns a ValidatorFunctionreturn(AbstractControlcontrol){finalform=controlasFormGroup;finalformControl=form.control(controlName);finalmatchingFormControl=form.control(matchingControlName);if(formControl.value!=matchingFormControl.value){matchingFormControl.addError({'mustMatch':true});// force messages to show up as soon as possiblematchingFormControl.touch();}else{matchingFormControl.setErrors({});}returnnull;};}
Extend Validator class (this is the implementation of Validators.pattern)
/// Validator that requires the control's value to match a regex pattern.classPatternValidatorextendsValidator{finalPatternpattern;/// Constructs an instance of [PatternValidator].////// The [pattern] argument must not be null.PatternValidator(this.pattern):assert(pattern!=null);@overrideMap<String,dynamic>validate(AbstractControlcontrol){RegExpregex=newRegExp(this.pattern);return(control.value==null||control.value==''||regex.hasMatch(control.value))?null:{ValidationMessage.pattern:{'requiredPattern':this.pattern.toString(),'actualValue':control.value,}};}}
Validators.mustMatch and Validators.pattern are already included in Reactive Forms so you don't have to implement them, the above code are just examples to get the idea.
Hi may I ask how did you put an initial value coming from firebase?
Hi John,
You can just set the value to a control as soon as you get it or you can reset the control to and initial value (release 4.0.0)
or
That's great! Thank you so much! 💞
One more thing it is possible to untouch a reactiveTextfield upon editing?
For example, I have an async validation that will check if an email is existing then, when I go to the editing profile screen it automatically validates the value of that field which I don't like. I want to validate it when the value of that field changes.
Lastly, is it possible to add an additional parameter to custom validator?
Hello John,
Async validators executes after every Sync validator executes without validation errors. That means that if you add for example Validators.required or Validators.email to your ReactiveTextField only when those validators marks the input as valid then the Async Validator excecutes.
But I think you are right about the issue, I will release soon a new version that let you ask inside your validator if the control is "dirty" or not, so you can check if the value has changed or not and request the server in the async validator. Mean while you can implement a workaround by asking inside your async validator if the value of the FormControl is different from the value of your model.
About the Second question, yes you can create a custom validator with additional params, there are two ways of doing this:
1-) Create a function/method that receives arguments and returns a ValidatorFunction. An example of this is the Validators.mustMatch. You can check the README.md.
2-) Extend Validator class.
Creating a function/method
Usage
Extend Validator class (this is the implementation of Validators.pattern)
Usage
Validators.mustMatch and Validators.pattern are already included in Reactive Forms so you don't have to implement them, the above code are just examples to get the idea.
Thank you so much for your help! I will look forward to the next version of your package! ❤