When activating a dependency that changes the required state of a field, a red asterisk is added to the field on the front end but no required validation is added and the entity can be saved without values in those fields.

Both modules, Conditional Fields and Field Conditional States, are based on the states.api and don't fix the problem either - a very old and annoying issue not solved within 3 years:

#1561272: Conditionally required fields are not required
#2306401: Enforce the state "required"
#2396685: Conditionally required fields are not required

Is it really that difficult for a skilled programmer (which I'm not) to fix this problem using drupal validation in combination with jquery (hide, fadeIn, addClass, removeClass)?
There are things that should be considered as pretty basic (from scratch) when using "powerful" CMS like Drupal - this is definitely one thing!

Comments

rooby’s picture

Issue summary: View changes
Anonymous’s picture

Priority: Major » Normal

The issues you refer to are part of contrib, not core. And AFAIK conditional fields are not part of D8 core either. So I'm not really sure why you filed this issue here. Can you elaborate on what the D8 core bug is exactly?

Is it really that difficult for a skilled programmer (which I'm not) to fix this problem using drupal validation in combination with jquery (hide, fadeIn, addClass, removeClass)?

No, not really. But writing a specific solution in a custom module is definitely not the same as writing code for a drupal contrib module, let alone a core module.

There are things that should be considered as pretty basic (from scratch) when using "powerful" CMS like Drupal - this is definitely one thing!

...

rooby’s picture

I thikn what they are referring to is the required state in the states api.

The problem is that it allows setting a field to be required but it isn't actually required, it just has a red asterisk.

rooby’s picture

Issue summary: View changes
fengtan’s picture

We can reproduce this in Drupal 7.50 -- not sure about Drupal 8.

Say you have this form:

function my_form() {
  $form['my_checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => 'My checkbox',
  );
  $form['my_textfield'] = array(
    '#type' => 'textfield',
    '#title' => 'My textfield',
    '#states' => array(
      'required' => array(
        ':input[name="my_checkbox"]' => array('checked' => TRUE),
      ),
    ),
  );
  return system_settings_form($form);
}

When checking my_checkbox, a red star shows up near my_textfield and suggests it is a required field. However, we can submit the form even if my_textfield is empty.

antiorario’s picture

Project: Drupal core » Conditional Fields
Version: 8.0.0-beta4 » 8.x-1.x-dev
Component: forms system » Code
Status: Active » Closed (duplicate)
Related issues: +#1561272: Conditionally required fields are not required

This was in the wrong spot, but it's also a duplicate of #1561272: Conditionally required fields are not required.