I am trying to update a value during the validate phase of a node-form, i.e. if form: When the custom validation error is fired, justI empty one of the fields.
for the last 30 hours I am trying to make sense of the drupal api, but I am giving up. I just do not seem to get the idea whatuse the different values meanfollowing code.
the function is: form_set_value($element, $value, &$form_state)
function eventcheck_node_validate($node, $form, &$form_state) {
if ($node->type == 'article') {
if ((array_key_exists('endpoints',$node->field_event_choose['und'][0])) && (($node->field_event_title['und'][0]['value']<>'') || (isset($node->field_event['und'][0]['value'])))) {
$form_set_value(???);
$form_set_error('event_checker_group',t('Error message'));
}
}
}
now iI understand that the last valueparameter for form_set_value()
is simply the $form_state, that I am having throughsame $form_state
the validate functionvalidation handler gets. but what about $elementWhat are the $element
and $valuethe $value
?
I was trying a lot and apparently the desired value resides in $form['field_name']['und'][0]['value']['#value']$form['field_name']['und'][0]['value']['#value']
and only there.
but when When I am trying form_set_value($form['field_name']['und'][0]['value']['#value'],'foo'try form_set_value($form['field_name']['und'][0]['value']['#value'],'foo',$form_state)
,$form_state) it raises
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()
and when I am trying:
$newvalue = $form['field_name']['und'][0]['value'];
$newvalue['#value']='foo';
form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);
it raises:
Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()
Thanks for any help!
[EDIT]
My complete function is:get this error.
function hook_node_validate($node,$form,&$form_state) {
if(fancy_stuff){
form_set_value();
form_set_error();
}
}
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()
[EDIT2]I also tried the following code.
//calls when node is being created
function eventcheck_node_validate($node,$form,&$form_state) {
//checks if the node type is applicable
if($node -> type=='article') {
//checks if field 'field_event_choose' AND either 'field_event_title'
//or 'field_event' is supplied, if so, fire form_set_value() and form_set_error()
if ((array_key_exists('endpoints',$node -> field_event_choose['und'][0])) &&
(($node -> field_event_title['und'][0]['value']<>'') || (isset($node ->
field_event['und'][0]['value'])))) {
//fire away
$form_set_value(???);
$form_set_error('event_checker_group',t('sorry blabla'));
}
}
}
$newvalue = $form['field_name']['und'][0]['value'];
$newvalue['#value']='foo';
form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);
I hope that helpsget this error.
Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()