Plone 6 Custom Form - Why is cancel button triggering validation to prevent handler from being called?

Hello, I made a custom form based on Plone 6 documentation.

Edit: Corrected link.

configure.zcml

<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:plone="http://namespaces.plone.org/plone"
    xmlns:browser="http://namespaces.zope.org/browser"
    i18n_domain="my.sandbox">

    <browser:page 
              name="my-form"
              for="*"
              class=".my_form.MyForm"
              permission="cmf.ManagePortal"
              />
</configure>

In my_form.py:

from plone import api
from plone import schema
from plone.autoform.form import AutoExtensibleForm
from z3c.form import button, form
from zope.interface import Interface


class IMyForm(Interface):
    
    name = schema.TextLine(title = "Your Name"
                           )

class MyForm(AutoExtensibleForm, form.EditForm):
    schema = IMyForm
    ignoreContext = True

    label="What's your name?"
    description = "Simpe, sample form"

    @button.buttonAndHandler("Ok")
    def handleApply(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        changes = self.applyChanges(data)
        if changes:
            self.status = "Settings changed"

    @button.buttonAndHandler("Cancel")
    def handleCancel(self, action):
        print("TEST")
        z = 0
        y = 4 / z
        self.request.response.redirect(api.portal.get().absolute_url())

The only real differences are I don't have the layer parameter for where I have the browser:page in configure.zcml and I have code to redirect to the front page in cancel.

When I click cancel, instead of redirecting to the front page, the name field gets an error message "Please fill out this field" and the cancel handler function isn't actually called. I tried a print statement and that didn't print in the terminal. I did try dividing by zero to trigger an error, but that was ignored.

Is it because I don't have the layer parameter in configure.zcml?
The cancel button is working in other forms. I went to add a Folder and clicked 'Cancel' and that worked as it should.

Hi, I don't know what the reason is; however, i can tell you this is a known bug and happens on all forms - even when creating standard content like "Page".

1 Like

the reason "pat-validation" is enabled. I think there is an option to disable "pat-validation" on forms.

1 Like

This has been reported and fixed here Cancel Adding page without title, not "possible" anymore (Classic) · Issue #3819 · plone/Products.CMFPlone · GitHub

Since all buttons are submit buttons (also cancel) you have to make sure you add the formnovalidate property to the cancel action. See p.a.z3cform how its done there:

Plone 6.0.x:

Plone 6.1.x:

2 Likes

Thanks guys!

Adding updateActions to the form worked