Skip to content

Commit

Permalink
Fix for removable-only collections
Browse files Browse the repository at this point in the history
  • Loading branch information
chives committed Jan 4, 2018
1 parent 9196ca2 commit 2b27e50
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
language: php

sudo: false
sudo: required

cache:
directories:
- $HOME/.composer/cache
- vendor

addons:
firefox: "47.0.1"

matrix:
include:
- php: 5.4
Expand All @@ -29,7 +32,7 @@ before_install:
- composer self-update
- if [[ $SYMFONY_VERSION ]]; then composer require symfony/symfony:${SYMFONY_VERSION} -n --no-update; fi;
- composer update $COMPOSER_FLAGS
- "[ ! -f bin/selenium.jar ] && wget wget -O bin/selenium.jar http://selenium-release.storage.googleapis.com/2.50/selenium-server-standalone-2.50.1.jar || true"
- "[ ! -f bin/selenium.jar ] && wget wget -O bin/selenium.jar http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar || true"

before_script:
- nohup php -S localhost:8080 -t features/fixtures/project/web > /dev/null 2>&1 &
Expand Down
27 changes: 27 additions & 0 deletions Behat/Context/FormContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function iFillFormFields(TableNode $table)
/**
* @Transform /"([^"]*)" non-editable collection/
* @Transform /non-editable collection "([^"]*)"/
* @Transform /removable-only collection "([^"]*)"/
*/
public function transformToNoneditableCollection($collectionNames)
{
Expand All @@ -96,6 +97,7 @@ public function transformToCollection($collectionNames)
* @Then /^("[^"]*" collection) should have (\d+) (element|elements)$/
* @Given /^(non-editable collection "[^"]*") has (\d+) (element|elements)$/
* @Then /^(non-editable collection "[^"]*") should have (\d+) (element|elements)$/
* @Then /^(removable-only collection "[^"]*") should have (\d+) (element|elements)$/
*/
public function collectionShouldHaveElements(NodeElement $collection, $elementsCount)
{
Expand Down Expand Up @@ -128,6 +130,30 @@ public function allCollectionButtonsDisabled(NodeElement $collection)
}
}

/**
* @Then /^button for adding item in (removable-only collection "[^"]*") should be disabled$/
*/
public function collectionAddButtonIsDisabled(NodeElement $collection)
{
$addButtons = $collection->findAll('css', '.collection-add');
expect(count($addButtons))->notToBe(0);
foreach ($addButtons as $addButton) {
expect($addButton->hasClass('disabled'))->toBe(true);
}
}

/**
* @Then /^buttons for removing items in (removable-only collection "[^"]*") should be enabled/
*/
public function collectionRemoveButtonsAreEnabled(NodeElement $collection)
{
$addButtons = $collection->findAll('css', '.collection-remove');
expect(count($addButtons))->notToBe(0);
foreach ($addButtons as $addButton) {
expect($addButton->hasClass('disabled'))->toBe(false);
}
}

/**
* @When /^I press "([^"]*)" in (collection "[^"]*")$/
*/
Expand All @@ -149,6 +175,7 @@ public function iFillWithInCollectionAtPosition($fieldName, $fieldValue, NodeEle

/**
* @Given /^I remove (\w+) element in (collection "[^"]*")$/
* @Given /^I remove (\w+) element in (removable-only collection "[^"]*")$/
*/
public function iRemoveElementInCollection($index, NodeElement $collection)
{
Expand Down
6 changes: 3 additions & 3 deletions Resources/public/js/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define(['jquery'], function($) {
removeButtonSelector: '> div > div > div > .collection-remove',
itemsContainerSelector: '> .collection-items',
itemSelector: '.form-group',
collectionsSelector: 'div[data-prototype]'
collectionsSelector: 'div[data-prototype-name]'
}, options);

var replacePrototypeName = function(template, prototypeName, index) {
Expand Down Expand Up @@ -40,7 +40,7 @@ define(['jquery'], function($) {

this.each(function(index, element) {
var $el = $(element);
if (!$el.data('prototype') || $el.data('current-index')) {
if (!$el.data('prototype-name') || $el.data('current-index')) {
return;
}

Expand All @@ -51,7 +51,7 @@ define(['jquery'], function($) {
$el.on('click', options.removeButtonSelector, removeCollectionItem);
}

if ($el.data('allow-add')) {
if ($el.data('allow-add') && $el.data('prototype')) {
$el.find(options.addButtonSelector).on('click', addCollectionItem);
}
});
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Form/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
$(label).append(' <i class="glyphicon glyphicon-asterisk"></i>');
}
});
$context.find('div[data-prototype]')
$context.find('div[data-prototype-name]')
.formCollection()
.on('add.collection-item', function(event, item) {
initForms($(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ admin:
tags: Tags
tag_elements: Elements
non_editable_tags: Non-editable tags
removable_comments: Removable-only comments
actions: Actions
subscriber:
name: Subscribers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'mapped' => false,
'required' => false
]);
$builder->add('removableComments', 'collection', [
'type' => 'text',
'data' => new ArrayCollection(['Comment 1', 'Comment 2', 'Comment 3']),
'label' => 'admin.news.list.removable_comments',
'allow_add' => false,
'allow_delete' => true,
'mapped' => false,
'required' => false
]);
}

public function getName()
Expand Down
5 changes: 5 additions & 0 deletions features/form/collections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ Feature: Managing form collections
Given there is a "news" with "title" "News 1" present in the database
And I am on the "News edit" page with id "1"
Then non-editable collection "Non-editable tags" should have 3 elements
And removable-only collection "Removable-only comments" should have 3 elements
And all buttons for adding and removing items in non-editable collection "Non-editable tags" should be disabled
And button for adding item in removable-only collection "Removable-only comments" should be disabled
And buttons for removing items in removable-only collection "Removable-only comments" should be enabled
When I remove first element in removable-only collection "Removable-only comments"
Then removable-only collection "Removable-only comments" should have 2 elements

0 comments on commit 2b27e50

Please sign in to comment.