My dblog was showing lots of "Notice: Undefined index: #required in mothership_form_element() (line 76 of /www/guesswatches.com/d7/sites/all/themes/mothership/mothership/functions/form.php)." errors.

Took a look, and it's this line:

if( $element['#required'] ) {

The fix is to add isset(), so it becomes:

if(isset($element['#required'])) {

Comments

Anonymous’s picture

I tested this, your fix works.

jessepinho’s picture

If I may be picky:

if (!empty($element['#required'])) {

would be better, since $element['#required'] could be set, but set to FALSE.

yatil’s picture

I think both solutions alter the meaning of the statement:

Let’s say $element['#required'] = TRUE then

if( $element['#required'] ) => TRUE
if(isset($element['#required'])) => TRUE
if (!empty($element['#required'])) => TRUE

If $element['#required'] = FALSE

if( $element['#required'] ) => FALSE
if(isset($element['#required'])) => TRUE
if (!empty($element['#required'])) => TRUE

If $element['#required'] is not set

if( $element['#required'] ) => ERROR
if(isset($element['#required'])) => FALSE
if (!empty($element['#required'])) => TRUE

most sensitive code is imho

if (isset($element['#required']) && $element['#required']) {
jessepinho’s picture

yatil: Your middle example, where $element['#required'] = FALSE, is incorrect about the return value of !empty(). If something is set to FALSE, empty() returns TRUE. Since a negation operator (an exclamation mark) is placed before empty(), it will instead return FALSE—not TRUE, like you wrote.

yatil’s picture

Right, I was wrong :-)

gagarine’s picture

Status: Needs review » Reviewed & tested by the community

#2 is the right way.

mojzis’s picture

StatusFileSize
new438 bytes

adding a patch for #2 :)

GemVinny’s picture

Thanks for patch - had to change line numbers but apart from that it worked for me.

Gem

zeropx’s picture

StatusFileSize
new438 bytes

Tested and can validate this as a fix.

Updated Mojzis's patch #7 with a small spelling fix on the word "required" it was "reguired" before :P

jessepinho’s picture

Doesn't look like it was a G to me...?

EDIT: Oh, in the comments. My bad.

zeropx’s picture

StatusFileSize
new598 bytes

Oops did that bad. :D Still fresh to this. Sorry.

Hopefully this one is right.

derhasi’s picture

Status: Reviewed & tested by the community » Fixed

This is already fixed in the current dev (since september): @see http://drupal.org/commitlog/commit/9436/bc33b09712e38e3e93b496a39014c458...

zeropx’s picture

Good catch, forgot to check Dev. :D Still spelled "required" wrong though with it stil "reguired" hehe

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jamiehollern’s picture

This issue is not fixed as of 7.x-2.10. The simple patch in #7 is the correct way to deal with this.

Although this isn't a big issue, Drupal coding standards dictate PHP should be E_ALL compliant and this causes issues for inexperienced site builders who don't know how to deal with the notice messages.

jamiehollern’s picture

Status: Closed (fixed) » Active
mcrittenden’s picture

@jamiehollern is correct, the issue has re-appeared due to this line:

if(isset($element['#title']) && $element['#title'] != 'Language' && $element['#required']) {

That line should be rewritten like so:

if(isset($element['#title']) && $element['#title'] != 'Language' && !empty($element['#required'])) {
mortendk’s picture

Status: Active » Needs review

please test the latest dev version - "works on my machine" ;)

mcrittenden’s picture

Status: Needs review » Fixed

Fixed by this commit. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Replace with