diff --git media.module media.module index 1b3500b..9e0f6b0 100644 --- media.module +++ media.module @@ -985,7 +985,7 @@ function media_element_info() { '#input' => TRUE, '#process' => array('media_element_process'), //'#value_callback' => 'media_element_value', - //'#element_validate' => array('file_managed_file_validate'), + '#element_validate' => array('media_element_validate'), '#theme_wrappers' => array('container'), '#progress_indicator' => 'throbber', '#extended' => FALSE, @@ -1018,6 +1018,7 @@ function media_element_process(&$element, &$form_state, $form) { '#title' => $element['#title'], '#markup' => '', '#description' => $element['#description'], + '#required' => $element['#required'], ); //@TODO: This should be loaded from the JS in case of a failed form submission. @@ -1092,6 +1093,27 @@ function media_element_process(&$element, &$form_state, $form) { } /** + * Validate media form elements. The file type is validated during the upload process, but this is + * necessary in order to respect the #required property. + */ +function media_element_validate(&$element, &$form_state) { + if ($element['#required']) { + $field_name = $element['#field_name']; + $lang = $element['#language']; + + $has_value = FALSE; + foreach ($form_state['values'][$field_name][$lang] as $value) { + if ($value['fid']) { + $has_value = TRUE; + } + } + if (!$has_value) { + form_error($element, t('%element_title is required.', array('%element_title' => $element['#title']))); + } + } +} + +/** * Implements hook_filter_info(). * * For performance, the media filter is allowed to be cached by default. See