Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49421

Using form Validators return values for error messages

$
0
0

@wterrill wrote:

In a validator connected with a form, I have something like this:

import { FormControl } from '@angular/forms';
export class SpotValidator {
    static isValid(control: FormControl): any {

        if (control.value == ""){
            return null;
        }

        if(isNaN(control.value)){
            return {
                "not a number": true
            };
        }

        if(control.value % 1 !== 0){
            return {
                "not a whole number": true
            };
        }

        if(control.value < 31){
            return {
                "too low": true
            };
        }

        if (control.parent.value.building == "metropolitan"){

            if (control.value > 260 && control.value < 10000){ //this allows for test parking spots during validation.
                return {
                    "too high": true
                };
            }
        }

        return null;
    }

}

How can I show the errors “too high”, “too low”, “not a number”, and “not a whole number” in the html of the form. In other words, I see that something is being returned to the html or the ts code (I’m not sure which) but I’m not sure how to access that return so that I can display the proper message.

I came across the angular custom validation write-up but, I’m sad to admit that I couldn’t quite make the leap between their example and what I’m trying to do.

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 49421

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>