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 validation rule order #1417

Closed
nixondesigndev opened this issue Apr 30, 2020 · 3 comments
Closed

Address validation rule order #1417

nixondesigndev opened this issue Apr 30, 2020 · 3 comments

Comments

@nixondesigndev
Copy link
Contributor

Description

We have an order form that when validating the Address model throws an exception if the countryCode isn't an integer, stemming from the validateState rule. We believe the root cause of this is if the address fields gets autofilled by the browser or an extension.

We're using Address::EVENT_DEFINE_RULES to try and catch this but they get called after the 1st party rules.

Steps to reproduce

  1. Pass in a string to a the countryID field.
  2. Validate.

Additional info

  • Craft version: Craft Pro 3.4.9
  • PHP version: 7.3.9
  • Database driver & version: MySQL 5.7.26
  • Plugins & versions: Commerce 3.0.11
@nfourtythree nfourtythree added the ℹ️ status: need more info When waiting for user to supply database or more information. label Apr 30, 2020
@nfourtythree
Copy link
Contributor

Hi @NixonGit

I have run some test cases and I can't seem to get the error you mentioned. It works no matter if the countryId and stateId values are strings or integers.

Do you have a stack trace for this?

Thanks.

@nixondesigndev
Copy link
Contributor Author

nixondesigndev commented Apr 30, 2020

Hi @nfourtythree

TypeError: Argument 1 passed to craft\commerce\services\Countries::getCountryById() must be of the type int, string given, called in /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/models/Address.php on line 304 and defined in /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/services/Countries.php:63
Stack trace:
#0 /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/models/Address.php(304): craft\commerce\services\Countries->getCountryById('qwerty')
#1 [internal function]: craft\commerce\models\Address->validateState('stateId', NULL, Object(yii\validators\InlineValidator))
#2 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/validators/InlineValidator.php(72): call_user_func(Array, 'stateId', NULL, Object(yii\validators\InlineValidator))
#3 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/validators/Validator.php(257): yii\validators\InlineValidator->validateAttribute(Object(craft\commerce\models\Address), 'stateId')
#4 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/Model.php(367): yii\validators\Validator->validateAttributes(Object(craft\commerce\models\Address), Array)
#5 /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/elements/traits/OrderValidatorsTrait.php(82): yii\base\Model->validate()
#6 [internal function]: craft\commerce\elements\Order->validateAddress('shippingAddress', NULL, Object(yii\validators\InlineValidator))
#7 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/validators/InlineValidator.php(72): call_user_func(Array, 'shippingAddress', NULL, Object(yii\validators\InlineValidator))
#8 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/validators/Validator.php(257): yii\validators\InlineValidator->validateAttribute(Object(craft\commerce\elements\Order), 'shippingAddress')
#9 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/Model.php(367): yii\validators\Validator->validateAttributes(Object(craft\commerce\elements\Order), Array)
#10 /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/controllers/CartController.php(254): yii\base\Model->validate()
#11 /Users/studiofour/Sites/1875/vendor/craftcms/commerce/src/controllers/CartController.php(239): craft\commerce\controllers\CartController->_returnCart()
#12 [internal function]: craft\commerce\controllers\CartController->actionUpdateCart()
#13 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#14 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#15 /Users/studiofour/Sites/1875/vendor/craftcms/cms/src/web/Controller.php(178): yii\base\Controller->runAction('update-cart', Array)
#16 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('update-cart', Array)
#17 /Users/studiofour/Sites/1875/vendor/craftcms/cms/src/web/Application.php(291): yii\base\Module->runAction('commerce/cart/u...', Array)
#18 /Users/studiofour/Sites/1875/vendor/craftcms/cms/src/web/Application.php(559): craft\web\Application->runAction('commerce/cart/u...', Array)
#19 /Users/studiofour/Sites/1875/vendor/craftcms/cms/src/web/Application.php(270): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#20 /Users/studiofour/Sites/1875/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#21 /Users/studiofour/Sites/1875/public/index.php(21): yii\base\Application->run()
#22 {main}

Here's the post data

'action' => 'commerce/cart/update-cart',
    'shippingAddress' => [
        'firstName' => '',
        'lastName' => '',
        'phone' => '',
        'address1' => '',
        'address2' => '',
        'city' => '',
        'countryId' => 'qwerty',
        'zipCode' => '',
    ],

And here are our custom validation rules:

Event::on(Address::class, Address::EVENT_DEFINE_RULES, function(DefineRulesEvent $event) {
            $event->rules[] = [['firstName', 'lastName'], 'required'];
            $event->rules[] = ['address1', 'required', 'message' => 'Address cannot be blank'];
            $event->rules[] = ['zipCode', 'required', 'message' => 'Post Code cannot be blank'];
            $event->rules[] = ['countryId', 'default', 'value' => 77];
            $event->rules[] = ['countryId', 'compare', 'compareValue' => 77, 'message' => 'We only ship to the United Kingdom'];
        });

@nfourtythree
Copy link
Contributor

HI @NixonGit

Thank you for all of the info. We have pushed up a fix that should address this and bring back a validation error. It will be included in the next release.

To get this early, change your craftcms/commerce requirement in you project's composer.json to:

"require": {
  "craftcms/commerce": "dev-develop#8bc118b0a07fd82be66bfc9ca8d601be8abc4781 as 3.1.3",
  "...": "..."
}

Then run composer update.

Thanks.

@nfourtythree nfourtythree added bug 👤 customers and removed ℹ️ status: need more info When waiting for user to supply database or more information. labels May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants