Skip to content

Commit

Permalink
Fixed #11162
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed May 10, 2022
1 parent 6dffbc5 commit cf8d4b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Fixed a bug where Assets fields’ “Upload files” buttons weren’t wrapping when there wasn’t enough space to show them alongside “Add an asset”. ([#11133](https://github.com/craftcms/cms/issues/11133))
- Fixed an error that occurred when searching for elements by a custom field. ([#11120](https://github.com/craftcms/cms/pull/11120))
- Fixed a bug where `Craft.getUrl()` was appending empty query strings to URLs when they weren’t needed. ([#11159](https://github.com/craftcms/cms/issues/11159))
- Fixed a bug where addresses could validate the wrong set of required fields, if the validation rules were invoked before the country code was set. ([#11162](https://github.com/craftcms/cms/issues/11162))

## 4.0.1 - 2022-05-06

Expand Down
34 changes: 20 additions & 14 deletions src/elements/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,25 @@ public function beforeValidate(): bool
public function defineRules(): array
{
$rules = parent::defineRules();

$rules[] = [['ownerId'], 'number'];
$rules[] = [['countryCode'], 'required'];
$rules[] = [['longitude', 'latitude'], 'safe'];
$rules[] = [self::_addressAttributes(), 'safe'];
return $rules;
}

$formatter = Craft::$app->getAddresses()->getAddressFormatRepository()->get($this->countryCode);
$requiredAddressFields = array_filter(
$formatter->getRequiredFields(),
fn(string $attribute) => !in_array($attribute, ['givenName', 'familyName', 'additionalName']),
);
$rules[] = [$requiredAddressFields, 'required', 'on' => self::SCENARIO_LIVE];

/**
* @inheritdoc
*/
public function afterValidate(): void
{
if ($this->getScenario() === self::SCENARIO_LIVE) {
$formatter = Craft::$app->getAddresses()->getAddressFormatRepository()->get($this->countryCode);
$requiredAttributes = array_filter(
$formatter->getRequiredFields(),
fn(string $attribute) => !in_array($attribute, ['givenName', 'familyName', 'additionalName']),
);

$requirableNativeFields = [
OrganizationField::class,
OrganizationTaxIdField::class,
Expand All @@ -512,18 +517,19 @@ public function defineRules(): array
/** @var BaseNativeField|null $field */
$field = $fieldLayout->getFirstVisibleElementByType($class, $this);
if ($field && $field->required) {
$attributes = match ($field->attribute()) {
array_push($requiredAttributes, ...match ($field->attribute()) {
'latLong' => ['latitude', 'longitude'],
default => [$field->attribute()],
};
foreach ($attributes as $attribute) {
(new RequiredValidator())->validateAttribute($this, $attribute);
}
});
}
}

foreach ($requiredAttributes as $attribute) {
(new RequiredValidator())->validateAttribute($this, $attribute);
}
}

return $rules;
parent::afterValidate();
}

/**
Expand Down

0 comments on commit cf8d4b5

Please sign in to comment.