question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add form wide validation

See original GitHub issue

Current validation is field level only. It would be great if there was the ability to validate the form as a whole with the error message appear above or below the form (preferably configurable).

My use case is I have a password and passwordConfirm fields which I want to match. Putting a validator on one or both fields won’t work as they are dependant on each other.

An alternative approach would be a formOption which runs all field validator whenever any field changes.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
zoul0813commented, Nov 14, 2017

Here’s a custom validator I wrote for comparing two fields, and a JSFiddle example

https://jsfiddle.net/zoul0813/rukxozdk/ - using _.isEqual for more complex comparisons

https://jsfiddle.net/zoul0813/rukxozdk/4/ - without lodash, doing simple "a" == "b" check

function isEqualTo(value, field, model) {
	if(field.equals == undefined)
		return ['invalid field schema, missing `equals` property'];
	let a = model[field.equals];
	if(value == a) 
		return [];
	return ['strings do not match'];
}

Just add this to your project, then add isEqualTo to the validator array for the second password. Add an equals property to the second password fields schema pointing to the first passwords model.

[{
  type: 'input',
  inputType: 'password',
  label: 'Password',
  model: 'password',
  min: 5,
  max: 10,
  required: true,
  validator: ['string']
},
{
  type: 'input',
  inputType: 'password',
  label: 'Password (Confirm)',
  model: 'password2',
  min: 5,
  max: 10,
  required: true,
  equals: 'password',
  validator: ['string', isEqualTo]
}]
2reactions
zoul0813commented, Nov 15, 2017

@icebob, @will-fgmnt here’s a rough draft of what I’m working on.

https://jsfiddle.net/zoul0813/z3up3rwo/

Basically just a Proxy wrapper for ValidateJs, it looks at the field’s rules property. ValidateJs.equality or any of the other validators works. You can also run multiple ValidateJs validations just by adding them to the “rules” property and calling one of the virtual functions to trigger them all (the rules property is just passed to validate() as the constraint). For simple usage, the function called on ValidateJs creates a "functionName": true constraint (for things like { email: true }).

This is just a rough draft, and I plan to clean this up quite a bit … I also hope to be able to get “form-wide validation” working so that when a field is changed, all the fields revalidate. Likely going to stick with the “field by field” validation, but plan to figure out how to get the fields to revalidate themselves by listening for a validation event.

// ValidateJs Proxy for vue-form-generator
// Use ValidateJs.equality, ValidateJs.length, ValidateJs.presence, ValidateJs.format, etc for the field `validator`
let ValidateJsObj = {}
window.ValidateJs = new Proxy(ValidateJsObj, {
    get: function get(target, name) {
        return function wrapper(value, field, model) {
            let constraint = {};
            let rules = field['rules'];
            if(rules == undefined) {
                rules = {};
                rules[name] = true;
            }
            constraint[field['model']] = rules;
            let errors = validate(model, constraint);
            if(errors != undefined && errors[field['model']])
                return errors[field['model']]
            return [];
        }
    }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Client-side form validation - Learn web development | MDN
The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this ...
Read more >
The Easiest Way To Add Validation To Your Forms - Tutorialzine
In this tutorial we will show you how to add validation rules to your forms, using only native HTML input attributes. Project Overview....
Read more >
The Complete Guide to HTML Forms and Constraint Validation
How much do you know about HTML5 form validation? Even if you know a lot, we bet you'll learn things you didn't know...
Read more >
Form Validation: Why It Matters and How to Get It Right - CXL
Inline validation is a wonderful way to reduce friction in forms. It's becoming more and more common, probably because we've seen compelling ...
Read more >
Web Form Validation: Best Practices and Tutorials
With client-side validation, form never gets submitted if validation fails. Validation is being handled in JavaScript methods that you create ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found