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

[4.x]: Address validation on SCENARIO_LIVE returns error's about fields that are filled in #11162

Closed
WHITE-developer opened this issue May 9, 2022 · 2 comments

Comments

@WHITE-developer
Copy link

What happened?

Description

When trying to save an address through a custom module and set the scenario to Live, the validation returns errors that the fullName, organization, organizationTaxId en administrativeArea cannot be blank while they are filled in and the selected country doesn't have an administrativeArea.

Custom code:

$address = new Address();
$address->setScenario(Element::SCENARIO_LIVE);

$this->populateNameAttributes($address);

$safeAttributes = [];
foreach ($address->safeAttributes() as $name) {
    $value = $this->request->getBodyParam($name);
    if ($value !== null) {
        $safeAttributes[$name] = $value;
    }
}
$address->setAttributes($safeAttributes);

$fieldsLocation = $this->request->getParam('fieldsLocation', 'fields');
$address->setFieldValuesFromRequest($fieldsLocation);

$successAddress = $address->validate(null, false);

Data of address element before calling validate()

{
  "countryCode": "NL",
  "locality": "Valkenswaard",
  "postalCode": "5555 XA",
  "addressLine1": "Parallelweg Oost 23",
  "organization": "WHITE Digital Agency",
  "organizationTaxId": "5555",
  "isProvisionalDraft": false,
  "enabled": true,
  "archived": false,
  "siteId": 1,
  "title": "Billing address",
  "trashed": false,
  "awaitingFieldValues": false,
  "propagating": false,
  "propagateAll": false,
  "newSiteIds": [],
  "isNewForSite": false,
  "resaving": false,
  "firstSave": false,
  "mergingCanonicalChanges": false,
  "updatingFromDerivative": false,
  "previewing": false,
  "hardDelete": false,
  "firstName": "Lukas",
  "lastName": "Jansen"
}

Expected behavior

Validate method returns true if the fields are filled in or not necessary

Actual behavior

Validate method returns errors on fields that are filled in

Craft CMS version

4.0.1

PHP version

8.0.8

Operating system and version

Linux 5.13.0-40-generic

Database type and version

MySQL 8.0.26

Image driver and version

Imagick 3.5.1 (ImageMagick 6.9.10-23)

Installed plugins and versions

No response

brandonkelly added a commit that referenced this issue May 10, 2022
@brandonkelly
Copy link
Member

Thanks for reporting that! This was happening because the validation rules were getting defined when you first called safeAttributes(), based on the default countryCode (us), and there’s no way to tell the model to refresh its validation rules once they’ve been defined.

Fixed for the next release by moving the county code (and field layout)-based required validation over to afterValidate().

@brandonkelly
Copy link
Member

Craft 4.0.2 is out now with the fix for this.

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