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

Fix PHP Deprecated: Creation of dynamic property $zfBreakChainOnFailure from Zend_Form_Element #440

Conversation

hung202028
Copy link

To reproduce the issue, I wrote a custom validator that implements Zend_Validate_Interface:

class Validator_JSON implements Zend_Validate_Interface
{
    private string $message;

    public function isValid($value): bool
    {
        json_decode($value);
        if (json_last_error() !== JSON_ERROR_NONE) {
            $this->message = "Invalid JSON: " . json_last_error_msg();
            return false;
        }

        return true;
    }

    public function getMessages(): array
    {
        return [$this->message];
    }
}

I then attached the custom validator to an element:

<?php
$element = new Zend_Form_Element_Textarea('textarea');
$element->addValidator(new Validator_JSON());
$element->isValid('hello')

Error encountered:
PHP Deprecated: Creation of dynamic property Validator_JSON::$zfBreakChainOnFailure is deprecated in Zend/Form/Element.php on line 1229

@develart-projects
Copy link
Collaborator

Not sure if related, but probably:

Run bin/parallel-lint --exclude vendor --exclude tests/Zend/Loader/_files/ParseError.php . --checkstyle | cs2pr
Error: Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting function (T_FUNCTION) or const (T_CONST) in ./library/Zend/Form/Element.php on line 202
Error: Process completed with exit code 1.

library/Zend/Form/Element.php Outdated Show resolved Hide resolved
@hung202028 hung202028 requested a review from IMSoP October 9, 2024 04:53
@develart-projects
Copy link
Collaborator

There were 8 failures:

  1. Zend_Form_ElementTest::testIsValidInsertsNotEmptyValidatorWhenElementIsRequiredByDefault
    Failed asserting that null is true.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:328
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testBreakChainOnFailureFlagsForExistingValidatorsRemainSetWhenNotEmptyValidatorAutoInserted
    Failed asserting that null is true.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:347
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testCanAddSingleValidatorAsString
    Failed asserting that null is false.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:705
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testCanAddSingleValidatorAsValidatorObject
    Failed asserting that null is false.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:725
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testCanRetrieveSingleValidatorRegisteredAsValidatorObjectUsingShortName
    Failed asserting that null is false.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:753
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testSetOptionsSetsArrayOfArrayValidators
    Failed asserting that null is true.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:1739
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testSetOptionsSetsArrayOfAssociativeArrayValidators
    Failed asserting that null is true.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:1765
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

  1. Zend_Form_ElementTest::testValidatorByUsingStringNotation
    Failed asserting that null is true.

/home/runner/work/zf1-future/zf1-future/tests/Zend/Form/ElementTest.php:2228
/home/runner/work/zf1-future/zf1-future/tests/resources/Runner.php:20
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:143
/home/runner/work/zf1-future/zf1-future/tests/Zend/AllTests.php:267

@develart-projects develart-projects added this to the 1.24.1 milestone Oct 14, 2024
@develart-projects
Copy link
Collaborator

Tests are now OK, this one will be probably the last one, before the next release.

I looked into this one. It$s fixing the problem described, and on the top of that, it's changing the breakOnFailure chain behavior. Could you please a bit more describe why is this change necessary? Thanks in advance.

@hung202028
Copy link
Author

Hi. @develart-projects ,
Thank you for reviewing the PR and for your feedback!

I believe the fix is necessary for the following reasons:

  1. Elimination of Deprecation Warning: The dynamic addition of properties, which is deprecated in PHP 8.2, and might result in a fatal error in PHP 9 onwards. In this case, the deprecation warning arises when the property $zfBreakChainOnFailure is dynamically added to a validator instance, as seen in these lines of code:
  1. Preservation of "Break Chain on Failure": The fix maintains the "break chain on failure" feature for both built-in and custom validators, including those implementing Zend_Validate_Interface. It does so by using an associative array to map each validator to its breakOnFailure setting, ensuring that the validation chain behavior remains intact while avoiding deprecation warnings.

@develart-projects develart-projects added bug Something isn't working to be released PR exists or in master, but not released yet labels Oct 15, 2024
@develart-projects develart-projects merged commit 1b174a2 into Shardj:master Oct 15, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working to be released PR exists or in master, but not released yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants