1

This seems like it should be simple, but has been challenging me for a few days now. I have a custom content type which contains a selectlist which defaults to 'Pending' for new nodes. Someone will later come along and set these to 'Approved' and when they do so I need a text field to be available and required prior to saving the node again.

I have tried the available modules 'Conditional Fields' and 'Field Conditional States' but they do not seem to work to make fields actually required. I assume this is because the JavaScript Form state has no effect on server-side logic and validation.

If I was coding this outside of Drupal 7 I would probably make the textfield disabled, required, and give it a default value. Then use an .onchange to check if it is set to “Approved” and, if so, enable it and empty the default value but I am not sure how to implement this with Drupal yet.

I didn't find anything else in the way of modules that seemed to work, so I have now begun looking at the function 'drupal_process_states'. This seems like it might be the right path, but is going to take me a while to figure out.

Any help is appreciated. Thanks!

1 Answer 1

1

Field Conditional States will help you modify the look of the field: it will make it appear or disappear, or will add the red asterisks to make it look required. But as you noticed, making it look required is not the same thing as making it required. What I have done is using a combination of Field Conditional States to change the look of the field, AND Field Validation to actually make it required (so that the form returns the "Field required" error message when submitted). Field Validation has a way to return the error by checking that the field is not empty, AND checking if a different field in the form has a particular value. You need the "PHP code" type of validation for this. Take a look at the documentation for this module's PHP code option.

3
  • Thanks to this recommendation I included PHP validation using Field Validation. I really didn't need Conditional States due the flexibility provided in the PHP validation option which allows me to highlight the missing field. In case it is useful to anyone else, here is what I ended up with: if($this->value=='approved' and empty($this->entity->field_account_string[und][0][value])){ echo '<style type="text/css"> #edit-field-account-string-und-0-value {border-style: solid; border-color: red;} </style>'; $this->set_error(); })
    – Jeramy
    Oct 29, 2015 at 19:05
  • Alternatively, if you click Set errors using field API the API will automatically turn the field border red without the need to write css echo
    – argiepiano
    Oct 29, 2015 at 22:08
  • I am actually using both. The validation is on the 'Approved' field and I am using the css to also highlight the 'Account' field with a custom error that reads "You must have a valid account number to set the status to approved." Thanks for the detail though, I had been fighting with this for weeks and it is great to finally have it resolved.
    – Jeramy
    Nov 2, 2015 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.