Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const validateAll = () => {
// can we move this outside of the fn?
const validator = new Validator(modelRef.value, rules)
console.log('validating')
valid.value = !dirty.value || validator.passes()
errors.value = dirty.value && validator.errors.errors
// console.log(validator.errors)
}
export const validateLogin = (values, props) => {
const { intl: { formatMessage } } = props
const rules = {
username: 'required|between:3,15|alpha_num',
password: 'required|between:6,16',
}
const messages = {
'required.username': formatMessage({ id: 'validate.username.required' }),
'between.username': formatMessage({ id: 'validate.username.between' }),
'alpha_num.username': formatMessage({ id: 'validate.username.alpha_num' }),
'required.password': formatMessage({ id: 'validate.password.required' }),
'between.password': formatMessage({ id: 'validate.password.between' }),
}
const validation = new Validator(values, rules, messages)
const errors = {}
if (validation.fails()) {
errors.username = validation.errors.first('username')
errors.password = validation.errors.first('password')
}
return errors
}
this.password2 = "";
if (field === "password" && event.target.value !== "" && event.target.value.length < 8) {
this.errors.password = "Password too short [min 8 chars]";
} else {
this.errors.password = false;
}
if (field === "email") {
let validation = new Validator(
{ email: event.target.value },
{ email: "required|email" }
);
validation.passes();
this.errors[field] = validation.errors.first(field)
}
if (field === "username") {
let validation = new Validator(
{ username: event.target.value },
{ username: "required" }
);
validation.passes();
this.errors[field] = validation.errors.first(field)
}
this[field] = event.target.value
});
if (!this.rules) {
this.errors = [];
return;
}
const rules = {
[this.name]: toJS(this.rules)
};
const attributeNames = {
[this.name]: this.label || this.name
};
const { model } = this.form;
const validator = new Validator({ model, ...values }, rules, this.messages);
validator.setAttributeNames(attributeNames);
validator.check();
this.errors = validator.errors.get(this.name);
}
onFieldChange = (field, value) => {
this.form.fields[field].value = value;
var validation = new Validator(
this.getFlattenedValues('value'),
this.getFlattenedValues('rule'));
this.form.meta.isValid = validation.passes();
this.form.fields[field].error = validation.errors.first(field)
};
validate() {
if (!this.validationRules || !this.dirty) {
return;
}
let data = {
value: this.value
};
let rules = {
value: this.validationRules
};
let validation = new Validator(data, rules, this.validationMessages);
validation.setAttributeNames({ value: this.name.replace(/_/g, ' ') });
this.valid = validation.passes();
if (!this.valid) {
this.validationError = validation.errors.first('value');
}
}
}
handleChange = field => event => {
this.setFieldValue(field, event.target.value);
let { email, password } = this.form.fields;
var validation = new Validator(
{ email: email.value, password: password.value },
{ email: email.rule, password: password.rule }
);
this.setValid(validation.passes());
this.setFieldError(field, validation.errors.first(field));
};
public static validate = (value: any, rule: string): IValidationResult => {
const validator = new Validator(
{
value
},
{
value: rule
},
ruleTranslations
);
const isValid = Boolean(validator.passes());
const firstErrorMessage = validator.errors.first('value');
return {
isValid,
errorMessage: typeof firstErrorMessage === 'string' ? firstErrorMessage : ''
};
const validate = (name, value) => {
const validation = new Validator(
{ [name]: value },
{ [name]: rules[name] },
);
const errorMessage = validation.fails() && validation.errors.first(name);
if (errorMessage) {
newErrors[name] = errorMessage;
hasError = false;
} else {
delete newErrors[name];
}
};