GithubHelp home page GithubHelp logo

darkobits / formation Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 3.57 MB

⌨️ Making Angular 1 forms suck less.

License: Do What The F*ck You Want To Public License

JavaScript 90.83% HTML 1.38% SCSS 7.79%
angular-1 forms

formation's Introduction



Hi! I'm Joshua. I build things with JavaScript.

I'm passionate about:

🧑‍💻  Developer Experience
💅  Design Language Systems
🌎  Distributed Systems
⚙️  Software Development Processes

Languages & Tools

                    

formation's People

Contributors

darkobits avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mkane827

formation's Issues

[Proposal] Support Dynamic Copy for ngMessages

Support Dynamic Copy for ngMessages

Use Case:

In an address form, a developer may be doing validation to check that a user's postal code is valid for the state/province they have selected. The developer would like to tailor the validation error message for the postal code field to say something like, "Please enter a valid zip code for California."

This requires that the ngMessage copy be dynamic.

Proposal:

Allow developers to pass either a string literal or a function when defining error messages:

vm.controls = {
  postalCode: {
    errors: [
      // Static error copy.
      ['required', 'This field is required.'],
      // Dynamic error copy.
      ['pattern', () => {
        const state = vm.addressForm.getModelValues().state;
        return `Please enter a valid postal code for ${state}.`;
      }]
    ]
  }
}

This approach would allow a developer to use any data in the context of the form's parent controller's closure to compute the copy for the error message. The downside, however, is that these definitions now become very tightly-coupled to the specific form in which they are being used; the error message function is making an assumption about control names in the form. This makes dynamic definitions less share-able and less reusable.

One way to combat this could be to ensure that these functions have fallbacks if their assumptions are false:

vm.controls = {
  postalCode: {
    errors: [
      // Static error copy.
      ['required', 'This field is required.'],
      // Dynamic error copy.
      ['pattern', () => {
        const state = vm.addressForm.getModelValues().state;
        
        if (state) {
          return `Please enter a valid postal code for ${state}.`;
        } else {
          // Form doesn't have a control named "state", or the
          // state control has a falsy model value. 
          return 'Please enter a valid postal code.';
        }
      }]
    ]
  }
}

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.