You are here

function matrix_validate in Matrix field 6.2

Validation callback This checks that required fields are filled in. Calls form_error on elements which are not filled in

Parameters

$element The form element to be checked:

$form_state:

Return value

$element

1 string reference to 'matrix_validate'
matrix_elements in ./matrix.module
Implementation of hook_elements().

File

./matrix.module, line 809
Defines simple matrix field types.

Code

function matrix_validate($element, &$form_state) {
  if (isset($form_state['post'][$element['#parents'][0]])) {

    //ahah callback for a standard element
    $values = $form_state['post'][$element['#parents'][0]];

    //dodgy
    $a = 3;
  }
  elseif (isset($form_state['values'][$element['#parents'][0]][0])) {

    //cck element
    $values = $form_state['values'][$element['#parents'][0]][0];

    //dodgy
    $a = 2;
  }
  elseif (isset($form_state['values'][$element['#parents'][0]])) {

    //standard element
    $values = $form_state['values'][$element['#parents'][0]];

    //dodgy
    $a = 1;
  }
  foreach ($element as $row_key => $row) {
    if (is_numeric($row_key)) {

      //ignore all other properties
      foreach ($row as $col_key => $col) {
        if (is_numeric($col_key)) {

          //check for required cells
          if ($col['#required'] == TRUE && $values[$row_key][$col_key] == '') {
            form_error($element[$row_key][$col_key], t("Cell at %col x %row is required", array(
              '%row' => strip_tags($element['#first_col'][$row_key]),
              '%col' => strip_tags($element['#header'][$col_key + 1]),
            )));
          }

          //check for validation callback
          if (!empty($col['#validate'])) {
            if ($col['#validate'] == 'numeric' && !isnumeric($values[$row_key][$col_key])) {
              form_error($element[$row_key][$col_key], t("Cell at %col x %row must be numeric", array(
                '%row' => strip_tags($element['#first_col'][$row_key]),
                '%col' => strip_tags($element['#header'][$col_key + 1]),
              )));
            }
            if ($col['#validate'] == 'custom') {
              $custom_validation_result = module_invoke_all('matrixvalidate', $values[$row_key][$col_key]);
              foreach ($custom_validation_result as $custom_validation) {
                if ($custom_validation !== TRUE) {
                  form_error($element[$row_key][$col_key], t("%custom_validation at %col x %row must be numeric", array(
                    '%custom_validation' => check_plain($custom_validation),
                    '%row' => strip_tags($element['#first_col'][$row_key]),
                    '%col' => strip_tags($element['#header'][$col_key + 1]),
                  )));
                }
              }
            }
          }
        }
      }
    }
  }
  return $element;
}