Skip to content
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

Address checkout step does not go through save_callbacks from DCA #2438

Closed
LupusVII opened this issue Mar 22, 2023 · 2 comments
Closed

Address checkout step does not go through save_callbacks from DCA #2438

LupusVII opened this issue Mar 22, 2023 · 2 comments
Labels
Milestone

Comments

@LupusVII
Copy link

Hi there,
First, I'm on a Contao 4.9, Isotope 2.6, PHP 7.4. I looked for this error in other issues, but did not find something similar. Also, I do not understand German so sorry if it was mentioned in a German issue :)

I put a save_callback on the postal field inside the tl_iso_address DCA (to compute with more precise rules for my customer). The AddressBook module triggers them, since it goes through the Form class from Haste package.
But for the address checkout step, it is ignored.

I did an override of the function validateFields inside vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/CheckoutStep/Address.php class, so now it works for me, but maybe I missed something, or it is something you want to put in the core.
My function replacement below.

Thanks for your time and for the work you put in Isotope :)

    protected function validateFields($blnValidate)
    {
        $arrAddress = array();
        $arrWidgets = $this->getWidgets();

        foreach ($arrWidgets as $strName => $objWidget) {

            // Validate input
            if ($blnValidate) {
                $objWidget->validate();
                $varValue = (string) $objWidget->value;

                // Convert date formats into timestamps
                if ('' !== $varValue && \in_array($objWidget->dca_config['eval']['rgxp'], array('date', 'time', 'datim'), true)) {
                    try {
                        $objDate = new \Date($varValue, $GLOBALS['TL_CONFIG'][$objWidget->dca_config['eval']['rgxp'] . 'Format']);
                        $varValue = $objDate->tstamp;
                    } catch (\OutOfBoundsException $e) {
                        $objWidget->addError(
                            sprintf(
                                $GLOBALS['TL_LANG']['ERR'][$objWidget->dca_config['eval']['rgxp']],
                                $GLOBALS['TL_CONFIG'][$objWidget->dca_config['eval']['rgxp'] . 'Format']
                            )
                        );
                    }
                }

                // Wrap in a try catch since Contao use Exception to catch backend errors
                try {
                    if (is_array($objWidget->dca_config['save_callback'] ?? null)) {
                        foreach ($objWidget->dca_config['save_callback'] as $callback) {
                            if (is_array($callback)) {
                                $varValue = \System::importStatic($callback[0])->{$callback[1]}($varValue, $dc);
                            } elseif (is_callable($callback)) {
                                $varValue = $callback($varValue, $dc);
                            }
                        }
                    }
                } catch (\Exception $e) {
                    $objWidget->addError($e->getMessage());
                }

                // Do not submit if there are errors
                if ($objWidget->hasErrors()) {
                    $this->blnError = true;
                } // Store current value
                elseif ($objWidget->submitInput()) {
                    $arrAddress[$strName] = $varValue;
                }
            } else {
                \Input::setPost($objWidget->name, $objWidget->value);

                $objValidator = clone $objWidget;
                $objValidator->validate();

                if ($objValidator->hasErrors()) {
                    $this->blnError = true;
                }
            }
        }

        return $arrAddress;
    }
@aschempp aschempp added the bug label Mar 30, 2023
@aschempp aschempp added this to the 2.9.0 milestone Mar 30, 2023
@aschempp
Copy link
Member

Thanks for your bug report and providing a solution. I think that's very valid and should be added to the next minor release. I'll look into the details as soon as I'm preparing version 2.9.

@aschempp
Copy link
Member

Implemented in 1a35519

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants