A jQuery plugin for the management of HTML(5) forms, with the allow_add option enabled, generated by Symfony 2.
Use the demo to get a first impression of the behaviour.
Include script after the jQuery library
<script src="/path/to/jquery.sfprototypeman.js"></script>
This project uses Grunt. If you build it, a minified version for production usage will be created at
build/jquery.sfprototypeman.min.js
Run
jQuery().sfPrototypeMan(); to extend Symfony generated forms with collections that ("allow_add" option) offer the [ability to add unknown/unlimited number of sub-forms/fields of a certain type](https://symfony.com/doc/current/form/form_collections.html#allowing-new-tags-with-the-prototype).
By default all form parts detected to be “repeatable” as per the Symfony way of doing that, will be extended with the ability to add and remove them.
Settings can be passed to sfPrototypeMan to override the defaultOptions, e.g.
jQuery().sfPrototypeMan({
addButtonText: "Give me one more field",
rmButtonText: "-"
});
You can attach listeners to sfPrototypeMan that will respond to events emitted by third-party code on the nodes handled by sfPrototypeMan, that is the elements with the prototype data attribute.
By default, sfPrototypeMan integrates with jQuery UI sortable by listening to the sortupdate event and responding respectively.
You can attach additional listeners, and thus make it integrate with third-party code in a clean fashion, like so
jQuery().sfPrototypeMan({
containerListeners: {
myEventName: function(event) {
// your code reacting to myEventName
// "this" in here will be bound to be the SfPrototypeContainer object
}
}
}); In this example, the event *myEventName* call on elements monitored by sfPrototypeMan will result in the anonymous callback function be called.
sfPrototypeMan emits the events prototype.elementremoved and prototype.added in the given situation.