I have a field that is part of a content type that I need to make required by using a JavaScript injector. The reason that I can't just make it required while setting the rules for that field is because I have to parse a string that will display on the same page whether or not I need to make it required. Any thoughts?

  • 1
    How sensitive is the form? If you absolutely need to make sure that the field is completed, you need the logic to run server-side. A determined user could easily remove any required attribute you set by javascript, and get through the form submission. I guess you could theoretically send an AJAX request whose callback loads the cached form and updates the #required status based on the client-side data, but it'd be quite messy. What's a javascript injector by the way, is that a module?
    – Clive
    Commented Aug 19, 2015 at 17:03
  • @Clive yes, it seems that it's a module Commented Aug 19, 2015 at 17:11
  • @Clive Yes, it is a module. I am willing to do everything server side with PHP, but I am rather new to Drupal, do you have any recommendations for how to get going with PHP and module development?
    – Matt
    Commented Aug 19, 2015 at 17:11
  • 1
    Thanks @user1527576. Matt, the bible is here, but there is a bit of a learning curve. If you're new to PHP too that's going to make it tricky, I don't remember coming across any articles on learning both at the same time. You kinda need to know PHP before most of module development stuff will make enough sense to use it effectively. Because it's so complex it's difficult to just copy/paste code snippets and have them work without tweaking too. Maybe someone else will be able to point you to a good resource, sorry I don't really have anything
    – Clive
    Commented Aug 19, 2015 at 17:16
  • @Clive, learning PHP is not an issue, I've been programming in PHP for almost a year now. I'm just not sure how to access fields and everything before a page loads.
    – Matt
    Commented Aug 19, 2015 at 17:17

1 Answer 1


Matt -

I would recommend looking into hook_node_validate(). This is a function hook provided by Drupal core that allows you to add custom validation into the form. While you cannot select "required" from the field UI, perhaps you can validate it here and then return an error if the form doesn't provide the correct information for that specific field.

Other things to investigate: hook_form_alter() and hook_validate - where you can add custom validation functions into the Drupal form array itself.

Helpful links: https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_validate/7


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.