When I add a "Media file selector" field to a node and set it as a required field, and then visit the node create/edit form, there is no asterisk indicating the field is required, and submitting the form without a value in the field does not trigger a validation error.

Patch coming shortly.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

becw’s picture

Status: Active » Needs review
FileSize
1.63 KB

Here's the patch.

becw’s picture

This bug is still present in the latest 7.x-1.x-dev. I've re-rolled the patch so that it applies cleanly.

JacobSingh’s picture

This seems like a no-brainer. I don't have time to test right now, but my understanding was that the required property would "just work" and doesn't need to be additionally specified. Is this not the case? If I look in system.module, I don't see it specified in most element_info arrays.

I'm a bit foggy on this area of the code...

becw’s picture

When you're providing your own element type, you have to deal with the #required property yourself: the 'media' FAPI element type gets built out within the media module, and it needs to add the property to its child input elements. (I think that #required isn't inherited by FAPI descendants.)

JacobSingh’s picture

Status: Needs review » Fixed

@bec: As always, you rock :)

Committed.

Status: Fixed » Closed (fixed)

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

bmcmurray’s picture

Version: 7.x-1.x-dev » 7.x-2.0-unstable6
Status: Closed (fixed) » Active
FileSize
855 bytes

The default 'fid' of a media input is '0', that is, zero as a string. This is causing the check in media_element_validate to never catch an empty submission on a required field.

Additionally, $element['#title'] appears to be empty regardless of whether the form element has a label set or not, so I've modified the error thrown when a required field isn't found to use: $form_state['field'][$field_name][$lang]['instance']['label']

Patch attached.

azinck’s picture

Status: Active » Needs review

#7 works for me

Status: Needs review » Needs work

The last submitted patch, 999594_7-media-element-required.patch, failed testing.

azinck’s picture

Status: Needs work » Needs review

Though #7 needs to be re-rolled against the correct directory.

daffie’s picture

This patch won't work for a form element with the type set to 'media'.

  $form['fid'] = array(
    '#type' => 'media',
    '#title' => t('Image'),
    '#default_value' => array('fid' => '')),
    '#required' => TRUE,
  );
azinck’s picture

daffie: The media field has been deprecated.

Dave Reid’s picture

Issue tags: +sprint, +Media Initiative
daffie’s picture

@azinck: Thank you for the info. I have switched back to 7.x-1.x. I am following Media gallery to see how they will solve this problem.

dddave’s picture

Version: 7.x-2.0-unstable6 » 7.x-2.x-dev
Status: Needs review » Needs work

per #7 and please patch against latest dev.

ParisLiakos’s picture

Status: Needs work » Fixed

I fixed the label part.
Previous commit for another issue accidentally fixed the other part.
marking as fixed.

Status: Fixed » Closed (fixed)

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

azinck’s picture

Not fixed for me. In a multi-value form one of the values that comes through is the text of the "add another" button which is a string, not an array. The existing method of checking for empty fails when it hits this string.

azinck’s picture

Status: Closed (fixed) » Needs review
ParisLiakos’s picture

Status: Needs review » Closed (fixed)
azinck’s picture

Done.

ParisLiakos’s picture

awesome, thanks:)