0

In my drupal-based project, we make a difference between laptop users and tablet users: Laptop users can skip some fields when registering, whereas tablet users have to fill in all of them. The trick is easily done by hiding or showing those fields from the signup form with jQuery and Modernizr. And I say trick, because it works, despite some of those hidden fields being marked as required, they are still allowed to signup without filling on them.

The problem comes when users forget their password and request a link to create a new one. When they get to the screen of "Enter new password and confirm it", users who have initially signed up via laptop (i.e., not filling on some of the required fields), cannot continue because they get the error "Fields X and Y cannot be left blank", or sth similar.

I want to disable these 2 fields from being required for Drupal (not from the signup form, where I have already "hidden" them), but I am not sure how to proceed. Where should I do this?

1 Answer 1

0

I would suggest, you use hook_form_alter() and then check for laptop users. If laptop users are on form then make #required false for these two fields.

Example:

$form['title']['#required'] = FALSE; 

Note: FYI Avoid using unset() in such type of scenario either set the #access property or #required property to FALSE. This keeps them in the form build tree, which avoids problems if other modules try to access or alter that information.

5
  • I am afraid I am a bit newbie for your answer... I would need something more like "go to this table in MySQL, search for X in field F and set 'required' to FALSE"... Either in the MySQL or in the Drupal admin panel. But I would definitely disable for all users.
    – Jago
    Commented May 29, 2014 at 11:10
  • @Jago first you look into api.drupal.org/api/drupal/modules!system!system.api.php/… and keep in mind never change in db directly there are number of options available to deal with such type of options. In your case you create custom module, create hook_form_alter and print the $form array and then do override respective fields as I suggested. You simply make field optional to avoid validation issue. Once you do all this stuff then share your code with us, if problem persists. Commented May 29, 2014 at 13:37
  • Ok, fair enough. Can you point me to some good example of usage of this? The module is already developed, I just need to locate where and how to use the function. So, can you provide a good example of usage? Thanks
    – Jago
    Commented May 30, 2014 at 5:01
  • I am adding to hook_form_FORM_ID_alter() the following: $form['card_holder_name']['#required'] = FALSE; but it does not seem to work. I always get the message that the field is still required...
    – Jago
    Commented May 30, 2014 at 8:52
  • I also tested this code at my end it works for me after clearing cache. I am sure it would also work for you, can you share your complete module code with me. And also confirm you are using correct form field name. If still you find issue with this, then please share your code I would fix this for you. Commented May 30, 2014 at 17:40

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.