Forums

validating; all fields must have different value

wiva 14 Sep, 2014
A have a form with 10 dropdown boxes. All boxes are based on the same database table.
The value of alle fields must be different on submit.

How can I validate these fields with chronoforms v4 before DB Save (to another DB table).
GreyHead 14 Sep, 2014
Hi wiva,

You'll need to add some Serverside validation before the DB Save using PHP to check that the values are unique. Here's one way that might work:
<?php
// an array of drop-down names
$dropdowns = array(
  'dropdown01',
  'dropdown02',
  . . .
  'dropdown09',
  'dropdown10',
);
$results = array();
foreach ( $dropdowns as $v ) {
  if ( !isset($form->data[$v]) || !$form->data[$v] ) {
    $form->validation_errors[$v] = "Please select a value for {$v}";
    return false;
  }
  $results[] = $v;
}
$results = array_unique($results);
if ( count($results) < 10 ) {
  $form->validation_errors[$v] = "Please select ten different options";
  return false;
}  
?>
Or you could do something with JavaScript to hide selected options from the remaining drop-downs . . .

Bob
wiva 21 Sep, 2014
I added this server side validation and an Event Loop in the OnFail box, but it is not validating right for me.
I think, there is no return false; if there are not ten different values in the array.
What i'm doing wrong.

<?php
// an array of drop-down names
$dropdowns = array(
  'renner1',
  'renner2',
  'renner3',
  'renner4',
  'renner5',
  'renner6',
  'renner7',
  'renner8', 
  'renner9',
  'renner10',
);
$results = array();
foreach ( $dropdowns as $v ) {
  if ( !isset($form->data[$v]) || !$form->data[$v] ) {
    $form->validation_errors[$v] = "Please select a value for {$v}";
    return fals;
  }
  $results[] = $v;
}
$results = array_unique($results);
if ( count($results) < 10 ) {
  $form->validation_errors[$v] = "Please select ten different options";
  return false;
}  
?>


Debugger

Data Array:
Array
(
[option] => com_chronoforms
[tmpl] => component
[chronoform] => individueel_formulier
[event] => submit
[renner1] => 100
[renner2] => 2
[renner3] => 123
[renner4] => 9
[renner5] => 119
[renner6] => 9
[renner7] => 4
[renner8] => 1
[renner9] => 3
[renner10] => 13
[tourwinnaar] => 19
[cf_id] => 5
[input_submit_13] => Verzenden
[3c3aa24fef58f2d9a6906036fceae50e] => 1
)
Validation Errors:
Array
(
)
GreyHead 21 Sep, 2014
Hi wiva,

Sorry, I missed an e in false
    $form->validation_errors[$v] = "Please select a value for {$v}";
    return false;

Bob
wiva 21 Sep, 2014
Hi Bob,

Yes i saw that and i corrected it,
but the the problem is stil there.

Wim
GreyHead 21 Sep, 2014
Hi wiva,

The code looks OK otherwise - I suggest that you add some lines of debug output to see exactly what is happening.

Bob
wiva 21 Sep, 2014
Hi Bob,

here's a screenshot
I placed the Debugger in OnSubmit under the Custom Server Site Validation.

[attachment=0]Schermafdruk van 2014-09-21 13:45:20.png[/attachment]

Wim
wiva 28 Sep, 2014
Hi Bob,

I think I solved the problem with this code.
A part of your suggested code is deleted and I added @form->data to the dropdowns.
Now the from is returning the error message correct and is not saved to the database. (in case of duplicate values)
In case of 10 different values the form is saved

Would you please check this for falal errors
Thanks for you help

<?php 
// an array of drop-down names
$dropdowns = array(
  $form->data['renner1'],
  $form->data['renner2'],
  $form->data['renner3'],
  $form->data['renner4'],
  $form->data['renner5'],
  $form->data['renner6'],
  $form->data['renner7'],
  $form->data['renner8'],
  $form->data['renner9'],
  $form->data['renner10'],
);

$dropdowns = array_unique($dropdowns);
if ( count($dropdowns) < 10 ) {
  $form->validation_errors[$v] = "Selecteer tien verschillende renners";
  return false;
}  
?>
GreyHead 28 Sep, 2014
Answer
1 Likes
Hi wiva,

Well done - if the code works then there are no fatal errors :-)

Bob
This topic is locked and no more replies can be posted.