I'd like to do something like the following:

Let's say that I intended to allow users to input their Social Security Number, in the format 123-45-6789, which is a field in a "Person" content type. When saving to the DB though, I'd like to store this SSN as an integer. So, before the form gets saved, I want to remove the dashes. How can I do this? I can't find an appropriate hook that will allow me to alter the data BEFORE it is submitted. Adding a submit handler would not work, I believe, since this additional handler would trigger after the data is already saved.

EDIT: I'd now like to change this value before the individual element's #element_validate function is used. I tried changing the callback of this element's #element_validate function, but then the actual value isn't being changed before being submitted to the database, and I get an insertion error.

EDIT2: I'm attempting to use value_callback, but still having the same issue - either I'm using this incorrectly, or this is not firing before the element's validator. Either way, the result is that rather than attempting to insert "123456789" into the database, "123-45-6789" is attempted, and since this is an integer field, insertion fails.


You may be interested in form_set_value(). You would use this within the form validation function. There are examples of its use posted below the API documentation in the comments if you need further clarification.

For help creating a validation function, see the Form API Quickstart Guide (D6) or How to use the Form API (D7).

  • This was a great start! I'm now running into one additional problem - the data that I need to change is in a format that would be invalid if left unchanged. So, to use the SSN example again - if I want to store "123-45-6789" as an integer, I must remove all of the dashes. BUT, before my function which uses form_set_value is called, the element's validator triggers, and sees that "123-45-6789" is not an integer. How can I get around this? – rybosome Nov 9 '11 at 21:45
  • You can use a value callback, see my answer – Berdir Nov 10 '11 at 6:20

Technically, to alter it before it is submitted, you'd need to do that in the client with JavaScript. However, what you are looking for I think is altering it before it is saved.

For nodes, you can implement hook_node_presave($node) where you can change any properties of the node object right before it is saved.

Form fields also support a #value_callback, which allows you to alter the actually submitted value and return something else/changed.

  • Any chance you could give me an example of it's usage? I'm attempting to use it, and I'm still having the issue that the element's validation is either not taking the value from this value callback, or it's firing before the value callback. I'll post the updated code in my question. – rybosome Nov 10 '11 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.