You are here

function recipe_ingredient_autocomplete_validate in Recipe 7.2

Form element validate handler for recipe ingredient autocomplete element.

1 string reference to 'recipe_ingredient_autocomplete_validate'
recipe_field_widget_form in ./recipe.module
Implements hook_field_widget_form().

File

./recipe.module, line 544
Contains functions for Recipe node CRUD and display.

Code

function recipe_ingredient_autocomplete_validate($element, &$form_state) {
  if (empty($element['unit_key']['#value']) && !empty($element['name']['#value'])) {
    form_error($element['unit_key'], t('You must choose a valid unit.'));
    return;
  }

  // The autocomplete widget doesn't supply the iid. Search for it and create
  // the new ingredient, if necessary.
  $value = array();
  if ($name = $element['name']['#value']) {

    // Get the field settings.
    $field = field_widget_field($element, $form_state);

    // Don't convert to lowercase if there is a ® (registered trademark
    // symbol).
    if ($field['settings']['ingredient_name_normalize'] == 1 && !preg_match('/®/', $name)) {
      $name = trim(strtolower($name));
    }

    // Try to find an iid with a name matching the entered ingredient name.
    // recipe_ingredient_id_from_name() creates the ingredient if not found.
    $value = array(
      'quantity' => $element['quantity']['#value'],
      'unit_key' => $element['unit_key']['#value'],
      'iid' => recipe_ingredient_id_from_name($name),
      'name' => $name,
      'note' => $element['note']['#value'],
      '_weight' => $element['_weight']['#value'],
    );
  }
  form_set_value($element, $value, $form_state);
}