I need to get some fields of a node edit form, for that purpose I use field_attach_form() to get the fields that I want with the value of the node.
Here's an example of the code of my form function:
function my_form($form, &$form_state, $node_id, $fields) {
$node = node_load($node_id);
$node_form = $form;
field_attach_form('node', $node, $node_form, $form_state);
foreach ($fields as $field) {
if ($field->show) {
$form[$field->name] = array('#type' => 'fieldset', '#title' => t('Field'), '#tree' => TRUE,);
$form[$field->name]['override'] = array('#type' => 'checkbox', '#title' => t('Override'),);
$form[$field->name][$field->name] = $node_form[$field->name];
$form[$field->name][$field->name]['#states'] = array(
'enabled' => array(
':input[name="' . $field->name . '[override]"]' => array('checked' => TRUE)
),
);
}
}
return $form;
}
I just wrap every field (that I want to show) in a fieldset with a checkbox.
One of the field is an image type, when field_attach_form loads the field, it shows the image thumbnail and the image file name perfectly, but when I click submit (without edit the field content) the value of the field is empty. In fact, the image field is required so the validation fails.
When I remove the previous image and upload a new one, it works perfectly. Am I missing something?
#states
if the field is disabled, when the form does the validation the field is empty.