# Required error is not working as expected when rules are present

**URL:** <https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631>\
**Category:** General\
**Created:** [July 25, 2023, 4:21pm UTC](https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631 "2023-07-25T16:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![collegewap](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/collegewap/32/744_2.png) [@collegewap](https://jsonforms.discourse.group/u/collegewap)\
**Post date:** [July 25, 2023, 4:21pm UTC](https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631/1 "2023-07-25T16:21:47Z")

</div>

Issue1: Let’s say fieldB is required only if fieldA has a value C1. When I use the rule, it works fine. However when I clear the value from fieldA the required error still remains.

Issue 2:  
While using the required rule, the error message is displayed properly, however, the required property is false inside the input component. If we have to show an asterisk for required items, then it doesn’t work while using conditional required.

 ![image](https://global.discourse-cdn.com/free1/uploads/jsonforms/original/1X/78b9436c9ba6632bd3dc1655e10a6ae01c68e132.png)

Repo (for both issues): [GitHub - collegewap/jsonforms-react-seed at issue-2](https://github.com/collegewap/jsonforms-react-seed/tree/issue-2)

---

<div class="post-metadata">

**Author:** ![collegewap](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/collegewap/32/744_2.png) [@collegewap](https://jsonforms.discourse.group/u/collegewap)\
**Post date:** [July 25, 2023, 4:22pm UTC](https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631/2 "2023-07-25T16:22:31Z")

</div>

Schema: [https://github.com/collegewap/jsonforms-react-seed/blob/issue-2/src/schema.json](https://github.com/collegewap/jsonforms-react-seed/blob/issue-2/src/schema.json)

UISchema: [https://github.com/collegewap/jsonforms-react-seed/blob/issue-2/src/uischema.json](https://github.com/collegewap/jsonforms-react-seed/blob/issue-2/src/uischema.json)

---

<div class="post-metadata">

**Author:** ![collegewap](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/collegewap/32/744_2.png) [@collegewap](https://jsonforms.discourse.group/u/collegewap)\
**Post date:** [July 25, 2023, 4:23pm UTC](https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631/3 "2023-07-25T16:23:08Z")

</div>

Screenshots (for issue1): Screenshots: [Required issue · Issue #1 · collegewap/jsonforms-react-seed · GitHub](https://github.com/collegewap/jsonforms-react-seed/issues/1)

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [July 26, 2023, 9:35am UTC](https://jsonforms.discourse.group/t/required-error-is-not-working-as-expected-when-rules-are-present/1631/4 "2023-07-26T09:35:32Z")

</div>

Hi @collegewap,

> [@collegewap](#):
>
> Issue1: Let’s say fieldB is required only if fieldA has a value C1. When I use the rule, it works fine. However when I clear the value from fieldA the required error still remains.

The linked schema behaves as expected

```auto
{
  "type": "object",
  "properties": {
    "productAccess_dc": {
      "type": "string"
    },
    "requestType": {
      "type": "string"
    }
  },
  "required": [],
  "allOf": [
    {
      "if": {
        "properties": {
          "requestType": {
            "const": "C1"
          }
        }
      },
      "then": {
        "required": ["productAccess_dc"]
      }
    }
  ]
}

```

The `if`-schema also returns true when `requestType` is undefined. That is default JSON Schema behavior, which you can easily test with an online validator like [this one](https://www.jsonschemavalidator.net/). As the `if`-schema successfully validates when `requestType` is not set, `productAccess_dc` will be required.

If you only want the `if`-schema to successfully validate when `requestType` is set AND is of value `C1`, then you need to add an additional required, e.g.

```auto
      "if": {
        "properties": {
          "requestType": {
            "const": "C1"
          }
        },
        "required": ["requestType"]
      },

```

> Issue 2:  
> While using the required rule, the error message is displayed properly, however, the required property is false inside the input component. If we have to show an asterisk for required items, then it doesn’t work while using conditional required.

This is a limitation of the JSON Forms bindings. Our bindings are only able to consider statically required properties. So in case of conditional required our `required` prop will always be `false`. To workaround this you can use custom renderers which are able to understand the dynamic required use cases in your schemas.
