I've got similar situation and for now I'm trying to resolve it. My first thoughts was to create a few separate elements:
- service, which will be responsible for any logic in the form
- one directive for whole form
- separate directives for any form element may occurs (input, select, checkbox, radiobuttons and so on)
I'd like to communicate between all of these elements via $rootScope: main service receives JSON data, for example:
{
formName: "sample form",
formMethod: "POST",
formAction : url,
fields {
0 :{
type: "input",
value: username.value,
error: username.error,
validator: username.validator
},
1: {
... next field definition as above...
}
submit: "Save",
submitCallback: save,
... other properties for whole form...
}
Then, my service creates each form field depends on items in JSON 'fields' property (field 'type' determines directive for single field, which should be used). Root elements in JSON determines global properties for whole form element (which is another directive).
Every callbacks and other properties I define in controller, then I only have to call component such like:
FormService.init( >JSON< );
In FormService init() method iterating via JSON fields "build" form.
In view, it should looks something like this:
<form-service>
... here is the placeholder for inner directives ...
</form-service>
This is just the beginning of my work, so I'll be happy to discuss about details :)
Greetings,
Rafal