Additional fields validation
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!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Additional fields validation
How I can validate additional fields in form on the server side. These fields are product attributes. Example form code:
Form code is located in ext/new-theme/client/html/templates/catalog/detail/body-standard.php
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 ?>
Re: Additional fields validation
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/
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, give us a star
If you like Aimeos, give us a star
Re: Additional fields validation
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.
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);
}
}
Re: Additional fields validation
Aimeos, will you answer ? Thanks
Re: Additional fields validation
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, give us a star
If you like Aimeos, give us a star
Re: Additional fields validation
The point is, the laravel method to redirect doesn't work. Work correctly only
Code: Select all
header('location: some www');
die;
Re: Additional fields validation
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, give us a star
If you like Aimeos, give us a star