I want to use angular to implement resuable form snippets, like
angular.module('xx').directive('snippetA', function () {
return {
scope: {
form: '='
},
link: function (scope) {
scope.form.validator.customValidator = function (value) {
// return true or false;
};
},
template: '<input ng-model="form.data.userName" custom-validator="form.validator.customValidator(form.data.userName)" name="userName">' +
'<input ng-model="form.data.userId" name="userId" required>'
}
});
I use isolate scope to prevent scope polluting. There may be some customValidator functions.
It's just an example, the real world project is much more complex than this.
Some properties can't just be shown by a form with some inputs, so i add a data property to the form object.
<form angular-validator name="formName" angular-validator-submit="form.submit()" class="form form-horizontal">
<snippet-a form="form"></snippet-a>
// more inputs or snippets
<button type="submit">submit</button>
</form>
$scope.form = {
validator: {},
data: {},
submit: function () {
// submit this.data
}
}
Currently I use angular-validation to do form validation. It works 'OK'. But the scope of it is the scope where the 'angular-validator' exists. So I must name it with a unchangeable name, for example 'form'!
the structure is
$scope.form = {
validator: {},
data: {},
submit: function ...
}
when my page contains just one form, It works fine. But when my page contains more than one form, the name of them will conflict.
What is the best practice of validating form snippets?