Forums

Migrate Event Switcher from Cfv4 to CFv5

HerKle 13 Nov, 2015
Hi,

I used event switcher to validate input in a v4 form. Now I want to do the same in a v5 form.

I dragged an event switcher into the submit action, adapted some php code from v4 into it and loaded the events "true" and "false", added an event loop into the "false" switch. This looks quite similar to what I made once in v4.

Unfortunately when testing the form by submitting it with empty fields, no warning with error message appeared, and an empty record was saved to db. I have no idea what went wrong.

The code for validation looks like this:
<?php
//Check first name
$valid = true;
$vnm=ucwords(strtolower(substr(trim(strip_tags(JRequest::getString('vnm', '', 'post'))),0,30)));
if (!$vnm) {
  $form->validation_errors['vnm'] = "Bitte geben Sie Ihren Vornamen an.";
  $valid=false;
}
else $form->data['vnm']=$vnm;

etc with some more fields…

return $valid;
?>


Thanks for any help!
Regards,
Herbert
Max_admin 14 Nov, 2015
Answer
1 Likes
$form->validation_errors
becomes:
$form->errors


and your must "return" the event name, instead of true or false!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 14 Nov, 2015
1 Likes
Hi Herbert,

Please see this FAQ - I have extended the FAQ to include Max's notes and a couple of simple examples.

Bob
HerKle 14 Nov, 2015
Thanks to both of you!

Meanwhile I found some more information elsewhere and changed my code:
<?php
$jinput = JFactory::getApplication()->input;
$valid = "success";

//Check first name
$vnm=ucwords(mb_strtolower(substr(trim(strip_tags($jinput->get('vnm', '', ''))),0,30),'UTF-8'));
if (!$vnm) {
  $form->errors['vnm'] = "Bitte geben Sie Ihren Vornamen an.";
  $valid="fail";
}
else $form->data['vnm']=$vnm;

//Some more fields

?>


I learned that JRequest is deprecated. And with JInput itself most of validating will be checked already by that object's filter options. But for now I still have to use my own custom validation.
I had to replace the function strtolower by mb_strotolower to make it work with other characters than a-z, too.

For your FAQ about validation (3rd option):
Using "Event Switcher" for validation I made the error messages visible by putting them into a Custom Code box in the OnLoad Event:

<?php
if($form->errors) {
  $ftext='<h3>Error!</h3><p>';
  foreach ($form->errors as $v) {
    $ftext.= '<strong>'.$v.'</strong><br />';
  }
  $ftext.='</p>';
  echo $ftext;
}
?>



Thanks again!
Regards,
Herbert
This topic is locked and no more replies can be posted.