Saving a unpublished node without validation

2018-10-19 09:17:17

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?

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.

THEME_form

  • 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.

    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

    2018-10-19 09:51:59
  • 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

    );

    2018-10-19 10:57:23