4

I have added two fields in to my custom content type using "Add existing field".

For specific URL, I need to set these two fields as Required.

So I have altered my form and set required condition. After setting it, I have printed the form using devel, these fields are having required settings.

$form['field_name']['und']['#required'] = TRUE;
$form['field_email']['und']['#required'] = 1;
dsm($form);

But required condition is not applied.

enter image description here enter image description here

That is, I could not see red color required indication as well as its not working in the form submit.

3 Answers 3

6

Try

$form['field_email']['und']['0']['value']['#required'] = TRUE;

and if not work then try

$form['field_email']['und']['0']['#required'] = TRUE;
4
  • @kalidasan your welcome :).
    – Yuseferi
    Dec 23, 2013 at 16:03
  • @kalidasan please if you get your answer mark the answer as correct
    – Yuseferi
    Dec 23, 2013 at 16:05
  • Thanks for the ['value'] tip. That's what was missing for me.
    – mcaleaa
    Jan 27, 2015 at 10:25
  • @mcaleaa your welcome, it get me good sense that my answers help others in future :)
    – Yuseferi
    Jan 27, 2015 at 10:30
0

I ended up needing an additional value set to make this work and thoguht others might benefit from knowing so:

$form['profile_personal_information']['field_company'][LANGUAGE_NONE]['#required'] = 1;
$form['profile_personal_information']['field_company'][LANGUAGE_NONE][0]['#required'] = TRUE;
$form['profile_personal_information']['field_company'][LANGUAGE_NONE][0]['value']['#required'] = TRUE;
-1

You can use Field Validation module:

This module adds an extra tab to each field instance, allowing you to specify validation rules for your field instances.

Or use hook_field_validate in you custom module.

1
  • Is this module need for this small fix?
    – Kalidasan
    Dec 17, 2015 at 10:42

Your Answer

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

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