1

I have a complex for that for various reasons I am validating in the _submit hook instead of the _validate. This is working great with the exception that the error class isn't getting added to the form elements.

Is there something simple I am missing or do I need to use form_get_errors and set the classes myself?

(The reason I am doing validation in submit is because I want to save the form data regardless of if it is valid or not.)

3
  • As you are referring to the hook hook_submit(), is the code for a module implementing a content type?
    – apaderno
    Mar 25, 2011 at 21:49
  • If you are saving the data even if they are not valid, then you are not really interested in validating them; in this case, why would you want to show any error to the users, if you are going to save the data?
    – apaderno
    Mar 25, 2011 at 22:00
  • I want to save the data but also make the user aware that there is an error in the entry. It is that simple.
    – Icode4food
    Mar 26, 2011 at 1:32

1 Answer 1

0

The easiest thing to do, would be to save the form data in the validation step if the form has errors.

function form_validate(&$form, &$form_state) {
  // Validate
  if (form_get_errors()) {
    // This code is only run if the form has errors.
    // Save the form data
    db_query("...");
  }
}

Then you could raise the errors and let drupal do it's thing, and use the submit function for it's intended use: handle valid form data.

If you need to process the data this can be done in the validation step as well, as the info in the $form_state array will be carried to the form submit handler.

3
  • The reason I have my save logic in the _submit instead of the _validate is that under certain circumstances I use $form['#redirect'] = 'my/special/path';. I don't believe I can redirect in _validate.
    – Icode4food
    Mar 25, 2011 at 21:06
  • @Icode: You only save during validate when the form doesn't validate. In this case I figure you want to redisplay the form with errors. You should have the save logic in your submit handler, but if you do want to save data when the form doesn't validate, you should do that in the validate handler.
    – googletorp
    Mar 25, 2011 at 21:32
  • I want to save the data, no matter if it validates or not. But I also want the user to be notified that things aren't up to par yet. Currently I am doing this all in the _submit handler for the form. The problem is that the error class isn't getting set on the for fields.
    – Icode4food
    Mar 26, 2011 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.