A common problem we're running into with machine names in D7 is that we have to add validation for certain pre-defined 'reservered' names that would conflict with system paths. I think it would be nice if we could just add a #reserved_values array property to any form API input element that would validate if the form element's value is not one of the reserved values.
For example:
function taxonomy_form_vocabulary($form, &$form_state, $edit = array()) {
...
$form['machine_name'] = array(
'#type' => 'machine_name',
'#default_value' => $vocabulary->machine_name,
'#maxlength' => 255,
'#machine_name' => array(
'exists' => 'taxonomy_vocabulary_machine_name_load',
),
);
...
}
function taxonomy_form_vocabulary_validate($form, &$form_state) {
// During the deletion there is no 'machine_name' key
if (isset($form_state['values']['machine_name'])) {
// Do not allow machine names to conflict with taxonomy path arguments.
$machine_name = $form_state['values']['machine_name'];
$disallowed = array('add', 'list');
if (in_array($machine_name, $disallowed)) {
form_set_error('machine_name', t('The machine-readable name cannot be "add" or "list".'));
}
}
}
Becomes simply:
function taxonomy_form_vocabulary($form, &$form_state, $edit = array()) {
...
$form['machine_name'] = array(
'#type' => 'machine_name',
'#default_value' => $vocabulary->machine_name,
'#maxlength' => 255,
'#machine_name' => array(
'exists' => 'taxonomy_vocabulary_machine_name_load',
),
'#reserved_values' => array('list', 'add'),
);
...
}