The question might look similar to How to form_alter fields attached to an unlimited field collection? but I am more concerned about "values not saved after custom validation"
I have a field collection "field_assignments" consists of:
field_check_user: A user reference field
field_assigned_booklets: An integer field
field_total_cost: A hidden field (used in validation handler for calculation)
I have content type "marking" having this field collection (unlimited values) along with a field_rate. I have alter node editing form and defined a custom validation which is working fine. The problem is that I cannot update the field collection item "field_total_cost" value in custom validation function. The validation function is
function _additional_validate($form, &$form_state) {
// If rate is set to 100 and assigned booklets are 5; this should result 500
$tc = $form_state['values']['field_assignments']['und'][0]
['field_assigned_booklets']['und'][0]['value'] * $form_state['values']['field_rate']['und'][0]['value'];
$form_state['values']['field_assignments']['und'][0]['field_total_cost']['und'][0]['value'] = $tc;
}
If I dpm $form_state, I can see that the ['field_total_cost']['und'][0]['value'] is updated (see image below) but when the node is submitted, it does not show that value in node view and does not save that value in the database. How can I save a value in field collection field?
I tried form_set_value() but couldn't understand how to set value for compounded fields (field collection in this case, I'll be thankful if someone can show how can I construct that "#parent" stuff with these fields). The code with form_set_value() I tried:
// testing on a single field - works perfectly
form_set_value($form['field_rate']['und'][0]['value'], $tc, $form_state);
// does not update the value
form_set_value($form['field_assignments']['und'][0]['field_total_cost']['und'][0]['value'], $tc, $form_state);
P.S: I have written 'und' instead LANGUAGE_NONE for ease