Git Product home page Git Product logo

form_field_validator's Introduction

form_field_validator

A straightforward flutter form field validator that provides common validation options.

Usage

     // assign it directly to a TextFormField validator  
     // you don't have the pass the value your self like  
     // validator: (value) => EmailValidator(errorText: 'invalid email address').call(value)  
       
     TextFormField(  
         validator: EmailValidator(errorText: 'enter a valid email address')  
         );  
   
     // create a reusable instance  
     final requiredValidator = RequiredValidator(errorText: 'this field is required');  
     
     // again assign it directly to the validator  
     TextFormField(validator: requiredValidator);  
     

Multi rules validation

   
  final passwordValidator = MultiValidator([  
    RequiredValidator(errorText: 'password is required'),  
    MinLengthValidator(8, errorText: 'password must be at least 8 digits long'),  
    PatternValidator(r'(?=.*?[#?!@$%^&*-])', errorText: 'passwords must have at least one special character')  
 ]);  
  
  String password;  
  
  Form(  
    key: _formKey,  
    child: Column(children: [  
      TextFormField(  
        obscureText: true,  
        onChanged: (val) => password = val,  
        // assign the the multi validator to the TextFormField validator  
        validator: passwordValidator,  
      ),  
  
      // using the match validator to confirm password  
      TextFormField(  
        validator: (val) => MatchValidator(errorText: 'passwords do not match').validateMatch(val, password),  
      )  
    ]),  
  );  
    

Make your own validator

   
class LYDPhoneValidator extends TextFieldValidator {  
  // pass the error text to the super constructor  
  LYDPhoneValidator({String errorText = 'enter a valid LYD phone number'}) : super(errorText);  
  
  // return false if you want the validator to return error  
  // message when the value is empty.  
  @override  
   bool get ignoreEmptyValues => true;  
    
	  @override  
	  bool isValid(String value) {  
	    // return true if the value is valid according the your condition  
	    return hasMatch(r'^((+|00)?218|0?)?(9[0-9]{8})$', value);  
	  }  
}  


   // use it by assigning it to the TextFormField validator  
   TextFormField(validator: LYDPhoneValidator());  


  
  
// you can also extend the base FieldValidator class   
// to work with non string values  
  
class CustomValidator extends FieldValidator<T>{  
   CustomValidator(String errorText) : super(errorText);  
  
	  @override  
	  bool isValid(T value) {  
	    // TODO: implement isValid  
	    return //condition;
	  }  
  }  
  

form_field_validator's People

Contributors

milad-akarie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.