5

I have site with a content type that has a large number of fields on it.

I would like to allow users to start to create nodes, but not have to fill out all of the fields in one sitting. Partially completed nodes don't have to be published, but they do need to be saved in the database.

Is is possible to save the node in an uncompleted state without the validation rules running. I am mainly interested in required fields not being required, unless the node is published.

When users finish populating nodes, the validation rules should run when the node is published.

Are there any ways to accomplish this workflow?

  • 5
    So you want to save first...and then validate?! Methinks that's the wrong way round...;) – Clive Nov 23 '12 at 9:34
  • 2
    I agree with @Clive regarding it perhaps being a bit backwards. I'd perhaps suggest running Clientside Validation, that should at least reduce some of the strain from the server side validation. It will still need to be validated server side, but if most of the data is valid it shouldn't take as long. – Chapabu Nov 23 '12 at 9:47
  • 4
    I could be miss-understanding, but isn't the point that for a highly complex and/or time-consuming form, it's nice for the editor to be able to save the form in a half-done state, to guard against data loss? Postpoing part/most/all of the validation to publication time does make sense in this case. – Letharion Nov 23 '12 at 10:04
  • 1
    @2-Stroker sounds like you should post an answer :) – Letharion Nov 23 '12 at 10:24
  • 3
    @Indrock Isn't there always? ;) – Letharion Nov 23 '12 at 11:16
3

Another option here is to add a custom validation function to force validation to run, and then also have a custom submit handler to clear any errors produces and save the node.

The code here is mostly taken from this question but this question and this post have also helped.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    switch($form_id) {
        case 'my_form_id':

            $form['actions']['draft'] = array(
                '#type' => 'submit',
                '#value' => t('Save draft'),
                '#weight' => '9',
                '#submit' => array('save_draft_submit'),
                '#validate' => array('save_draft_validate'),
                '#attributes' => array(
                    'class' => array('cancel'), // add the class cancel will skip any clientside validation
                ),
                //'limit_validation_errors' => array(), // this should be enough to skip validation however does not work for me
            );

            break;
    }
}

function save_draft_validate($form, &$form_state) {
    // Set as unpublished.
    $form_state['values']['status'] = 0;
    // Force validation.
    $errors = &drupal_static('form_set_error', NULL, TRUE);

}

function save_draft_submit($form, &$form_state) {
    // clear error messsages
    drupal_get_messages();
    // save the node
    node_form_submit($form, $form_state);
    $form_state['redirect'] = 'node/'.$form_state['nid'].'/edit';
}
2

Use the following answer at your own risk:

You need to remove the validation functions from the form. You will need to do a hook_form_alter and then remove the validate functions that are defined by the node module. You might need to inspect the $form variable to find out where the validation methods are being set and then unset them.

<?php

THEME_form_alter(&$form, &$form_state, $form_id) {
  // Only remove the validate functions when adding a form, not edditing.
  if ($form_id == 'FORM_ID') {
    unset($form['#validate']); 
  }
}

If you make sure the nodes are not published when this code runs and that you do not unset the validators when editing the node, you can pretty much assure that when you go to try publish the node the validators will run in the regular way and you can catch the errors at that point.

As mentioned in the comments, this is a strange thing to be doing and should be avoided, however if you are sure this is the correct course of action, this should help you.

  • 1
    I was hoping no one will post this answer :( – GoodSp33d Nov 23 '12 at 9:57
  • 1
    @2-Stroker Me too...but then again, he's not recommending it as such, just providing it as a proof of concept. I'd feel more comfortable if there was a large USE THIS AT YOUR OWN RISK banner at the top of this post though ;) – Clive Nov 23 '12 at 9:58
  • 1
    A warning has been added, I kind of agree with the notion that this should rarely be used. – Sam152 Nov 23 '12 at 10:04
  • 1
    This still lacks an explanation of what the risks are. I don't see that there would be any risks in this on most of the sites I've been involved in building, so either I'm just slow, or what risks this presents depends a lot on the use-case. – Letharion Nov 23 '12 at 11:14
  • 1
    When I mentioned validation I meant make sure required fields were not required. Submitted content should be checked to make sure it is not rouge. – Rodney Nov 23 '12 at 13:40

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.