backbone.validation icon indicating copy to clipboard operation
backbone.validation copied to clipboard

fn function won't update message output for if else statements msg output

Open kirkdm opened this issue 6 years ago • 0 comments

Below is my code for validating a model but the fn: function doesn't update the msg output when you change the value of the form for e.g if I add text 'Admin' it will validate the first part of the value in the if statement but then it will validate the second part and the message changes but the message output won't update so it's always the first msg not the second. My other question is how do you have multiple validations for an fn call back it's like you can't. I mean I want to validate other things that have a function because I have other regular expressions to test that are different from each other. Even if I turn it into an array - multiple fn functions won't work

`define([ 'jquery', 'underscore', 'backbone', 'app', 'backbone-validation', 'models/validation' ], function($, _, Backbone, App, bv, v) {

App.Models.Role = Backbone.Model.extend({
	defaults: {
		name: null,
		display_name: null,
		description: null,
	},
	validation: {
		name: {
			required: true,
			fn: 'alphadash'
		},
		display_name: {
			required: true				
		},
		description: {
			required: true
		}
	},
	alphadash: function(value, attr, computedState) {
		let msg = '';			
		
		if(value.search(/^[a-zA-Z0-9-_]+$/) == -1) {
			msg = 'Name can only contain letter, numbers and 1 hypen.'
		} else if(value.match(/-/g) == null || (value.match(/-/g) || []).length > 1) {
			msg = 'Display Name must contain 1 hypen between words. e.g products-create'
		}

		console.log(msg);

		return msg;
	},
	hyphen: function(value, attr, computedState) {
		console.log('hypen');
	},
});

return App;

});`

kirkdm avatar May 31 '18 02:05 kirkdm