I feel like I'm missing something major here.
How do I use this module in conjunction with custom validation functions implemented using #element validate? Is the process similar to the "Form API Validation" code samples?

Thanks in advance.

Comments

attiks’s picture

It's basically the same, you have to attach it to the js_rules on the server and provide a custom javascript function (or use an existing) to do the validation in javascript. If you get stuck let me know.

Anonymous’s picture

Thank you for responding. I'm still a bit stuck. Below is a simplified example of what I'm trying to do. My validation (tied to the parent element) compares the two child elements and sets an error using form_set_error(). It doesn't seem like hook_clientside_validation_rule_alter() is even involved in this process.

In the form builder:

$form['container'] = array(
  '#type' => 'item',
  '#title' => t('Container'),
  '#tree' => TRUE,
  '#element_validate' => array('_container_validate'),
  '#prefix' => '<div class="my-container">',
  '#suffix' => '</div>',
);
$form['container']['textfield_one'] = array(
  '#type' => 'textfield',
  '#title' => t('Textfield 1'),
  '#tree' => TRUE,
);
$form['container']['textfield_two'] = array(
  '#type' => 'textfield',
  '#title' => t('Textfield 2'),
  '#tree' => TRUE,
);

Validation function:

function _container_validate($element, &$form_sate, $form) {
  if ($element['textfield_one']['#value'] == $element['textfield_two']['#value']) {
    form_set_error('container', t("The two fields cannot have the same value."));
  }
}

EDIT: #1410346: hook_clientside_validation_rule_alter does not work looks relevant. Much like OP, it seems that I'm using the module incorrectly, by trying to implement hook_clientside_validation_rule_alter() without using FAPI Validation or Field Validation. This sort of brings me back to the original question, can I use this module given the above scenario?

attiks’s picture

Category: support » feature

#1410346 is the same, I'm changing this into a feature request and will see how we can solve this

attiks’s picture

Assigned: Unassigned » jelle_s
jelle_s’s picture

Status: Active » Fixed

This is fixed in the latest dev version. Below is example code of how you would do this:

/**
 * Implements hook_clientside_validation_rule_alter().
 */
function cv_test_clientside_validation_rule_alter(&$js_rules, $element, $context) {
  switch($context['type']) {
    case 'element_validate':
      if (in_array('_container_validate', $context['functions'])) {
        // This function is already provided in clientside_validation.module
        _clientside_validation_set_not_equal(
          $element['textfield_one']['#name'], // the name of the first element
          $element['textfield_one']['#title'], // the title of the first element
          array(
            'form_key' => $element['textfield_two']['#name'],
            'name' => $element['textfield_two']['#title']
          ), // an array with the name and title of the second element
          $js_rules, // the js_rules array which will be passed by reference
          t("The two fields cannot have the same value") // The error message to display
        );
      }
      break;
  }
}

/**
 * Implements hook_menu().
 */
function cv_test_menu() {
  return array(
    'cv_test_form' => array(
      'title' => t('Test Clientside Validation'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('cv_test_form'),
      'access arguments' => array('access content'),
    ),
  );
}

/**
 * Form callback.
 */
function cv_test_form() {
  $form = array();
  $form['container'] = array(
    '#type' => 'item',
    '#title' => t('Container'),
    '#tree' => TRUE,
    '#element_validate' => array('_container_validate'),
    '#prefix' => '<div class="my-container">',
    '#suffix' => '</div>',
  );
  $form['container']['textfield_one'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield 1'),
    '#tree' => TRUE,
  );
  $form['container']['textfield_two'] = array(
    '#type' => 'textfield',
    '#title' => t('Textfield 2'),
    '#tree' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

/**
 * Validation callback
 */
function _container_validate($element, &$form_state, $form) {
  if ($form_state['values']['container']['textfield_one'] == $form_state['values']['container']['textfield_two']) {
    form_set_error('container', t("The two fields cannot have the same value."));
  }
}

jelle_s’s picture

Backported to 6.x as well

Anonymous’s picture

Wow, thank you so very much!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.