[MGNLFORM-275] Skip validation of empty non-mandatory field Created: 15/Dec/15  Updated: 08/Feb/16  Resolved: 04/Feb/16

Status: Closed
Project: Magnolia Form Module
Component/s: None
Affects Version/s: 1.4.8, 2.0.1
Fix Version/s: 2.3.4

Type: Bug Priority: Major
Reporter: Vincent Gombert Assignee: Oanh Thai Hoang
Resolution: Fixed Votes: 0
Labels: support
Remaining Estimate: 6.75h
Time Spent: 2d 1.25h
Original Estimate: 3d

Attachments: PNG File FormValidation.png    
Issue Links:
Relates
causality
caused by MGNLFORM-178 Form's Honeypot anti-spam - needed im... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:
Sprint: Saigon 29
Story Points: 5

 Description   

There is a validation error when an empty field is validated.
Here are the step to reproduce:

  1. create a form
  2. add a non mandatory input text field with email validator
  3. submit the form with empty input text
  4. a validation error occurs: the input text is not valid

Looking at info.magnolia.module.form.templates.components.DefaultFormDataBinder i noticed that a field is validated if value != null. On previous version of form module the test was !StringUtils.isEmpty(value).



 Comments   
Comment by Oanh Thai Hoang [ 27/Jan/16 ]

Hi rkovarik, tmattsson

While working on DefaultFormDataBinder class. I found below code to get multiple values:

final String values = StringUtils.join(MgnlContext.getParameterValues(controlName), "__");

In case of multiple-empty-mandatory fields, those empty fields value will be set as "__" and can pass the validator of mandatory. Here is the unit test that runs failed.

This test is provided 2 empty-mandatory fields, expectation result is invalid but they still pass the validator

@Test
    public void mandatoryFieldsWithMultipleValuesIsNotValidWhenTheyAreEmpty() throws Exception {
        // GIVEN
        Node fieldNode1 = new MockNode();
        fieldNode1.setProperty(controlNamePropertyName, controlName);
        fieldNode1.setProperty("mandatory", true);
        fieldList.add(fieldNode1);
        fieldNode.setProperty("mandatory", true);
        when(request.getParameterValues(controlName)).thenReturn(new String[]{"", ""});
        MockDefaultFormDataBinder binder = new MockDefaultFormDataBinder(templateDefinitionRegistry, formModule);

        // WHEN
        binder.bindAndValidateFields(fieldList.iterator(), step);

        // THEN
        assertFalse(step.isValid());
    }

    private class MockDefaultFormDataBinder extends DefaultFormDataBinder {

        public MockDefaultFormDataBinder(TemplateDefinitionRegistry templateDefinitionRegistry, FormModule formModule) {
            super(templateDefinitionRegistry, formModule);
        }

        @Override
        protected String getErrorMessage(String message, Node node) {
            return "Error";
        }
    }

I have committed code to fix this case in https://git.magnolia-cms.com/users/oanh.thai/repos/form/commits/fa461bce8c2ada7217c29d8cd3f931182180b719.

Please feel free to review it for me. Thanks

Generated at Mon Feb 12 05:38:36 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.