function country_validate in Countries 8
Same name and namespace in other branches
- 7.2 countries.module \country_validate()
Validates a country.
Validates and normalises the core country properties. Note that this requires the cid to test updates valids correctly.
if (country_validate($country)) {
country_save($country);
}
else {
foreach ($country->_errors as $property => $error_message) {
form_set_error($property, $error_message);
}
}
Parameters
object $country: The country object that is about to be saved.
Return value
bool TRUE if there were no errors, FALSE otherwise.
If FALSE, the $country->_errors will store an array of error messages indexed by the country property that caused the error.
5 calls to country_validate()
- CountriesCacheUnitTest::testCache in tests/
countries.test - CountriesCRUDUnitTest::testCountriesCRUD in tests/
countries.test - This test that the country property lookup is working.
- CountriesFunctionsUnitTest::testCountriesLock in tests/
countries.test - This test that core countries can not be deleted while user ones are.
- countries_admin_form_validate in ./
countries.admin.inc - Validate country form submissions.
- countries_import_bulk_import_form_submit in modules/
countries_import/ countries_import.admin.inc
File
- ./
countries.module, line 336 - Defines the field and entity information for countries.
Code
function country_validate(&$country) {
module_load_include('admin.inc', 'countries');
$errors = array();
$defaults = country_create();
foreach (countries_core_properties() as $key => $label) {
$country->{$key} = isset($country->{$key}) ? trim($country->{$key}) : $defaults->{$key};
if ($length = drupal_strlen($country->{$key})) {
if ($error = countries_property_invalid($key, $country->{$key})) {
$errors[$key] = $error;
}
}
elseif ($key == 'name') {
$errors['name'] = t('Name is required');
}
elseif ($key == 'iso2') {
$errors['iso2'] = t('ISO alpha-2 field is required');
}
}
// Only continue checking if there are no errors with the ISO alpha-2 value,
// as this is required for the other validation functions.
if (empty($errors['iso2'])) {
// If there is no cid, check that the ISO alpha-2 is not being used.
if (empty($country->cid) && ($existing = country_load($country->iso2))) {
$errors['iso2'] = t('The ISO alpha-2 code is already in use.');
}
else {
// Only record duplicate errors if there is no other errors on the field.
if ($duplicates = country_duplicate_field_check($country)) {
foreach ($duplicates as $key => $error) {
if (empty($errors[$key])) {
$errors[$key] = $error;
}
}
}
}
}
if (!empty($errors)) {
$country->_errors = $errors;
}
return empty($errors);
}