php plugin - onbeforeprocess

Status
Not open for further replies.

earobins

Member
I have a form that accepts presenter applications for a conference. It working in Joomla 1.5 for a couple of years and I haven't really looked at it in awhile. I upgraded the site to 2.5.18 and upgraded Fabrik initially to 3.0, then ended up with 3.1, since the github file said it could be used with J! 2.5. I really didn't think this form would be used again, but now I need to get it working. Several things were not working after the upgrades, but most are now sorted out. I have a php plugin that runs when the form is submitted, (onBeforeprocess) that I cannot get to work correctly. My list is for applicants. An applicant may or may not apply with up to 5 copresenters, so copresenters are in a repeating group. There is a field called copresenter that is a dropdown with values of 0 - 5. If an applicant selects between 1-5, the 1st repeating group fields should be shown for them to enter the copresenter information. If they say they will have 3 copresenters, they are required to enter information for 3 copresenters. The plugin is checking to make sure that the number of copresenters entered matched the number selected in the dropdown box. I may not be setting the error message correctly. I followed the instructions in the wiki, but am not seeing what is wrong.
No matter what is entered in the form, a syntax: JSON parse error is displaying on submit and the form then tries to start validating the first field again and the validation spinner just keeps spinning.

I am uploading a screen shot of the error and a portion of what I am displaying in Firebug. The php code is below.

The other issue I am having is that when the copresenter dropdown changes, if it >0 the first empty occurance of the repeating group copresenter should be shown. It was working in Firefox at one point, with Fabrik 3.0, but since moving to 3.1 it will only show the "empty data" box with the +/-. Once you click on the + the fields display, but I would like the first occurance to show as soon as the # of copresenters changes. Since the upgrade it has not worked that way in Chrome. That is how it worked in the past, under J!1.5.

Thank you! I appreciate the help.

the php plugin - from /plugins/fabrik/php/scripts...
<?php
/**
* this is a very simple example of a script to stop the form being processed
* The form is then redisplayed with an error
* notice at the top of the form (and the previously filled in data still visible)
*/
$formModel->formData;

echo "<pre>";print_r($formModel->formData);
$repeatTotals = $formModel->formData['fabrik_repeat_group'][16];
$copres = $formModel->formData['fab_applicant___copresenters'];

echo "repeat group";
echo var_dump($repeatTotals);
echo "copres";
echo var_dump($copres);
if ($copres != $repeatTotals)
{$formModel->errors['fab_applicant___copresenters'][] = 'Please enter contact information for all copresenters';
return false;
}else
{return true;}
?>
 

Attachments

  • Capture.JPG
    Capture.JPG
    32.9 KB · Views: 276
  • fabrikpdf.pdf
    130.2 KB · Views: 341
Hi
I presume looking at the first screenshot, that the form is being processed via ajax? If so your echo and print_r data will be added the the server response. This has the effect of creating an invalid json response which Fabrik will interpret as an error (and hence the 'syntax error' message that is shown at the top of the form.

For testing I would turn off the ajax submit, and add an exit; line before you return from within your custom script. This will then print out the debug information in the browser window.
(Alternatively you can leave the ajax submit on, but use firefox and firebug to examine the response of the ajax form submit, but I find that a bit more fiddly to deal with)
 
Thanks, Rob. I think I was already using your alternative solution, but to make sure I understand you - I set the form processing Ajax Validation = No, added the exit, then tried again in both Firefox w/Firebug and Chrome with the dev. tools turned on. The data looked correct. When I took the exit back out, I figured out 2 things. Obviously, I don't understand what my compare is doing, because when the 2 values are =, it is failing. When they are not equal it is passing. I also realized that error is happening after the error/success message is set. The end portion of what I am seeing in Firebug/Chrome dev tools is shown below. Does this mean the problem is in fabrik-min.js? Am I passing/setting something incorrectly in my php script? Thanks. I just went back and verified that all of my plugins have been updated and that I am running the correct versions of everything. As far as I can tell, they are correct.

) repeat groupstring(1) "0" copresarray(1) { [0]=> string(1) "1" } {"msg":"Thank you Elizabeth. Your application has been submitted. You will receive an email confirmation shortly.","url":"index.php?option=com_content&view=article&id=150&isMambot=1","baseRedirect":false,"rowid":"25","redirect_how":"samepage","width":300,"height":300,"x_offset":0,"y_offset":0,"title":"","reset_form":true}: SyntaxError: Unexpected token <
fabrik-min.js:1​
 
No, what he meant is that your form is being submitted through AJAX. So when you echo anything, it's going in to the JSONresponse that comes back from the AJAX call, which breaks the JavaScript that processes the form response.

Is this form on a normal Fabrik component link, or in form module, or in an article using the content plugin, or ...

-- hugh
 
Understood - the echos I was using to debug were actually causing the error. Thanks. I have pulled them - I looked at them enough times to know that the variables have the correct data in them. That took care of the error. Now I am trying to figure out why the code below is always returning an error. My interpretation of it is that it should only flag the field if the two values are not equal, but it is giving the error whether they are equal or not equal. I will continue to work on it, but I am guessing that you will know instantly what is wrong with it.

In the past, the form was in an article. Right now, I have it set up in the article and to pull directly from the menu as a component link. I just did that to see if it made any difference in the processing. Normally, it will run from an article.

<?php
/**
* this is a very simple example of a script to stop the form being processed
* The form is then redisplayed with an error
* notice at the top of the form (and the previously filled in data still visible)
*/
$formModel->formData;

$repeatTotals = $formModel->formData['fabrik_repeat_group'][16];
$copres = $formModel->formData['fab_applicant___copresenters'];

if ($copres != $repeatTotals)
{$formModel->errors['fab_applicant___copresenters'][] = 'Please enter contact information for all copresenters';
return false;
}else
{return true;}
?>
 
Friendly bump.

So frustrated because this seems should be so easy but my php is definitely lacking. I have googled/searched php manual and a book I have don't understand how to set one variable (at least I think that is what the problem is).

This running from php plugin, onBeforeProcess. fab_applicant__copresenters is a dropdown field, with values 0-5. When I echo my variables, this is what shows (below) . In the (front end) form I set copresenters to 1, and entered information for 1 copresenter, so the values should be equal and the entry should pass the validation without setting an error message. It is not, so my assumption is that I am not setting that copres variable correctly because it is showing it as an array and I need to get it to just a string (?). I have set that copres variable multiple ways, but have yet to get it right. I appreciate any help.

repeat totals: string(1) "1" copres:array(1) { [0]=> string(1) "1" }
php code again:
<?php
/**
* this is a very simple example of a script to stop the form being processed
* The form is then redisplayed with an error
* notice at the top of the form (and the previously filled in data still visible)
*/
$formModel->formData;

$repeatTotals = $formModel->formData['fabrik_repeat_group'][16];
$copres = $formModel->formData['fab_applicant___copresenters'];

if ($copres != $repeatTotals)
{$formModel->errors['fab_applicant___copresenters'][] = 'Please enter contact information for all copresenters';
return false;
}else
{return true;}
?>
 
never mind...I realized what was wrong. If statement should be:
if ($copres[0] != $repeatTotals)
I have a couple of other questions but will open separate threads.
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top