Socket
Socket
Sign inDemoInstall

symfony-collection-js

Package Overview
Dependencies
1
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    symfony-collection-js

A jquery plugin to dynamically create elements of a symfony form collection.


Version published
Maintainers
1
Created

Readme

Source

symfonyCollectionJs

A jquery plugin 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.

install with yarn

yarn add symfony-collection-js

or install with npm

npm install symfony-collection-js

basic usage

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();

options

var defaults =  {
        max_elems:              100,
        call_post_add_on_init:  false,
        post_add:               function($new_elem, is_init) { return true; },
        post_delete:            function($delete_elem) { 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_alias:      '__AttrName__',
        prototype_name:         '__name__'
    };

In post_up and post_down, $switched_elem is the moved elem that the user didn't click on. prototype_name_alias is used in internal to maintain indexes, you most likely won't need ever to change this parameter. 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.

Other functionalities

Add an empty element:

$('.collection').formCollection('add');

Remove an element with its index (starting from 0):

$('.collection').formCollection('remove', 2);

Clear every element:

$('.collection').formCollection('clear');

Requirement

Symfony (2 or more) Jquery (1.12.4 or more should be fine, unclear)

Note

Currently, the indexes are maintained only in the attributes (id, name, for); since it is the common case. If you want to do more, you should use the callbacks. I'm not that much a fan of how I maintained the indexes after a removal / add in middle / move. If someone has a better / simpler way, I'm interested (I didn't want to make the user add data in the form or something like that).

For advances features, you should use the plugin of ninsuo.

FAQs

Last updated on 31 Aug 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc