1

I have created a custom validate function for specific node form.

But validate rules are different between edit a current node or add a new node modes.

Now, I want to know, is there any way that we understand whether we are in edit mode or create mode in form_validate function ?

  • In which version you are trying to adding your custom form... D7 or D8? – Manav Jul 9 '18 at 18:18
  • I apologize, I forgot to write drupal version . D7 – Mehrdad201 Jul 9 '18 at 19:54
2

A new node will not have a node ID.

So you can check the form values or the original form for that to determine whether it is a new node or not.

For example:

if (empty($form_state['values']['nid'])) {
  // This is a new node.
}
else {
  // This is an existing node.
}
|improve this answer|||||
  • Thanks a lot rooby. I used your suggestion and it worked. – Mehrdad201 Jul 10 '18 at 10:16
1

Rooby solution is good, but I usually check the presence of "nid" property in $form['#node']

function my_module_form_validate_func(&$form, &$form_state) {
  if (!isset($form['#node']->nid)) {
    //new node
  }
  else {
    //existing node
  }
}
|improve this answer|||||
  • Yep, there are at least 4 ways I can think of to check in various places in $form and $form_state. All valid. – rooby Jul 10 '18 at 11:25
  • Thanks for you attention. I have some other questions in different issues in drupal 7. if you can please visit drupal.stackexchange.com/questions/264230/… – Mehrdad201 Jul 11 '18 at 6:31

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.