Buy Now
Sign in

Event Switcher Conditions

rtobias , April 24 2017, 20:09
R
rtobias 53
April 24 2017, 20:09 #367399
Hello again,

In converting one of my CF5 forms to CF6, I'm trying to recreate an event switcher. I saw in the demo that you have for the Data Source: {data:product}. However, in the past, we were about to use code directly in the event switcher to customize our condition. For example, if I wanted to compare a field value to a GET param, I had done something like this:


<?php

if ($form->data["code"] == $_GET['key'])
{
return"success";
}
else
{
return "fail";
}


I see in the instructions this:
  
{function:FUNCTION_NAME} or {fn:FUNCTION_NAME}
Run a function, function must be created under the Events tab.

Would I need to use a custom code (php) action before my event switcher, or do I need to go back to my designer and add something to the events tab?


As an aside, if I need to set up an event on a field for my switch condition to operate off of, that becomes difficult for me because the code field is a hidden field. I'm assuming you didn't give hidden fields the same abilities as text fields because normally, people can't change the values of hidden fields. That said, if hidden fields did have the same abilities as text fields, I suppose I could set up an event to compare if the value was equal to a url param (assuming I could use php in the event setup to return the 'key' url param's value). All said and done though, I hope I'm missing something because this seems quite a bit more messy than just being able use code to set my condition as above.
Randy
admin 28
April 24 2017, 20:28 #367403
Hi Randy,

In v6 the event switcher will accept a field value, but in your case here where you need to do some logic operation, you need a PHP function before it, the same code you have, then you need to use this in the "Data provider" of the event switcher:
{var:php_function_name}

Your events should then be set to:
success,fail

because those are the values you expect to receive from the data provider, which is your PHP code.

Where do you get the "code" field data from ? also in v6 it should be $this->data

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!
Did you try the new ChronoForms7 ? if yes then please send us your comments!!
R
rtobias 53
April 24 2017, 21:02 #367407
Thanks a bunch Max!

I use this setup a couple of ways, but my primary one is on user registration, I generate a hashed random code and store it in the db. I also send the registrant an email with an activation link, which also contains an entity id:


function generateLink($eid, $code, $baseURL)
{
  $params = array(
    "eid" => $eid,
    "key" => $code
  );  

  $url = $baseURL . http_build_query($params);
  return $url;
}


Part of the $baseURL is the name of an event that I use to look up the registrant's info from the db, and if the activation code that I loaded from the db matches the code that I get from the url parameters, then I mark them as activated (really, I just delete the activation code from the db). Later on, in other forms, I can load up the entity id for this user, and if they have an activation code (or don't have an account at all), I use another event switcher to either display the form normally, or display a bit of text stating, "You need to be logged in to use this system" as well as a button that redirects them to the login form.

I haven't been able to test if it works yet but I added the php as you suggested:

function checkCode()
{
if ($this->data["code"] == $_GET['key'])
{
return"success";
}
else
{
return "fail";
}
}


and in the event switcher, I set the Data Provider to "{var:checkCode}".

Really liking that I don't have to open and close modals all the time now, so it is easy for me to scroll up to see what I named a function in a different action.
Randy
R
rtobias 53
April 24 2017, 21:07 #367408
Also, thanks for the heads up about $this->data instead of $form->data.
Randy
admin 28
April 25 2017, 18:54 #367432
Hi Randy,

You should not have the text "function" in your code, do you have that ?

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!
Did you try the new ChronoForms7 ? if yes then please send us your comments!!
R
rtobias 53
April 25 2017, 18:55 #367434
I do, I thought I had to declare that this is a function.
Randy
admin 28
April 25 2017, 20:50 #367437
Hi Randy,

The code should be inside the PHP function (or action, its called function in Connectivity and so I use the same name), but your code should not include the function definition.

Best regards,
Max
Max
If your main question got answered then please mark the answer using the button!
Did you try the new ChronoForms7 ? if yes then please send us your comments!!
R
rtobias 53
April 25 2017, 20:55 #367438
Oh, that was my confusion. I thought I had to declare a php function, not that I was labeling my php action and then referring to it in the Data Provider.


{var:php_action_name}


Also, I need to make sure that I am using a separate php action for this instead of combining it with another one. Alright, this makes more sense now. Thanks.
Randy