Forums

Create condition field A empty - Field B and C values different from 0

Repitol 12 Mar, 2015
Hi,
In my form, user registers for an activity
at field A, he writes the names of the participants
at field B : number of adults
at field C : number of children

I see some users filed the field A ( on or two names) but let Field B and C empty
so the result is wrong
I tried to add a Custom Code Action at On Load Event to verify that field B and C are not to default value 0 when field A is not empty
but I'm not sure about the code nor the place it has to be set 😶
here is what I tried
<?php
if (isset($('participants_soiree_restaurant') AND $('total_soiree_restaurant_adultes') == "0" AND $('total_soiree_restaurant_enfants') == "0"
{
    echo "<script>alert(\"Vérifiez le nombre de participants à la soirée restaurant\")</script>";
}
?>
GreyHead 12 Mar, 2015
Hi Repitol,

ChronoForms v4 has a Custom Serverside validation action that you can use for this. Please see this FAQ

Also the form data is stored in the $form->data array so you need to use e.g. $form->data['participants_soiree_restaurant'] to access it.

Bob
Repitol 12 Mar, 2015
Do I have to replace form with the name of my form ?
e.g. : $myform-name->data['participants_soiree_restaurant']
GreyHead 12 Mar, 2015
Hi Repitol,

No

Bob
Repitol 13 Mar, 2015
Hi Bob,
I'm a real dumb newbie with coding (with writing correctly in English too...)
even with the FAQ example, I don't succed in writing the code correctly so that if field $form->data['participants_soiree_restaurant'] is not empty AND $form->data['total_soiree_restaurant_adultes'] == 0 AND $form->data['total_restaurant_enfants'] == 0
an alert is shown
some users edit names in field 'participants_soiree_restaurant' but they let total fields at 0, then the result is wrong.
Could you please give me a valid code ? please please please !!! :?
fasenderos 16 Mar, 2015
If I understand you need to know how many people are adults or childrens, so if user inserts 2 names there are 3 possibilities: 2 adults or 2 childrens or 1&1
First put a bold text on your form like this: "Please insert partecipants name separated by comma ex: John Doe, Kevin Smith, Buffalo Bill
Than use the custom server side validation to check: 1) Users list is not empty 2) number of names = adults+childrens

<?php
//count number of names separated by comma
$users = substr_count($form->data['participants_soiree_restaurant'], ',') + 1;
//number of adults
$adults = $form->data['total_soiree_restaurant_adultes'];
//number of childrens
$childrens = $form->data['total_restaurant_enfants'] ;
$error_count = 0;
if ( $form->data['participants_soiree_restaurant'] == '' ) {
  $form->validation_errors['participants_soiree_restaurant'] = 'This field can\'t be empty';
  $error_count++;
} elseif ( $users != $adults + $childrens ) {
  $form->validation_errors['total_soiree_restaurant_adultes'] = 'Number of adults and childrens must match the names in users list';
$form->validation_errors['total_soiree_restaurant_adultes'] = 'Number of adults and childrens must match the names in users list';
  $error_count++;
}
if ( $error_count > 0) {
  return false;
}
?>
Repitol 16 Mar, 2015
Hi Bob,
thanks for the code. Unfortunately it doesn't work.
I put the custom server side validation code within the ON Submit event, just after Handle Arrays event and before DB save event.
I tested with the names separated with comas but 0 in total adults and 0 in total children.
I could submit the form without problem.
Maybe I didn't put the code at the right place ?

Repitol
fasenderos 16 Mar, 2015
Hi Repitol,
first of all I'm not Bob🙂
Second you really need Handle Arrays??
Put the Custom Server Side validation in the first place of the On Submit event, and put an Event Loop in the On fail event of the Custom server side validation
Andrea
Repitol 16 Mar, 2015
Oups sorry Fasenderos ! I didn't pay attention.
When I delete Handle Arrays, I loose everything in the On Submit event !
I can't delete it
Repitol 16 Mar, 2015
When I put the Custom Server Side validation in the first place of the On Submit event, and put an Event Loop in the On fail event of the Custom server side validation it seems to work but I don't have the error message displayed
just a slide to the incorrect field.
Repitol 16 Mar, 2015
Ok
I've seen you made a little mistake on the code you submit
twice the same line ($form->validation_errors['total_soiree_restaurant_adultes'])
I replaced the second by $form->validation_errors['total__restaurant_enfants']
and it seems to work now
Could you please confirm I did the right thing ?
Thanks

Repitol
fasenderos 16 Mar, 2015
No problems.
Restore the Handel Arrays, maybe after the Cusom Server Side. If didn't work, place the Handle Arrays before the Custom Server Side.
However the code I posted before for the Custom Server Side should work, I attach an example form (without Handle Arrays).
Andrea
fasenderos 16 Mar, 2015
Oops I did not see your new messages.
You are right, I made a mistake. here the right code


<?php
//count number of names separated by comma
$users = substr_count($form->data['participants_soiree_restaurant'], ',') + 1;
//number of adults
$adults = $form->data['total_soiree_restaurant_adultes'];
//number of childrens
$childrens = $form->data['total_restaurant_enfants'] ;
$error_count = 0;
if ( $users == '' ) {
  $form->validation_errors['participants_soiree_restaurant'] = 'This field can\'t be empty';
  $error_count++;
} elseif ( $users != $adults + $childrens ) {
  $form->validation_errors['total_soiree_restaurant_adultes'] = 'Number of adults and childrens must match the number of users in the list';
$form->validation_errors['total_restaurant_enfants'] = 'Number of adults and childrens must match the number of users in the list';
  $error_count++;
}
if ( $error_count > 0) {
  return false;
}
?>
Repitol 16 Mar, 2015
Yeah !! I begin to understand coding !! 😀
thanks a lot for your help - I wouldn't be able to find the solution by myself.
Is it possible to have count names separated by line more than by comma ?
that's to say one name by line ?
fasenderos 16 Mar, 2015
1 Likes

Is it possible to have count names separated by line more than by comma ?


Replace this
$users = substr_count($form->data['participants_soiree_restaurant'], ',') + 1;

with this
$users = substr_count($form->data['participants_soiree_restaurant'], "\n");
Repitol 16 Mar, 2015
Thanks !!!
😀
Repitol 17 Mar, 2015
Hi,
I have a problem with the code
If the field 'participants_soiree_restaurant' is empty => alert "this field can't be empty"
but if user edit "none" (no participants) it is considered as a name of one participant so total can't be 0

Should I delete that ?
 $form->validation_errors['participants_soiree_restaurant'] = 'This field can\'t be empty';

Thanks
GreyHead 17 Mar, 2015
Hi Repitol,

If it is a numeric field then I suggest that you validate it as an integer.

Bob
Repitol 17 Mar, 2015
Bob, I don't know how to manage that. I'm brainless for coding.
What I know is just how to copy/paste the code Fasenderos suggested (shame on me)
Alert is on fields with "Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit"

    <?php
    //count number of names separated by comma
    $users = substr_count($form->data['participants_soiree_restaurant'], ',') + 1;
    //number of adults
    $adults = $form->data['total_soiree_restaurant_adultes'];
    //number of childrens
    $childrens = $form->data['total_restaurant_enfants'] ;
    $error_count = 0;
    if ( $form->data['participants_soiree_restaurant'] == '' ) {
      $form->validation_errors['participants_soiree_restaurant'] = 'Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit';
      $error_count++;
    } elseif ( $users != $adults + $childrens ) {
      $form->validation_errors['total_soiree_restaurant_adultes'] = 'ATTENTION ! Restaurant - Le nombre total doit correspondre au nombre d\'inscrits';
    $form->validation_errors['total_restaurant_enfants'] = 'ATTENTION ! Restaurant - Le nombre total doit correspondre au nombre d\'inscrits';
      $error_count++;
    }
    if ( $error_count > 0) {
      return false;
    }
    
    
    $users = substr_count($form->data['participants_coupe_ski'], ',') + 1;
    //number of adults
    $adults = $form->data['total_coupe_ski_adultes'];
    //number of childrens
    $childrens = $form->data['total_coupe_ski_enfants'] ;
    $error_count = 0;
    if ( $form->data['participants_coupe_ski'] == '' ) {
      $form->validation_errors['participants_coupe_ski'] = 'Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit';
      $error_count++;
    } elseif ( $users != $adults + $childrens ) {
      $form->validation_errors['total_coupe_ski_adultes'] = 'ATTENTION ! Coupe ski - Le nombre total doit correspondre au nombre d\'inscrits';
    $form->validation_errors['total_coupe_ski_enfants'] = 'ATTENTION ! Coupe ski - Le nombre total doit correspondre au nombre d\'inscrits';
      $error_count++;
    }
    if ( $error_count > 0) {
      return false;
    }
    
    $users = substr_count($form->data['participants_ski_seul'], ',') + 1;
    //number of adults
    $adults = $form->data['total_ski_seul_adultes'];
    //number of childrens
    $childrens = $form->data['total_ski_seul_enfants'] ;
    $error_count = 0;
    if ( $form->data['participants_ski_seul'] == '' ) {
      $form->validation_errors['participants_ski_seul'] = 'Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit';
      $error_count++;
    } elseif ( $users != $adults + $childrens ) {
      $form->validation_errors['total_ski_seul_adultes'] = 'ATTENTION ! Ski - Le nombre total doit correspondre au nombre d\'inscrits';
    $form->validation_errors['total_ski_seul_enfants'] = 'ATTENTION ! Ski - Le nombre total doit correspondre au nombre d\'inscrits';
      $error_count++;
    }
    if ( $error_count > 0) {
      return false;
    }

$users = substr_count($form->data['participants_buffet'], ',') + 1;
    //number of adults
    $adults = $form->data['total_buffet_adultes'];
    //number of childrens
    $childrens = $form->data['total_buffet_enfants'] ;
    $error_count = 0;
    if ( $form->data['participants_buffet'] == '' ) {
      $form->validation_errors['participants_buffet'] = 'Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit';
      $error_count++;
    } elseif ( $users != $adults + $childrens ) {
      $form->validation_errors['total_buffet_adultes'] = 'ATTENTION ! Buffet - Le nombre total doit correspondre au nombre d\'inscrits';
    $form->validation_errors['total_ski_seul_enfants'] = 'ATTENTION ! Buffet - Le nombre total doit correspondre au nombre d\'inscrits';
      $error_count++;
    }
    if ( $error_count > 0) {
      return false;
    }


$users = substr_count($form->data['participants_soiree_restau'], ',') + 1;
    //number of adults
    $adults = $form->data['total_buffet_adultes'];
    //number of childrens
    $childrens = $form->data['total_buffet_enfants'] ;
    $error_count = 0;
    if ( $form->data['participants_soiree_restau'] == '' ) {
      $form->validation_errors['participants_soiree_restau'] = 'Ce champ ne doit pas rester vide, indiquer aucun si personne n\'est inscrit';
      $error_count++;
    } elseif ( $users != $adults + $childrens ) {
      $form->validation_errors['total_soiree_restau_adultes'] = 'ATTENTION ! Restaurant samedi - Le nombre total doit correspondre au nombre d\'inscrits';
    $form->validation_errors['total_soiree_restau_enfants'] = 'ATTENTION ! Restaurant samedi - Le nombre total doit correspondre au nombre d\'inscrits';
      $error_count++;
    }
    if ( $error_count > 0) {
      return false;
    }
    
    ?>
GreyHead 17 Mar, 2015
Hi Repitol,

In CFv4 you can set client side validation from the Preview tab in the Form Editor, open the Text Box element settings, go to the Validation tab and check Digit. No coding required.

Similarly you an add ServerSide validation with the AutoServerSide Validation action in the Events tab.

Bob
Repitol 17 Mar, 2015
The problem is due to the server side validation needed on the form
I don't know if the validation tab can change that (? - tell me)
Some users can register 2 names in "participants" field but let 0 in "total" fields (where the calculation for total to be paid is made)
if I configure validation tab and check Digit, how can a user register his name ?
what is needed : you write a name, you can't edit 0 in the field 'total-adults' and 'total-children"

Or I have not understood your solution...
This topic is locked and no more replies can be posted.