You are here

function farm_livestock_move_form_validate in farmOS 7

Validate callback for movement quick form.

File

modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc, line 417
Farm livestock move quick form.

Code

function farm_livestock_move_form_validate($form, &$form_state) {

  // Validate animal or group asset.
  // Extract asset IDs and load assets.
  $assets = array();
  $asset_names = drupal_explode_tags($form_state['values']['move']['assets']);
  foreach ($asset_names as $asset_name) {
    $id = 0;
    $matches = array();
    $result = preg_match('/\\[id: ([0-9]+)\\]/', $asset_name, $matches);
    if (!empty($matches[$result])) {
      $id = $matches[$result];
    }

    // If an ID couldn't be extracted, throw an error.
    if (empty($id)) {
      form_set_error('move][asset', t('Could not load the animal asset. Make sure the animal asset ID is included. For example: "My animal [id: 123]"'));
    }

    // Load the asset.
    $asset = farm_asset_load($id);

    // If asset was loaded add to list of assets to save.
    if (!empty($asset)) {
      $assets[$id] = $asset;
    }

    // If the asset didn't load, throw an error.
    if (empty($asset)) {
      form_set_error('move][asset', t('Could not load the animal asset. Make sure the animal name and ID are correct.'));
    }
  }

  // Save the asset to the form state.
  $form_state['storage']['assets'] = $assets;
}