Download & Extend

media form elements don't respect '#required' property

Project:Media
Version:7.x-2.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (fixed)
Issue tags:Media Initiative, sprint

Issue Summary

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.

Comments

#1

Status:active» needs review

Here's the patch.

AttachmentSizeStatusTest resultOperations
999594_1-media-required_fields.patch1.63 KBIdlePASSED: [[SimpleTest]]: [MySQL] 0 pass(es).View details

#2

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

AttachmentSizeStatusTest resultOperations
999594_2-media-required_fields.patch1.68 KBIdlePASSED: [[SimpleTest]]: [MySQL] 0 pass(es).View details

#3

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...

#4

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.)

#5

Status:needs review» fixed

@bec: As always, you rock :)

Committed.

#6

Status:fixed» closed (fixed)

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

#7

Version:7.x-1.x-dev» 7.x-2.0-unstable6
Status:closed (fixed)» active

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.

AttachmentSizeStatusTest resultOperations
999594_7-media-element-required.patch855 bytesIdleFAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 999594_7-media-element-required.patch. Unable to apply patch. See the log in the details link for more information.View details

#8

Status:active» needs review

#7 works for me

#9

Status:needs review» needs work

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

#10

Status:needs work» needs review

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

#11

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,
  );

#12

daffie: The media field has been deprecated.

#13

#14

@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.

#15

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

per #7 and please patch against latest dev.

#16

Status:needs work» fixed

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

#17

Status:fixed» closed (fixed)

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

#18

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.

AttachmentSizeStatusTest resultOperations
999594-18-media-element-required.patch422 bytesIdlePASSED: [[SimpleTest]]: [MySQL] 5 pass(es).View details

#19

Status:closed (fixed)» needs review

#20

Status:needs review» closed (fixed)

can you please incorporate your patch in the issue here #1558830: media_element_validate() has hardcoded $form_state values reference and does not work with unlimited

#21

Done.

#22

awesome, thanks:)