| Versions | |
|---|---|
| 4.7 – 5 | node_example_validate(&$node) |
| 6 | node_example_validate($node, &$form) |
Implementation of hook_validate().
Our "quantity" field requires a number to be entered. This hook lets us ensure that the user entered an appropriate value before we try inserting anything into the database.
Errors should be signaled with form_set_error().
developer/
<?php
function node_example_validate(&$node) {
if ($node->quantity) {
if (!is_numeric($node->quantity)) {
form_set_error('quantity', t('The quantity must be a number.'));
}
}
else {
// Let an empty field mean "zero."
$node->quantity = 0;
}
}
?>