function farm_livestock_birth_form_validate in farmOS 7
Validate callback for birth quick form.
File
- modules/
farm/ farm_livestock/ farm_livestock.farm_quick.birth.inc, line 190 - Farm livestock birth quick form.
Code
function farm_livestock_birth_form_validate($form, &$form_state) {
// Validate mother and father.
$parents = array(
'mother',
'father',
);
foreach ($parents as $parent) {
if (!empty($form_state['values']['birth'][$parent])) {
// Extract asset ID.
$id = 0;
$matches = array();
$result = preg_match('/\\[id: ([0-9]+)\\]/', $form_state['values']['birth'][$parent], $matches);
if (!empty($matches[$result])) {
$id = $matches[$result];
}
// If an ID couldn't be extracted, throw an error.
if (empty($id)) {
form_set_error('birth][' . $parent, t('Could not load the @parent animal record. Make sure the animal asset ID is included. For example: "My animal [id: 123]"', array(
'@parent' => $parent,
)));
continue;
}
// Load the asset.
$asset = farm_asset_load($id);
// If the asset didn't load, throw an error.
if (empty($asset)) {
form_set_error('birth][' . $parent, t('Could not load the @parent animal record. Make sure the animal name and ID are correct.', array(
'@parent' => $parent,
)));
continue;
}
// Save the asset to the form state.
$form_state['storage'][$parent] = $asset;
}
}
// If both a mother and a father are specified, make sure they're different.
if (!empty($form_state['storage']['mother']) && !empty($form_state['storage']['father'])) {
if ($form_state['storage']['mother']->id == $form_state['storage']['father']->id) {
unset($form_state['storage']['father']);
form_set_error('birth][father', t('The mother and father cannot be the same animal.'));
}
}
// Iterate through the children.
foreach ($form_state['values']['birth']['child'] as $i => $child) {
// Make sure that either the name or tag ID is filled in.
if (empty($child['name']) && empty($child['tag_id'])) {
form_set_error('birth][child][' . $i . '][name', t('The child must have a name or tag ID.'));
}
}
}