
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
symfony-collection-js
Advanced tools
A plugin working with and without jquery to dynamically create elements of a symfony form collection.
A plugin working with and without jquery to dynamically create elements of a symfony form collection.
I created this plugin because the common Js plugin had a bug that didn't let me place the button to add an element wherever I wanted. And I wanted to be able to add dynamically elements.
yarn add symfony-collection-js
npm install symfony-collection-js
Your collection container should have the prototype mentioned in the symfony documentation, and an id or a class. So in your form type:
$builder->add('collectionProperty', CollectionType::class, [
...
'prototype' => true,
'by_reference' => false,
'attr' => array(
'class' => 'collection',
),
...
]);
And to init the collection element in javascript:
$('.collection').formCollection(); // with jQuery
formCollection(document.querySelectorAll('.collection')); // in pure javascript with a selector
formCollection(document.getElementById('myId')); // in pure javascript with an id
var defaults = {
max_elems: 100,
call_post_add_on_init: false,
post_add: function(new_elem, context) { return true; },
post_delete: function(delete_elem, context) { return true; },
post_up: function(elem, switched_elem) { return true; },
post_down: function(elem, switched_elem) { return true; },
other_btn_add: null,
btn_add_selector: '.collection-add',
btn_delete_selector: '.collection-delete',
btn_up_selector: '.collection-up',
btn_down_selector: '.collection-down'
prototype_name: '__name__'
};
In post_up and post_down, switched_elem is the moved elem that the user didn't click on. prototype_name should probably be changed if you use nested collection (in the FormType too, with the same value).
Note that the buttons are not created by the plugin but have to already exist.
Add an empty element:
$('.collection').formCollection('add'); // with jQuery
formCollection(document.querySelectorAll('.collection'), 'add'); // in pure javascript with a selector
formCollection(document.getElementById('myId'), 'add'); // in pure javascript with an id
Delete an element with its index (starting from 0):
$('.collection').formCollection('delete', 2); // with jQuery
formCollection(document.querySelectorAll('.collection'), 'delete', 2); // in pure javascript with a selector
formCollection(document.getElementById('myId'), 'delete', 2); // in pure javascript with an id
Clear every element (doesn't call post_delete):
$('.collection').formCollection('clear'); // with jQuery
formCollection(document.querySelectorAll('.collection'), 'clear'); // in pure javascript with a selector
formCollection(document.getElementById('myId'), 'clear'); // in pure javascript with an id
Refresh the attributes of every elements from the index given (starting from 0). You might need this in specific cases, such as with drag and drop:
$('.collection').formCollection('refreshAttributes', 0); // with jQuery
formCollection(document.querySelectorAll('.collection'), 'refreshAttributes', 0); // in pure javascript with a selector
formCollection(document.getElementById('myId'), 'refreshAttributes', 0); // in pure javascript with an id
The context argument in post_add and post_delete will have of this values, depending on how it's called:
formCollection.POST_ADD_CONTEXT = {
BTN_ADD: 4,
OTHER_BTN_ADD: 8,
INIT: 15,
ADD_METHOD: 16
};
formCollection.POST_DELETE_CONTEXT = {
BTN_DELETE: 23,
DELETE_METHOD: 42
};
Just open the page index_with_jquery.html or index_without_jquery.html in the test folder. It starts the tests on opening.
Yes ! in the example folder, with and without jQuery, without and without loader.
Symfony (2 or more)
If you're using this without jQuery, but with a loader (AMD, CommonJS, or anything), you MUST use a version on a branch / tag with "js-only" in the name! Otherwise it WILL NOT BUILD! (I didn't want to make several versions but tests ended showing that it was the best choice... Unless you have a better idea?)
This should theoretically work with unlimited nested collection. However if you encounter an issue, let me know. Don't hesitate to let me know if you're using this plugin, I'm super interested ! For advances features, you should use the plugin of ninsuo. The file test/prep_nojquery_tests.js contains a homemade micro version of jQuery, I guess it can interest some people.
FAQs
A plugin working with and without jquery to dynamically create elements of a symfony form collection.
The npm package symfony-collection-js receives a total of 42 weekly downloads. As such, symfony-collection-js popularity was classified as not popular.
We found that symfony-collection-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.