node_example_validate

Versions
4.6 – 7
node_example_validate(&$node)

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().

Code

developer/examples/node_example.module, line 162

<?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;
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.