Skip to content

Commit

Permalink
Update demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
hueitan committed May 30, 2014
1 parent 73ff4b0 commit a58a1c4
Showing 1 changed file with 135 additions and 134 deletions.
269 changes: 135 additions & 134 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,137 @@
angular.module('myApp', ['validation'])

// -------------------
// config phase
// -------------------
.config(['$validationProvider', function ($validationProvider) {

var defaultMsg,
expression;

/**
* Setup a default message for Url
*/
defaultMsg = {
url: {
error: 'This is a error url given by user',
success: 'It\'s Url'
}
};

$validationProvider.setDefaultMsg(defaultMsg);


/**
* Setup a new Expression and default message
* In this example, we setup a IP address Expression and default Message
*/
expression = {
ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
};

defaultMsg = {
ip: {
error: 'This isn\'t ip address',
success: 'It\'s ip'
}
};

$validationProvider.setExpression(expression);
$validationProvider.setDefaultMsg(defaultMsg);

// or we can just setup directly
$validationProvider.setDefaultMsg({ ip: { error: 'This no ip', success: 'this ip'}});
$validationProvider.setExpression({ ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ });

/**
* Additions validation
*/
$validationProvider.setExpression({
(function () {
angular.module('myApp', ['validation'])

// -------------------
// config phase
// -------------------
.config(['$validationProvider', function ($validationProvider) {

var defaultMsg,
expression;

/**
* Setup a default message for Url
*/
defaultMsg = {
url: {
error: 'This is a error url given by user',
success: 'It\'s Url'
}
};

$validationProvider.setDefaultMsg(defaultMsg);


/**
* Setup a new Expression and default message
* In this example, we setup a IP address Expression and default Message
*/
expression = {
ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
};

defaultMsg = {
ip: {
error: 'This isn\'t ip address',
success: 'It\'s ip'
}
};

$validationProvider.setExpression(expression);
$validationProvider.setDefaultMsg(defaultMsg);

// or we can just setup directly
$validationProvider.setDefaultMsg({ ip: { error: 'This no ip', success: 'this ip'}});
$validationProvider.setExpression({ ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ });

/**
* @param value , user input
* @returns {boolean} true iff valid
* Additions validation
*/
huei: function (value) {
return value === 'Huei Tan';
}
});

$validationProvider.setDefaultMsg({
huei: {
error: 'This should be Huei Tan',
success: 'Thanks!'
}
});
}])

// -------------------
// controller phase
// -------------------

.controller('index', ['$scope', '$injector', function ($scope, $injector) {

// Injector
var $validationProvider = $injector.get('$validation');


// Initial Value
$scope.form = {
requiredCallback: 'required',
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form2 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form3 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form)
.success($scope.success)
.error($scope.error);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form4 = {
changeErrorMsg: 'This is the First Error Msg',
changeMsg: function () {
$scope.form4.changeErrorMsg = 'This is the Second Error Msg';
}
};

$scope.form5 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

// Callback method
$scope.success = function (message) {
alert('Success ' + message);
};

$scope.error = function (message) {
alert('Error ' + message);
};
}]);
$validationProvider.setExpression({
/**
* @param value , user input
* @returns {boolean} true iff valid
*/
huei: function (value) {
return value === 'Huei Tan';
}
});

$validationProvider.setDefaultMsg({
huei: {
error: 'This should be Huei Tan',
success: 'Thanks!'
}
});
}])

// -------------------
// controller phase
// -------------------
.controller('index', ['$scope', '$injector', function ($scope, $injector) {

// Injector
var $validationProvider = $injector.get('$validation');


// Initial Value
$scope.form = {
requiredCallback: 'required',
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form2 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form3 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form)
.success($scope.success)
.error($scope.error);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

$scope.form4 = {
changeErrorMsg: 'This is the First Error Msg',
changeMsg: function () {
$scope.form4.changeErrorMsg = 'This is the Second Error Msg';
}
};

$scope.form5 = {
checkValid: $validationProvider.checkValid,
submit: function (form) {
$validationProvider.validate(form);
},
reset: function (form) {
$validationProvider.reset(form);
}
};

// Callback method
$scope.success = function (message) {
alert('Success ' + message);
};

$scope.error = function (message) {
alert('Error ' + message);
};
}]);
}).call(this);

0 comments on commit a58a1c4

Please sign in to comment.