V5 event switcher help

Nuvelle 17 Jul, 2015
Hi,

I need some help with the V5 event switcher.

I am remaking my forms from V4 to V5 and the custom serverside validation is no longer an option.

I have an event switcher with the following in:

<?php
include (JPATH_SITE.'/components/com_chronoforms5/badwords.php');
return $isvalid;
?>


The referenced badword file is external as I have approximately 10 forms that all use the same filter.

<?php
$input_array=
array(
'input_name',
'input_email',
'input_query',
'input_fullname',
'input_dob',
'input_age',
'input_nationality',
'input_addnum',
'input_addname',
'input_addcity',
'input_addpostcode',
'input_phonehome',
'input_phonemobile',
'input_email',
'input_marital',
'input_nextofkin',
'input_prof',
'input_profemployer',
'input_profaddress',
'input_education',
'input_hobies',
'input_prevjoin',
'input_previnfo',
'input_interested',
'input_connected',
'input_connectedinfo',
'input_god',
'input_crim',
'input_criminalrecordinfo',
'input_available',
'input_hearabout',
'input_text_41',
'input_text_34',
'input_text_38',
'input_text_39',
'input_text_45',
'input_text_54',
'input_text_55',
'input_text_56',
'input_text_57',
'input_text_1',
'input_text_2',
'input_text_3',
'input_text_4',
'input_text_9',
'input_text_10'
);
$isvalid=true;

$ban_array_swear = array(
'bastard',
'bitch',
'coon',
'cunt',
'f u c k',
'faggot',
'fuck',
'goatse',
'hardcore',
'hentai',
'incest',
'lemon party',
'nig nog',
'nigga',
'nigger',
'prick',
'pussy',
'queer',
'rape',
'shit',
'slut',
'tub girl',
'tubgirl',
'twat',
'wanker',
'YHDCA',
'whore'
);

foreach ( $ban_array_swear as $v ) {
  foreach ( $input_array as $vv ) {
    if ( stripos($form->data[$vv], $v) !== false ) {
      $form->validation_errors[$vv] = 'Forms with offensive words are blocked!';
      $isvalid = false;
    }
  }
}

$ban_array_seo = array(
'seo',
'search engine',
'optimisation',
'optimise',
'customers',
'johnw2502@gmail.com'
);

foreach ( $ban_array_seo as $v ) {
  foreach ( $input_array as $vv ) {
    if ( stripos($form->data[$vv], $v) !== false ) {
      $form->validation_errors[$vv] = 'We do not require any SEO services.';
      $isvalid = false;
    }
  }
}

$ban_array_no = array(
'sialkot',
'Fair Goods Traders',
'Bhatti',
'Abu',
'Mabaad',
'hoodregalia',
'hoodhood',
'photography',
'Saba'
);
foreach ( $ban_array_no as $v ) {
  foreach ( $input_array as $vv ) {
    if ( stripos($form->data[$vv], $v) !== false ) {
      $form->validation_errors[$vv] = 'We do not require your services.';
      $isvalid = false;
    }
  }
}
?>


My events are:

on false - event loop
on true - send email and display message


No matter what is typed into the boxes, the event switcher never goes off - it continues as if nothing has happened.

I have tried moving the "return $isvalid;" code into the file but the results are the same.

This worked fine with V4, and according to the forum posts I can find, should work.

Am I missing something?? Please help!
GreyHead 18 Jul, 2015
Answer
Hi Nuvelle,

There are a couple of problems to be fixed.

a. The value of $invalid is probably outside the scope of the Event Switcher so the value is lost.

b. The Event Switcher requires a string to be returned, not the boolean true/false values.

To fix both of these add this code at the end of your included file:
if ( $isvalid ) {
  $form->data['isvalid'] = 'true';
} else { 
  $form->data['isvalid'] = 'false';
}
?>
then in the Event Switcher replace the last line with
return $form->data['isvalid'];
this will work provided that your Event Switcher events are called 'true' and 'false' - if the names are different edit the first code snippet to match.

Bob
Nuvelle 18 Jul, 2015
All working great thanks!

I changed it all around a bit so each different type of block above now has its own event and messages once I realised it was expecting a string rather than the true/false from before.

Thank you again!
Nuvelle 18 Jul, 2015
On a side note, is there a way to automatically get an array of all field names?

As you can see above the list is manually created using

$input_array=
array(
list of fields...
)


Every time I add a new form I have to check each field is listed, or add them in.

Is there code I could use that would automated this, no matter how many fields a form has?
GreyHead 19 Jul, 2015
1 Likes
Hi Nuvelle,

The form data is all in the $form->data array so you could just loop through that; if necessary you can add an array of inputs *not* to check.

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