Additional fields validation

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
krzysiekp
Posts: 85
Joined: 05 Nov 2021, 16:19

Additional fields validation

Post by krzysiekp » 05 Nov 2021, 16:37

How I can validate additional fields in form on the server side. These fields are product attributes. Example form code:

Code: Select all

 <div id="as-gift" class="d-none">
                    <div class="row py-2">
                        <div class="col-6 form-group">
                            <small id="emailHelp " class="form-text text-muted">Od kogo *</small>
                            <input placeholder=" " name="b_prod[0][attrcustid][33]" type="text" class="form-control gift-field" data-required="1">
                        </div>
                    </div>
                    <div class="row py-2">
                        <div class=" col-6">
                            <small id="emailHelp" class="form-text text-muted">Dla kogo *</small>
                            <input placeholder=" " name="b_prod[0][attrcustid][34]" type="text" class="form-control gift-field" data-required="1">
                        </div>
                    </div>
                    <div class="row py-2">
                        <div class="form-group col-6">
                            <small id="emailHelp" class="form-text text-muted">Adres e-mail odbiorcy *</small>
                            <input placeholder=" " name="b_prod[0][attrcustid][43]" type="email" class="form-control gift-field" data-pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" data-required="1">
                            <div class="col-12 p-0 mt-1 form-error-info bad-format" style="display: none">Zły format</div>
                        </div>
                    </div>
                    <div class="py-3" style="width: 100%">
                        <small id="emailHelp" class="form-text text-muted">Treść wiadomości</small>
                        <textarea
                            style="height: 157px;"
                            name="b_prod[0][attrcustid][35]" class="form-control gift-field"
                            rows="3"></textarea>
                        <small id="emailHelp" class="w44 form-text text-muted">Pozostała liczba znaków
                            150</small>
                    </div>
                </div>
                <input value="<?= $layoutId ?>" name="b_prod[0][attrcustid][41]" type="hidden" class="form-control">
                <?php if ($sessionCardType == 'p') : ?>
                    <input value="<?= $boxId ?>" name="b_prod[0][attrcustid][42]" type="hidden" class="form-control">
                <?php endif ?>
Form code is located in ext/new-theme/client/html/templates/catalog/detail/body-standard.php

User avatar
aimeos
Administrator
Posts: 8356
Joined: 01 Jan 1970, 00:00

Re: Additional fields validation

Post by aimeos » 06 Nov 2021, 14:55

You can write a decorator for the basket/standard component which checks in its process() method if the posted values are valid and which adds an error message to the view in case of an error.

Here you can find the documentation for implementing decorators for HTML client classes:
https://aimeos.org/docs/latest/frontend ... omponents/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

krzysiekp
Posts: 85
Joined: 05 Nov 2021, 16:19

Re: Additional fields validation

Post by krzysiekp » 08 Nov 2021, 10:00

Hi Aimeos!

How to return back when error ? My code below. This code takes you to the next page (basket/standard) despite the error thrown. How to redirect to previous page with error list etc. ? redirect() method not working.

Code: Select all

public function process()
    {
        $view = $this->getView();
        $context = $this->getContext();

        try {
            if ($view->param('b_action') == 'add') {

                $prods = (array)$view->param('b_prod', []);
                foreach ($prods as $p) {
                    if (($errors = $this->checkFields($p)) !== []) {
                        throw new \Aimeos\Client\Html\Exception(sprintf('At least one billing address part is missing or invalid'));
                    }
                }
            }
            $this->getClient()->process();
        }
        catch (\Aimeos\Client\Html\Exception $e) {
            $error = array($context->getI18n()->dt('client', $e->getMessage()));
            //dd($error);
            $view->standardErrorList = array_merge($view->get('standardErrorList', []), $error);
        } catch (\Aimeos\Controller\Frontend\Exception $e) {
            $error = array($context->getI18n()->dt('controller/frontend', $e->getMessage()));
            $view->standardErrorList = array_merge($view->get('standardErrorList', []), $error);
        } catch (\Aimeos\MShop\Exception $e) {
            $error = array($context->getI18n()->dt('mshop', $e->getMessage()));
            $view->standardErrorList = array_merge($view->get('standardErrorList', []), $error);
        } catch (\Exception $e) {
            $error = array($context->getI18n()->dt('client', 'A non-recoverable error occured'));
            $view->standardErrorList = array_merge($view->get('standardErrorList', []), $error);
            $this->logException($e);
        }
    }

krzysiekp
Posts: 85
Joined: 05 Nov 2021, 16:19

Re: Additional fields validation

Post by krzysiekp » 19 Nov 2021, 10:38

Aimeos, will you answer ? Thanks

User avatar
aimeos
Administrator
Posts: 8356
Joined: 01 Jan 1970, 00:00

Re: Additional fields validation

Post by aimeos » 20 Nov 2021, 10:04

The error is thrown by the basket/standard component which is the only one that can check the form data for errors. The catalog/detail component is never executed because the form action URL is those of the basket and if you automatically redirect to the previous page, the user won't see the error message at all.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

krzysiekp
Posts: 85
Joined: 05 Nov 2021, 16:19

Re: Additional fields validation

Post by krzysiekp » 20 Nov 2021, 17:59

The point is, the laravel method to redirect doesn't work. Work correctly only

Code: Select all

header('location: some www');
die;

User avatar
aimeos
Administrator
Posts: 8356
Joined: 01 Jan 1970, 00:00

Re: Additional fields validation

Post by aimeos » 21 Nov 2021, 14:35

Laravels redirect() doesn't work because it returns a redirect object that must be returned by a Laravel controller action.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply