I'm using the OpenLayers map as the Geofield input widget and have set the field to be a 'required' field.
This 'required' option doesn't seem to be respected for the form widget?


ctuxboy’s picture

I have the same problem.

Brandonian’s picture

Title: Geofield/Openlayers widget 'field is required' setting not respected » Geofield 'field is required' setting not respected
Status: Active » Postponed

Thanks for the bug reports, @mwesthof and @ctuxboy. This is an issue with all of our widgets in Geofield. Due to how we handle our input widgets (a Geofield widget in 7.x-1.x is compromised of 8 different inputs that might be hidden depending on which input you've chosen), I'm not sure there's an easy answer to this, especially with the Openlayers input.

In the 2.x branch, this will be much easier to handle b/c we'll use standard/custom FormAPI elements, which will be easier to work with in terms of setting whether or not they're required.

mwesthof’s picture

@Brandonian thank you for your response.
Figured this might be tackled in the 2.0 branch. I tried fixing it within the 1.0 branch, but with no success within a limited amount of time because of the way the code is setup.

For those interested in a possible (quick and dirty) work-a-round:
I ended up with writing a small module which implements hook_form_alter() and hook_form_validate() to check if the posted value of the field is empty.

The field required status is now checked, but this obviously is a not a very charming solution since it's completely outside of the Geofield module and can end you up with confusing situations.

sylvaticus’s picture

@mweshof can u share the code ? Thanks

mwesthof’s picture

Sure @sylvaticus. You'll have to modify the snippets to create your own module (change the YOUR_MODULE into your module name, YOUR_CONTENT_TYPE into the name you used and the 'field_location_wkt' field name, into the field name you used).

It's actually just a hook_form_alter implementation. It's not to pretty, since the code has to be carved to work with specific variables. But at least you can check if the map was clicked when the node form is posted.

Since I only need one language, I left the language support at 'UND'. If you need another language, you can replace UND with the current user's language I guess. (Haven't tested that.)

 * @file YOUR_MODULE.module Add explicit check to see if some coordinates are passed from the geofield map.
 * Implements hook_form_alter().
 * Add explicit form_validate callback to a content type specific form.
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  // only for node forms
  if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) {
    // if ($form['#node']->type == 'YOUR_CONTENT_TYPE') {
    $form['#validate'][-1] = 'YOUR_MODULE_form_validate';

 * Form validation  callback function for YOUR_MODULE_form_alter().
 * Add explicit check to see if some coordinates are passed from the geofield map.
function YOUR_MODULE_form_validate($form, &$form_state) {
  // @TODO replace with language independent code
  // Notice: This is a work-a-round because the geofield widget provided doesn't check
  // the required status properly. Once geofield module fixes the issue, we
  // should go back to that module's provided functionality.

  // 'field_location_wkt' is the machine name of the field as given in your fields settings 
  // for the content type.
  $field_location_wkt = $form_state['values']['field_location_wkt']['und'][0]['wkt'];
  if ($field_location_wkt == "") {
  	form_set_error('Event location', t('Please click on the map using the pen tool, to select a location where the event occurred.'));

Updated: typo
Update 2: typo in line // if ($form['#node']->type == 'YOUR_CONTENT_TYPE') {

d_f’s picture


You are a god. Thanks a ton!

Confirmed this works like a charm.

*edit: spoke too soon. It seems to want to apply itself on every content type I have on the site, even content types without the specified field. So, if I want to edit any existing content or add new content that is not of the same type as the one with the geofield field, I get the error "Notice: Undefined index: field_location_on_map in geofield_check_form_validate() (line 30 of custom module)" upon submitting or editing any content which does not have the geofield field.

*edit2: If I edit the "YOUR_CONTENT_NAME" var with the machine name of the content type (by uncommenting that code), I get a "Server error
The website encountered an error while retrieving http://websitename.url/. It may be down for maintenance or configured incorrectly." I'm a bit lost as to how to get this working correctly, any suggestions are greatly appreciated.

mwesthof’s picture

@d_f The error message sounds to abstract to tie any conclusions to, but I noticed a typo in the commented out code: a '$'-sign character was missing in my example. Try if that fixes it for you. Otherwise, enable more expliciet error messaging on your development environment or check the error log.

Please also notice that the code was modified (I took out customer and application specific names, etc.) and not tested on syntax after being copied from my working version.

spidersilk’s picture

I'm trying to use the code provided in #5 but can't get it to work - I changed the module name and node type to mine, and initially had changed field_location_wkt to just field_location, since the comments say to use the machine name of your field, but after that didn't work, and I found that the Location field did have a column called field_location_wkt in the database, I tried setting it back to that and it still didn't work.

I don't get any error messages or anything like that - not while submitting the item, and not in the Drupal log or the server error logs either. It just doesn't prevent nodes with blank locations from being submitted.

Edit: Did a bit of investigating with Devel and dpm() statements, and it seems to be that the validate function is not being called, although everything up to that point works. The form_alter function runs, it successfully identifies the node form and the node type, but then it doesn't call the validation function (I put a dpm() statement at the very beginning of that function, and that one never outputs anything). I tried changing the [-1] to [] on a co-worker's advice, but that didn't help.

Anyone have any other ideas?

Michael Westhof’s picture

Since everything works, but the validate function isn't being called, the issue your stuck on seems to be a generic form api question (?).

What I can think of:

  1. Trying clearing cache
  2. Try calling the validate function through on of these suggestions http://stackoverflow.com/a/18700827
juanramonperez’s picture

Issue summary: View changes

This works for me on latitude and longitude widget

function MY_MODULE_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
	$form['field_location'][LANGUAGE_NONE][0]['geom']['#required'] = TRUE;
timos’s picture

Version: 7.x-1.x-dev » 7.x-2.3
Status: Postponed » Active


I'm sorry but this issue is not fixed on 7.x-2.x branch. I use the last stable version and i get the problem at leat with the Latitude/Longitude widget.

I'll try to do a patch...

I'm sorry, i think i confused with another problem. Mine is that one i'm on field instance settings from, if i check the required field checkbox, i have to fill default value (and it's a non sense because, if default values are filled, the required field doesn't check anything as fields are always filled by the default...)

But, well, it seems be a another problem, so i just open a new issue.



timos’s picture

Version: 7.x-2.3 » 7.x-1.x-dev
Status: Active » Postponed

  • Brandonian committed 85f73d3 on 8.x-clear-files
    Better handling of required field for latlon element. From [#1555854]