formFor should support attributes that contain collections of items. Validation should be possible on the collection as a whole as well as on individual items.
ngForm supports collections with the following syntax:
<form>
<label>My name</label>
<input type="text" ng-model="formData.name">
<label>My things</label>
<ul>
<li ng-repeat="thing in formData.things">
<!-- Items are wrapped with inner ngForms -->
<ng-form name="thingForm">
<input type="text" ng-model="thing.description">
</ng-form>
</li>
</ul>
</form>
Approach A: Nested formFor directives
formFor HTML might look like this for the case of collections:
<form form-for="formData" validation-rules="validationRules">
<text-field label="name" attribute="name"></text-field>
<label>My things</label>
<!-- Wrap collections with a formForCollection -->
<ul>
<li ng-repeat="thing in formData.things"
form-for="things" validation-rules="thingValidationRules">
<text-field attribute="description"></text-field>
</li>
</ul>
</form>
Validation rules for top-level object may look like this:
$scope.validationRules = {
name: {
required: true
},
things: {
// Signifies that 'things' is a validatable collection
collection: {
min: 1, // Require at least 1 'thing'
max: 4, // Allow no more than 4 'things'
}
}
};
And validation rules for individual items within the collection might then look something like this:
$scope.thingValidationRules = {
description: {
required: true,
minlength: 10
}
};
Approach B: Array brackets within attribute names
formFor HTML might look like this for the case of collections:
<form form-for="formData" validation-rules="validationRules">
<text-field label="name" attribute="name"></text-field>
<label>My things</label>
<ul>
<li ng-repeat="thing in formData.things">
<!-- The bracket syntax notifies formFor that 'things' is a collection -->
<text-field attribute="things[].description"></text-field>
</li>
</ul>
</form>
Validation rules would then look like:
$scope.validationRules = {
name: {
required: true
},
things: {
// The 'collections' attribute signifies that 'things' is a validatable collection
// Collections support custom validation rules
collection: {
min: 1, // Require at least 1 'thing'
max: 4, // Allow no more than 4 'things'
// Validation rules for individual 'thing' objects go inside of 'fields'
// All formFor validation types are supported here
fields: {
description: {
required: true,
minlength: 10
}
}
}
}
};