Problem/Motivation
If you edit an order and submit a custom adjustment (or any type) without a label you get a PHP error message.
The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">InvalidArgumentException</em>: Missing required property label. in <em class="placeholder">Drupal\commerce_order\Adjustment->__construct()</em> (line <em class="placeholder">73</em> of <em class="placeholder">modules/contrib/commerce/modules/order/src/Adjustment.php</em>). <pre class="backtrace">Drupal\commerce_order\Plugin\Field\FieldWidget\AdjustmentDefaultWidget->massageFormValues(Array, Array, Object) (Line: 368)
Proposed resolution
Make the label required in form.
Remaining tasks
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2926563-2-17.patch | 3.44 KB | alexpott |
| #18 | 2926563-2-17.test-only.patch | 1.72 KB | alexpott |
| #13 | 2926563-2-13.patch | 2.74 KB | alexpott |
| #12 | 2926563-12.patch | 6.95 KB | sorabh.v6 |
| #9 | Schermafbeelding 2017-12-05 om 16.36.40.png | 23.3 KB | harings_rob |
Comments
Comment #2
alexpottComment #5
alexpottThe fail for the non test-only patch in #2 seems unrelated tried to re-create locally and it does not happen.
Comment #8
bojanz commentedThanks!
Comment #9
harings_rob commentedThis does not seem correct. Adding the required = TRUE leads to some regression.
Following steps are to reproduce:
1. Start creating order in backend.
2. Fill in the order but not the adjustments
3. Save (You cannot because of html validation)
4. Disable html validation
5. Save
6. Get an error message that the field is mandatory, but no form is highlighted
7. Select an adjustment to see the error field.
Below are some screenshots:
https://www.drupal.org/files/issues/Edit___Drupal.png
https://www.drupal.org/files/issues/Edit___Drupal%203.png
https://www.drupal.org/files/issues/Schermafbeelding%202017-12-05%20om%2...
Could you let me know what the intended behavior is? Maybe I can provide a fix.
Comment #10
smccabe commentedCan confirm what @harings_rob sees, I also get the same issue and the required = TRUE is wrong. Seems like a larger widget rework would be needed before this would work? It errors the form if you are not adding an adjustment at all, but the error is hidden.
For now I think a revert of just that small piece is best?
Comment #11
harings_rob commentedHi @smccabe,
I forgot to include the PR for this:
https://github.com/drupalcommerce/commerce/pull/841
Comment #12
sorabh.v6I confirm the code in PR in #11 is working for me. Posting the patch from #11.
Comment #13
alexpottI think we should just have one test called
testCreateOrderAdjustmentValidation()- actually looking at the test further I don't think we need a new method at all.Comment #14
harings_rob commentedHi @alexpott,
However we can indeed test them from a single method, I wanted to be more verbose on the test case.
If the current test fails, it might not be directly visible what the issue is except that it is when creating an order.
If testCreateOrderWorksWithoutAdjustment fails, it is visible that there will be an issue when there is no adjustment, same for testOrderWithInvalidAdjustmentShowsError.
Ideally tests should be compact, and focussed on a single functionality.
This is, I guess, a personal opinion. But I just wanted to clarify why I did it this way.
Comment #15
alexpott@harings_rob - thinking about this some more
testCreateOrderWorksWithoutAdjustmentis definitely worth it. As that is the bug you're fixing. My bad.But actually that test does not successfully create an order.
Comment #16
alexpottNeeds work #15. I'll improve the test coverage.
Comment #17
harings_rob commentedIt indeed does not, could be an improvement to the test.
Comment #18
alexpott@harings_rob
I totally agree for unit tests this should be the aim. With functional tests there is always a balance between cost of maintenance, running each test, and verbosity and these things.
Comment #20
mortona2k commentedI didn't see the php error, but when I tried to save the order form with an empty adjustment, I got this js console error:
An invalid form control with name='adjustments[1][definition][label]' is not focusable.
Applying #18 fixes the error.
Comment #21
smccabe commentedTested patch as well, also works for me.
Comment #23
bojanz commentedThanks!
Replaced this with a !empty, it's cleaner.
Reverted this change. While an identical comparison is theoretically better, we don't do this for any other string comparison in Commerce, so consistency wins out.