My Color Picker sample field module performs both field and widget level validation, and they do two different things: The widget validates that each r, g, and b field is 0 to 255, and the field validates that the color has less green than 255 (because "green is expensive"), simulating a field setting that the widget does not necessarily know about.
When I submit a node form with both a widget and field validation error (red of 256, green of 255), I get both errors; field validation is being called even though widget validation failed. That's because of the way node.module is written:
function node_form_validate($form, &$form_state) {
$node = $form_state['values'];
node_validate($node, $form);
// Field validation. Requires access to $form_state, so this cannot be
// done in node_validate() as it currently exists.
$node = (object)$node;
field_attach_form_validate('node', $node, $form, $form_state);
}
When widget validation fails, the widget may not properly create the data elements that the field needs. Thus it isn't clear to me that it is safe/reasonable to invoke field validation.
Should we declare that it is best practice not to call field_attach_validate() if FAPI validation fails? If so, we should document that, then turn this into a node.module bug report, and probably user.module and comment.module too (I haven't checked).
Comments
Comment #2
bleen commentedWouldn't this lead to just as many situations where I submit a form; get an error (via form validation) because I left required field FOO blank; fix the error by entering a value for FOO and resubmit. Now I get an error (via field validate) because I enter an improper value in field BAR. Would've been great if I got that message the first time so I do not need to resubmit the form *again*.
I feel as though this is one of those situations that could easily suck in either circumstance and there is no way to know in the generic case which is the best policy...
Comment #3
attiks commentedI created #1845546: Implement validation for the TypedData API to try to solve this on all levels, since there are only 10 days left, we need to figure out what needs to be done before feature freeze and what can be done later.
Comment #12
catch#1845546: Implement validation for the TypedData API is long fixed, marking this as duplicate.