magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.51k stars 9.31k forks source link

[Issue] Fix type when validating field using attribute rules #38774

Open m2-assistant[bot] opened 4 months ago

m2-assistant[bot] commented 4 months ago

This issue is automatically created based on existing pull request: magento/magento2#38773: Fix type when validating field using attribute rules


Description (*)

If attribute based rules are used to specify validation of a field in HTML, incorrect type is used when validating. For example, validation rule is ipv4 and has assigned a value of true and is processed as String, but should be Boolean instead.

<input name="field" ipv4="true" ... />

Related Pull Requests

N/A

Fixed Issues (if relevant)

N/A

Manual testing scenarios (*)

  1. create new form (for example on Homepage, custom-form.phtml)
  2. paste the following code:
    <form class="form" action="#" id="custom-form" method="post">
    <fieldset class="fieldset">
    <div class="field required">
            <label class="label" for="custom-field"><span>Attribute Rule</span></label>
            <div class="control">
                <input name="custom-field" id="custom-field" title="Attribute Rule" value="ipv4" ipv4="true" class="input-text" type="text" />
            </div>
        </div>
    </fieldset>
    </form>
    <script type="text/x-magento-init">
    {
        "*": {
            "Magento_Customer/js/block-submit-on-send": {
                "formId": "custom-form"
            }
        }
    }
    </script>
  3. submit the form, error will be displayed on the field. When debugging the applied rule using the following command in rules method:
    
    rules: function( command, argument ) {
    ...
              data = $.validator.normalizeRules(
                $.extend(
                    {},
                    $.validator.metadataRules(element),
                    $.validator.classRules( element ),
                    $.validator.attributeRules( element ),
                    $.validator.dataRules( element ),
                    $.validator.staticRules( element )
                ), element );

// debug the value if (!!$.validator.attributeRules(element) && Object.keys($.validator.attributeRules(element)).length > 0) { console.log($.validator.attributeRules(element)); } ...



- Actual value: String
`{ipv4: 'true'}`
- Expected value: boolean
`{ipv4: true}`

### Questions or comments
The PR has been elaborated in my article which describes the issue in more details: [Deep Dive into Validation Rules](https://inchoo.net/dev-talk/deep-dive-into-validation-rules/)

### Contribution checklist (*)
 - [x] Pull request has a meaningful description of its purpose
 - [x] All commits are accompanied by meaningful commit messages
 - [ ] All new or changed code is covered with unit/integration tests (if applicable)
 - [ ] README.md files for modified modules are updated and included in the pull request if any [README.md predefined sections](https://github.com/magento/devdocs/wiki/Magento-module-README.md) require an update
 - [ ] All automated tests passed successfully (all builds are green)
m2-assistant[bot] commented 4 months ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 4 months ago

Hello @dvrgoc,

Thank you for the report and collaboration!

We are able to get the actual result, that is ipv4 value is string in jquery.validate.js. Please refer the screenshot:

image

But can you mention the issue you are facing due to change in datatype.

dvrgoc commented 4 months ago

HI @engcom-November

that is correct, this is datatype issue.

While I was working with other methods of implementing the validation rules, all other rules return Boolean, not String.

image

Please read my article which covers the topic - Deep Dive into Validation Rules

If you have any questions, let me know.

engcom-November commented 4 months ago

Hello @dvrgoc,

Thanks for the update.

I agree with this, inconsistent return type compared to rest of the methods is not a good practice and might cause error further down the line. Hence confirming this.

github-jira-sync-bot commented 4 months ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-12079 is successfully created for this GitHub issue.

m2-assistant[bot] commented 4 months ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

github-jira-sync-bot commented 4 months ago

:x: You don't have permission to export this issue.