-
Notifications
You must be signed in to change notification settings - Fork 170
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
Not getting custom field errors on request to update-cart #1292
Comments
Hey @samhibberd We have just added the ability to pass This will give you errors (if applicable). The update will be included in the next release. To get this early, change your "require": {
"craftcms/commerce": "dev-develop#bcf386211cede1f3e042433dad62c6349400b1f8 as 3.0.11",
"...": "..."
} Then run The following is the information that will be going in the docs with this feature. Validating custom fieldsBy default, when updating the cart, custom fields are not validated. This is to avoid showing the user errors for unrelated fields when taking an action such as adding to cart. If you want to allow custom fields to be validated on an update cart request, you can do so by adding the following field to the form: <input type="hidden" name="validateCustomFields" value="1"> |
Thanks for getting this working. After a talking through with @nfourtythree, I raised if it would potentially be better to determine if the |
Hi @nfourtythree assuming this falls under the this issue, but addresses (with custom rules defined) are also not validating when saving via Would there be an equivalent Preference would still be validate custom fields if any provided via |
Hi @samhibberd I have just tested with the following custom validation and I was able to retrieve the error posting to Event::on(Address::class, Address::EVENT_DEFINE_RULES, function($event) {
if (!empty($event->rules)) {
$event->rules[] = [['firstName'], function($attribute, $params, Validator $validator) use ($event) {
if ($event->sender->firstName && $event->sender->firstName != 'Testing') {
$validator->addError($event->sender, $attribute, Craft::t('site', 'First name is not "Testing".'));
}
}];
}
}); |
My bad, I was using a custom controller which was pre-filing the firstName and lastName, values, adding some additional rules in the defineRules handler worked a dream: public static function defineRules(DefineRulesEvent $e)
{
$e->rules[] = [[ 'firstName', 'lastName', 'address1', 'city', 'zipCode', 'countryId' ], 'required'];
} Regards custom fields, is this being considered:
|
At the moment we are sticking with the extra It is definitely a valid point about someone being able to disable it. But as a developer, you will undoubtedly have checks to stop the customer from progressing any further if you do not have the data you require based on the stage they are at in the checkout. In a similar way, you wouldn't allow show the customer the payment form if they didn't have any line items. |
@samhibberd We has to reverse the This defaults to false, but if set to true, it will validate any custom field that is submitted in a cart update request. It will not validate any custom field when set to false (default). It will not validate a custom field that is not submitted when set to true. |
In 3.0.12 |
Brilliant, thanks team, exactly what we were hoping for. 🎉 |
Calling the update-cart controller via Ajax, does not return any field errors. Includes both matrix and standard custom fields, standard fields look to save ok (if valid), not got matrix fields playing ball yet, so must be failing validation somewhere. Validation when saving in the cp is working.
Update 24/02/20:
Craft 3.4.6.1
Commerce 3.0.10
The text was updated successfully, but these errors were encountered: