-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix loading new item list after adding new created element on sortable collections #8202
base: 4.x
Are you sure you want to change the base?
Changes from all commits
c7a0016
fc76bd0
5a53c89
8da174b
0ce9927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
|
||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormTypeInterface; | ||
use Symfony\Component\Form\FormView; | ||
|
@@ -27,6 +30,25 @@ | |
*/ | ||
final class ChoiceTypeExtension extends AbstractTypeExtension | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
if ($options['multiple'] && (true === ($options['sortable'] ?? false))) { | ||
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) use ($options) { | ||
/** @var PreSubmitEvent $event */ | ||
$form = $event->getForm(); | ||
Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PsalmUndefinedDocblockClass
Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PsalmUndefinedDocblockClass
Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PHPStan
Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PHPStan
Check failure on line 38 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PHPStan
|
||
$data = $event->getData(); | ||
Check failure on line 39 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
|
||
if (!is_array($data) || count($data) !== 1) { | ||
return; | ||
} | ||
|
||
if (str_contains($data[0], ',')) { | ||
$event->setData(explode(',', $data[0])); | ||
Check failure on line 46 in src/Form/Extension/ChoiceTypeExtension.php GitHub Actions / PsalmUndefinedDocblockClass
|
||
} | ||
}, 1); | ||
} | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$optionalOptions = ['sortable']; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,17 +329,24 @@ This code manages the many-to-[one|many] association field popup | |
data: {_xml_http_request: true }, | ||
dataType: 'html', | ||
type: 'POST', | ||
success: function(html) { | ||
success: function(html) { | ||
jQuery('#field_container_{{ id }}').replaceWith(html); | ||
var newElement = jQuery('#{{ id }} [value="' + data.objectId + '"]'); | ||
if (newElement.is("input")) { | ||
newElement.attr('checked', 'checked'); | ||
|
||
if (newElement.length) { | ||
if (newElement.is("input")) { | ||
newElement.attr('checked', 'checked'); | ||
} else { | ||
newElement.attr('selected', 'selected'); | ||
} | ||
} else { | ||
newElement.attr('selected', 'selected'); | ||
var selections = jQuery('#{{ id }}').val().split(','); | ||
selections.push(data.objectId); | ||
jQuery('#{{ id }}').val(selections.filter((val) => val.length > 0).join(',')); | ||
Comment on lines
+343
to
+345
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If entities use string as id, the usage of |
||
} | ||
|
||
jQuery('#field_container_{{ id }}').trigger('sonata-admin-append-form-element'); | ||
} | ||
} | ||
}); | ||
|
||
{% endif %} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it's work for your usage (with the javascript),
I assume it will introduce a bug if someone use this Extension/FormType in another situation for texte with string like
It will become
['Foo', 'bar']
instead of staying the same...I dunno if there is a way to avoid such thing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the correct approach here would be to have multiple hidden inputs, one for each id. Like the ModelAutocompleteType does it.
This would probably be more work, tho.
Another approach would be adding a special prefix to the value, that then can be checked.