I've just updated my site to 4.7. Everything seems to be working except I can't seem to change a fields value in the node_validation function of my custom modules.

In 4.6 under mymodule_validation I could change a value. If I wrote $node->color = 'red' red would be saved, or if there was an error, 'red' would be in the field when the form refreshes.

It doesn't work in 4.7. I can set errors, but when I change the value it has no effect.

Somebody please help

Thanks,
Sam

Comments

pwolanin’s picture

In my experience with 4.7, you need to use hook_submit, or hook_nodeapi($op='submit') to actually change the node values after validation, but before the data is saved.

in node_validate the form values are not passed in by reference, and cannot be changed directly. If you want to change form values at the validate stage, you need to use form_set_value

---
Work: BioRAFT

samcohen’s picture

Thank you for pointing out the form_set_value function. Currently, I'm getting around this by using the hook_nodeapi function, but that only works after submitting and doesn't fix values when the form has an error and refreshes. Plus you have to do it both on update and insert.

I've tried to make form_set_value work based on the instructions, and I can't figure it out.

Where should I use this function. Should it be within the node_validate function?

And let's say I want to change $node->title to strip_tags($node->title) inthe validation stage. (I know this isn't necessary, but it's an example)

Do you know how I would use the form_set_value function to do this.

Thanks,
Sam

pwolanin’s picture

there don't seem to be too many uses in the core code, but one is in search_form_validate:

function search_form_validate($form_id, $form_values, $form) {
  form_set_value($form['basic']['inline']['processed_keys'], trim($form_values['keys']));
} 

So, I think you would call this from within hook_validate instead of directly setting the node value as you did in 4.6.

---
Work: BioRAFT

Andrei Toutoukine’s picture

Where I supposed to find $form ?

pwolanin’s picture

In 4.7 the node_validate function is defined as:

function node_validate($node, $form = array()) 

and it calls hook_validate:

// Do node-type-specific validation checks.
  node_invoke($node, 'validate', $form);
  node_invoke_nodeapi($node, 'validate', $form); 

so, I think the correct definition for hook validate would be:

function hook_validate($node, $form = array()) 

I think the hook_validate docs are out of date (a number of docs are still lagging). I'll see if I can fix it, or you can work on it if you have CVS access.

---
Work: BioRAFT