-
-
Save pavsidhu/9934838fac0cd68ef5d46b848a41760e to your computer and use it in GitHub Desktop.
import validation from 'validation.js' | |
export default function validate(fieldName, value) { | |
// Validate.js validates your values as an object | |
// e.g. var form = {email: '[email protected]'} | |
// Line 8-9 creates an object based on the field name and field value | |
var formValues = {} | |
formValues[fieldName] = value | |
// Line 13-14 creates an temporary form with the validation fields | |
// e.g. var formFields = { | |
// email: { | |
// presence: { | |
// message: 'Email is blank' | |
// } | |
// } | |
var formFields = {} | |
formFields[fieldName] = validation[field] | |
// The formValues and validated against the formFields | |
// the variable result hold the error messages of the field | |
const result = validatejs(formValues, formFields) | |
// If there is an error message, return it! | |
if (result) { | |
// Return only the field error message if there are multiple | |
return result[field][0] | |
} | |
return null | |
} |
#Actually, it is not an error, but it is only structure of the code. using that you have to make your own wrapper
I try to make that as under...
import validation from 'validate.js'
export default function validate(fieldName, value) {
var constraints = {
email: {
presence: true,
format: {
pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
message: 'Invalid email id',
}
},
password: {
presence: true,
length: {
minimum: 4,
message: 'Invalid Password',
}
},
confirmPassword: {
presence: true,
equality: 'password'
},
phoneNo: {
presence: true,
format: {
pattern: "^[0-9]{10}$",
message: 'Invalid phone number',
},
},
};
var formValues = {}
formValues[fieldName] = value
var formFields = {}
formFields[fieldName] = constraints[fieldName]
const result = validation(formValues, formFields)
if (result) {
return result[fieldName][0]
}
return null
}
There is actually a slight typo with the original post as well.
Change import validation from 'validation.js'
to import validation from 'validate.js'
👍
I got the following error: Cannot set property 'tests' of undefined
Here is my code.
import validation from 'validation.js'
export default function validate(fieldName, value) {
var constraints = {
email: {
presence: {
message: "Email is required"
},
email: {
message: "Doesn't look like a valid email"
}
}
};
var formValues = {};
formValues[fieldName] = value;
var formFields = {};
formFields[fieldName] = constraints[fieldName];
const result = validation(formValues, formFields);
if (result) {
return result[fieldName][0];
}
return null;
}
For those who face the issue as @Big-Silver mentioned:
There are two similar npm packages available: validation.js and validate.js, this similarity by name becomes a confusion.
This approach works only with validate.js, so make sure to install the correct one: npm install --save validate.js
#Actually, it is not an error, but it is only structure of the code. using that you have to make your own wrapper
I try to make that as under...import validation from 'validate.js' export default function validate(fieldName, value) { var constraints = { email: { presence: true, format: { pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, message: 'Invalid email id', } }, password: { presence: true, length: { minimum: 4, message: 'Invalid Password', } }, confirmPassword: { presence: true, equality: 'password' }, phoneNo: { presence: true, format: { pattern: "^[0-9]{10}$", message: 'Invalid phone number', }, }, }; var formValues = {} formValues[fieldName] = value var formFields = {} formFields[fieldName] = constraints[fieldName] const result = validation(formValues, formFields) if (result) { return result[fieldName][0] } return null }
For some strange reasons the confirmPassword
field always returns a validation error, please how can I solve this blocker
Hi, this code have error on field and unknow validatejs function.