[SOLVED] - How to bypass node_validate via hook_form_alter

Success?

I built a Drupal 7 site recently that uses commerce. On each product page is a registration form (think ubercarts+events). I was able to get all working very well, rigorously tested. First let me describe my setup, in case it helps someone else.

My Setup

To load an already existing event form, we have to use hook_node_view, where we look to see if the user already submitted a form. This is important because each product node allows a single user to purchase multiple attendee seats / tickets. So the name and phone is required. And, the user could “add another” via ajax.

Inside this function I am calling drupal_get_form($event_form_id, $node), where $node is either a new node (in case this is the first time the user is filling out the form) or an existing event node that has been referenced to the product, user, and order_id.

To load & modify the commerce, hook_node_presave is used, to check the status of the cart, the node and node reference are used and to update the cart as needed.

The Problem - Browser's Back Button

The problem you will run into, anytime a user has access to a node form already submitted, you can click the browsers back button immediately after saving the node, and upon resubmitting it you will get an error like so:

Drupal 4.7 – 6: This content has been modified by another user, changes cannot be saved.

Drupal 7: The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.

Why?

Because of this little code in node.module node_validate:

<?php
 
if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
   
form_set_error('changed', t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
  }
?>

In hook_node_view, I am calling: drupal_get_form($event_form_id, $node)

Where $node is either a new node or an existing one. If an existing node, I am using the original $node->changed value, strait from the node table, which is what node_validate() is checking against for errors.

To bypass node_validate check, you need to modify the form value for $node->changed as a value in the future.

How?

<?php
hook_form_alter
(&$form, &$form_state, $form_id) {
  if(
$form_id == 'node_form_id_to_bypass_node_validte') {
   
$form['changed']['#default_value'] = time() + 300; // gives the user 5 minutes to modify this form
 
}
}
?>

Now when the event form is submitted, $node->changed will use the value from $form['changed']['#default_value'] when node_validate checks for errors.

In the $form object from hook_form_alter, you have both $form['node']['changed'] and $form['changed']. This is confusing! If you change the time values for $form['node']['changed'] you will not be able to bypass the timestamp checks in node_validation. You have to modify $form['changed']['#default_value'].

Thanks dmitrig01 for guiding me in the right direction.

Happy Hacking!