In creating a custom node module, it is great that you can create a _validate function to check user input.
However, what would be *really* neat would be the ability to auto-correct (or auto format) some common user mistakes, rather than just giving them an error message.
For example, some of my modules requires the user to submit a URL. For consistency, I want these to always be in the format "www.domain.com/page.htm" and *not* include the leading "http://". Despite giving this field a description that says the "http://" should not be included, users will often just cut+paste a URL from their browser, thus including the "http://" bit that I don't want.
Sure, I can write a _validate function to check if the URL starts "http://" and return an error, but it would be much nicer to just "auto-correct" this simple mistake.
Until now I have done this by creating a custom function, and modified my hook_insert and hook_update functions to call it, something like this (very cut down for simplicity):
function mylib_strip_url($url) {
// strip leading "http://", if present
if (substr($url,0,7) == 'http://') {
$stripped_url = substr($url,7);
} else {
$stripped_url = $url;
}
return $stripped_url;
}
function mynode_insert($node) {
db_query("INSERT INTO {mytable} (nid, website) VALUES (%d, '%s')", $node->nid, mylib_strip_url($node->website));