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

Creating custom widget instance validation is weird due to controller type mismatch #27397

Closed
MellenIO opened this issue Mar 23, 2020 · 4 comments · Fixed by #28379
Closed

Creating custom widget instance validation is weird due to controller type mismatch #27397

MellenIO opened this issue Mar 23, 2020 · 4 comments · Fixed by #28379
Assignees
Labels
Component: Widget Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Priority: P3 May be fixed according to the position in the backlog. Progress: done Reported on 2.4.0 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@MellenIO
Copy link
Contributor

Summary (*)

See this line of code:

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Widget/Controller/Adminhtml/Widget/Instance/Validate.php#L22

The function \Magento\Widget\Model\Widget\Instance::validate() is type hinted to return either a \Magento\Framework\Phrase or a bool
https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Widget/Model/Widget/Instance.php#L294

This means the is_string check is going to fail. This further means that, unless you type cast the phrase to a string, you will not ever get a constructive error message out of the widget validation controller.
This isn't a huge issue for the most part with Magento core because of the frontend validation but for those creating custom widget validation rules it will break.

Examples (*)

$firstResult = __('Test message');
if ($firstResult !== true && is_string($firstResult)) {
    echo 'We have an error!';
}

Does not output anything.

Proposed solution

I think the easiest solution is just to change the validation controller line to the following:

if ($result !== true && (is_string($result) || ($result instanceof \Magento\Framework\Phrase)) {
// ...

On Magento sites with custom validation rules we've gotten around this by adding an afterValidate plugin to \Magento\Widget\Model\Widget\Instance which type casts the result to string (and returns a string for subsequent errors, rather than a phrase).

@MellenIO MellenIO added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Mar 23, 2020
@m2-assistant
Copy link

m2-assistant bot commented Mar 23, 2020

Hi @MellenIO. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@MellenIO do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Mar 23, 2020
@engcom-Echo engcom-Echo self-assigned this Apr 30, 2020
@m2-assistant
Copy link

m2-assistant bot commented Apr 30, 2020

Hi @engcom-Echo. 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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Echo engcom-Echo added Component: Widget Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Apr 30, 2020
@ghost ghost removed the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Apr 30, 2020
@ghost ghost unassigned engcom-Echo Apr 30, 2020
@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Apr 30, 2020
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @engcom-Echo
Thank you for verifying the issue. Based on the provided information internal tickets MC-33880 were created

Issue Available: @engcom-Echo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@ghost ghost assigned zaximus84 May 26, 2020
@sdzhepa sdzhepa added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Jun 15, 2020
@ghost ghost added Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Priority: P3 May be fixed according to the position in the backlog. labels Aug 17, 2020
@m2-community-project m2-community-project bot added Progress: PR Created Indicates that Pull Request has been created to fix issue and removed Progress: PR in progress labels Sep 24, 2020
@magento-engcom-team magento-engcom-team removed the Progress: PR Created Indicates that Pull Request has been created to fix issue label Sep 24, 2020
@magento-engcom-team
Copy link
Contributor

Hi @MellenIO. Thank you for your report.
The issue has been fixed in #28379 by @zaximus84 in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.2 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Sep 25, 2020
@magento-engcom-team magento-engcom-team added the Reported on 2.4.0 Indicates original Magento version for the Issue report. label Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Widget Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Priority: P3 May be fixed according to the position in the backlog. Progress: done Reported on 2.4.0 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants