You are here

function farm_livestock_milk_form_validate in farmOS 7

Validate callback for milk quick form.

File

modules/farm/farm_livestock/farm_livestock.farm_quick.milk.inc, line 87
Farm livestock milk quick form.

Code

function farm_livestock_milk_form_validate($form, &$form_state) {

  // Validate the animal/group asset.
  if (!empty($form_state['values']['asset'])) {

    // Extract asset ID.
    $id = 0;
    $matches = array();
    $result = preg_match('/\\[id: ([0-9]+)\\]/', $form_state['values']['asset'], $matches);
    if (!empty($matches[$result])) {
      $id = $matches[$result];
    }

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

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

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

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