Repository: wagtail/wagtail
File: wagtail/admin/forms/pages.py (Python)

Code before:

    def clean(self):
        cleaned_data = super().clean()
        if "slug" in self.cleaned_data:
            page_slug = cleaned_data["slug"]
            if not Page._slug_is_available(page_slug, self.parent_page, self.instance):
                self.add_error(
                    "slug",
                    forms.ValidationError(
                        _(
                            "The slug '%(page_slug)s' is already in use within the parent page"
                        )
                        % {"page_slug": page_slug}
                    ),
                )

        return cleaned_data
    
    @property
    def media(self):
        media = super().media
        if self.show_comments_toggle:
            media += forms.Media(
                js=["wagtailadmin/js/comments.js"],
            )
        return media


class MoveForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.page_to_move = kwargs.pop("page_to_move")
        self.target_parent_models = kwargs.pop("target_parent_models")

        super().__init__(*args, **kwargs)

        self.fields["new_parent_page"] = forms.ModelChoiceField(
            initial=self.page_to_move.get_parent(),
            queryset=Page.objects.all(),
            widget=widgets.AdminPageMoveChooser(
                can_choose_root=True,
                user_perms="move_to",
                target_models=self.target_parent_models,
                pages_to_move=[self.page_to_move.pk],
            ),
            label=_("New parent page"),
            help_text=_("Select a new parent for this page."),
        )


class ParentChooserForm(forms.Form):
    def __init__(self, child_page_type, user, *args, **kwargs):

Review comment (suggestion):
@the-r3aper7 let's add a unit test to ensure this logic works and the `comments.js` is included or not.

I would recommend adding something to `wagtail/admin/tests/test_edit_handlers.py` in the class `TestCommentPanel`.

See `test_comments_toggle_enabled` and maybe add the assertion for the js existing there and the opposite in `test_comments_disabled_setting`.

For examples of how we test for the presence of a script inclusion, see the file `wagtail/admin/tests/test_workflows.py` and look for `wagtailadmin/js/workflow-action.js`. Observe that we use the correctly formed full static asset URL for the assertion.

```py
        # check that the workflow-action script is present with the correct data-activate attribute
        workflow_action_js = versioned_static("wagtailadmin/js/workflow-action.js")
        scripts = soup.select(f"script[src='{workflow_action_js}']")
```



Code after:

    def clean(self):
        cleaned_data = super().clean()
        if "slug" in self.cleaned_data:
            page_slug = cleaned_data["slug"]
            if not Page._slug_is_available(page_slug, self.parent_page, self.instance):
                self.add_error(
                    "slug",
                    forms.ValidationError(
                        _(
                            "The slug '%(page_slug)s' is already in use within the parent page"
                        )
                        % {"page_slug": page_slug}
                    ),
                )

        return cleaned_data

    @property
    def media(self):
        media = super().media
        if self.show_comments_toggle:
            media += forms.Media(
                js=["wagtailadmin/js/comments.js"],
            )
        return media


class MoveForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.page_to_move = kwargs.pop("page_to_move")
        self.target_parent_models = kwargs.pop("target_parent_models")

        super().__init__(*args, **kwargs)

        self.fields["new_parent_page"] = forms.ModelChoiceField(
            initial=self.page_to_move.get_parent(),
            queryset=Page.objects.all(),
            widget=widgets.AdminPageMoveChooser(
                can_choose_root=True,
                user_perms="move_to",
                target_models=self.target_parent_models,
                pages_to_move=[self.page_to_move.pk],
            ),
            label=_("New parent page"),
            help_text=_("Select a new parent for this page."),
        )


class ParentChooserForm(forms.Form):
    def __init__(self, child_page_type, user, *args, **kwargs):