Can't get php validation working

Joppo2

Member
Hi,

I created a list and related form with Fabrik and for one of the elements I want to add some php validation but it's not working - see code below. The code is supposed to look up the eneterd name in a database table; if not found in db the validation will/must fail (return false). But when I enter a wrong name in the form (not in db) the form still submits the eneterd data and stores in the db??

In fact, - just a test- when I only include a 'return false' in the php validation code the submitted data is still recorded in the db (???)

Appreciate your help.

Php validation code:

// Create a new query object.
$query = $db->getQuery(true);
//$namecollective = $formData['weq_productshares___collective'];
//$namecollective = $formModel->formData['weq_productshares___collective']; //this fabrik form element
$query->select($db->quoteName(array('user_id')));
$query->from($db->quoteName('#__extendedreg_users'));
$query->where($db->quoteName('cf_collectivename') . ' = '. $data); //use $data for this form element
$db->setQuery($query);
$db->execute();
$collective_id = $db->loadResult(); //query returns null if fails (no record in db

returns isset($collective_id);
 
At least it must be
return ...; (not returns)

For debugging you can include e.g.
var_dump($data,$collective_id);exit;
before the return to see what you get.
 
thnx.

corrected 'return' statement and added var_dump line. No effect, e.g., submitted form data is still stored in db and var_dump line does not show anything on webpage...

ps: the related Condition field is empty, but I also tried with a 'return true;' statement - without any effect
 
Ok, then there seems to be a syntax error somewhere (php not running at all; assuming your validation is published).
Try line by line.
Is your Joomla error reporting set to maximum?
 
validation is published indeed. Just set error reporting to maximum but no errors display at all.

Are there any other conditions/Fabrik or Joomla 3.x settings that must be met to run php validation?
 
I think you need to intiate the db connection, so the first line of the code shouyld be:
$db = FabrikWorker::getDbo();

Also, I'm not sure what you are trying to do here: $query->select($db->quoteName(array('user_id'))); ?

You can insert echo $query->dump(); exit; before the setQuery line, so you print out your query. You can run it in phpmyadmin to see if it returns the expected records.
 
thnx for the suggestions (!)

My mistake that I forgot to post the line "$db = JFactory::getDbo();" in my initial post - sorry. I understand from the example at http://fabrikar.com/forums/index.php?wiki/common-php-tasks/ that this standard Joomla db access method is allowed by Fabrik). But I will try your suggestion anyway.

Nevertheless, I'm still intriged to understand why the php validation part is not working at all - even when I put a simple single line 'return false' in it? (which should enforce the custom error message when submitting the form - (?) That suggests something else is not working apart from the type of php code...

ps: just my joomla system information if that may be of any help?:

SettingValue

PHP Built On Darwin MMs-MacBook-Pro.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
Database Version 5.6.12
Database Collation
PHP Version 5.4.16
Web Server Apache/2.2.26 (Unix) DAV/2 PHP/5.4.16 mod_ssl/2.2.26 OpenSSL/0.9.8y
WebServer to PHP Interface apache2handler
Joomla! Version Joomla! 3.3.0 Stable [ Ember ] 30-April-2014 14:00 GMT
Joomla! Platform Version Joomla Platform 13.1.0 Stable [ Curiosity ] 24-Apr-2013 00:00 GMT
 
Just an update on my struggle with the php validation; I discovered that all Validation options (isemail, isgreaterorlessthan, php, etc...) are not working. I started from scratch to test/check the Validation part in Fabrik, e.g., I installed a new Joomla 3.x configuration (with some sample data), set Joomla reporting level to max, installed Fabrik and created a simple list and form with just one additional field 'Collective'; same story as reported before: Form submission always happens and no custom validation error message appears - irrespective the entered value, type of validation(php, isemail,..) and php code in case of the php validation option. When I choose for example isemail, the related tooltip does work.

I could debug the code by means of Netbeans that I use if I knew where to look at in the Fabrik code. However, I dont think there is something wrong with the Joomla/Fabrik code and I have the strong impression I have forgotten something stupid like some Fabrik parameter not being set properly. Pls see the attached file for the settings I used for the simple example.

ps: using php code in Default with the Eval option (in order to set a default value for the formfield) works properly.

ps2(!): maybe it's a coincidence but the nature of my problem seems very similar as reported earlier in http://fabrikar.com/forums/index.php?threads/no-validation-error-messages-showing.37664/. However, I checked that my installed Fabrik version is 3.1 (date Feb 2014). My Joomla version is Joomla! 3.3.0 Stable [ Ember ] 30-April-2014.

Any suggestions are highly appreciated (?)
 

Attachments

  • screenshots.zip
    155.9 KB · Views: 161
1) After installing Fabrik, did you make an update from GitHub (Fabrik 3.1 available download won't work with J 3.3)?
2) Have you any JS error on the page?
3) Have you tested with Protostar template?
 
Hi jfquestiaux,

Thnx for your suggestions.

The answers to your questions:

1: No, i did not. I will (after I have digested the github update instructions). Hopefully that solves the problem. Will let you know.

2. No, just checked with the JS console; no js errors; Should I get js errors?

3. Yes. (is that good or bad? ;-) )
 
1) You should. The 3.1 "core" is getting a little old and there are issues if you use the latest Joomla releases

2) You shouldn't, but if you had some, that might be a reason

3) Sometimes issues are caused by 3rd party templates (conflicting JS and/or CSS), so it's always good to test with the default J! template to rule out a problem coming from the template.
 
Hi, just an update: I finally managed to upgrade Fabrik from Github (further to jfquestiaux's suggestion) but no result, e.g., the form field validation (php,isemail, etc) is still not working.

Is this issue only my struggle or is it it a known fabrik development issue? E.g., I wonder whether anybody else got the (php) form field validation working with Joomla 3.x and the latest Fabrik version from Github?

Update #2: I just created a simple joomla testwebsite at http://mf68.cloudaccess.net/administrator/index.php.
Login details: Username: mfcloud3
Password: 69qUhc4X3m

Then I:
- installed Fabrik
- created a simple list with one added element 'collective' and set the validation to isemail.
- linked the list to a new Main menu option 'myfab' on the public page (http://mf68.cloudaccess.net/).

As before it doesn't work either, e.g., when submitting the form with a non valid email in the 'Collective' form field, no errror message shows up and the formfield is stored in the db (and showed in the list.

Perhaps if somebody is willing to log in and take a look into the Fabrik list,form and element configuration? I still assume this is not a Fabrik bug and I somehow configured something wrong within Fabrik...

Appreciate your help.
 
I just had a look.
It seems this is not a recent GitHub update (the site is showing exactly the "J!3.3 without GitHub" issues: you can't add a 2nd validation....).
If you are running Joomla3.3 you MUST update from GitHub.
 
thnx for you comment(!). You are right, I did not update the online test version at cloudaccess yet.

Maybe when you logged in you saw 2 validations at the moment I added one to try. Now there is only one validation (isemail) again and that does not work either.

(ps: as posted before I already updated my local test version from Github, which did

I will upgrade the online test version this weekend and will let you know.
 
Update: I finally managed to upgrade Fabrik in the proper way on my laptop and the validation is working now.

Just two notes:
1. With respect to the Fabrik upgrade instructions at http://fabrikar.com/forums/index.php?wiki/update-from-github/ - copying the six folders from https://github.com/Fabrik/fabrik/archive/joomla3.zip did work for me, but copying from the local github did not (dont understand why?)

2. I noticed that the 'isemail' validatation passes strings with '@' but without '.something' (like 'me@gmail') -

Thanks to all who responded and helped on this post (!)
 
The "isemail" validation just uses the Joomla validation method and, since Joomla 3.0.0 I beleive, Joomla consider xxx@yyyy to be a valid email, which technically it is (in a local network).
If you want to validates "real world" adresses (like xxx@yyyy.zzz), you need to use regex.

I've used this and it works even with the new domains extensions:
/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/
 
It's not so much that Joomla consider foo@something valid, it just is. Like "root@localhost".

Note that Fabrik does have a helper func for isEmail(), where we look to see which mailer you are using, and apply slightly different rules if you are using sendmail, to avoid some issues where different mailers barf on different email formats.

Anyway, as JFQ says, if you need to only allow a@b.c shapes, you'll need to roll your own validation, either a PHP or a regex. There's a lot of code you can borrow from Teh Interwebz, just google for some obvious phrases like "php email validation" or "regex email validation", and you'll find solutions ranging from very simple and straightforward, to the sublime. I think my favorite is this regex:

Code:
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)
?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\
r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[
\t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)
?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]
)*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[
\t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*
)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)
*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+
|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r
\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:
\r\n)?[ \t])+|\Z|(?=[\["()<>@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\t])+|\Z|(?=[\["()<>@,;:\\".\[
\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-
\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(
?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;
:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"
.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\
]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\
[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\
r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\]
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]
|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \0
00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\
.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,
;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?
:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[
^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]
]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(
?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(
?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[
\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t
])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t
])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?
:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|
\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:
[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\
]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)
?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["
()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)
?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>
@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[
\t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,
;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:
\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[
"()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])
*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])
+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)

... which matches all RFC822 valid formats. :)

-- hugh
 
LOL! Yeah, I don't actually recommend that regex. It's more of an in joke than a serious regex for emails.

However, there are a number of much shorter regular expressions you can find out there, which handle most of the common formats.

The main problem here is that J! and Fabrik have to allow for some uncommon ones, like the "no top level domain part" (foo@bar), as they are perfectly legal, and some Fabrik users would want to deliver to local hosts, but you may not want those to validate for your normal users, and require a real "internet" address.

"One of these days" I'll add a little more intelligence to the 'isemail' validation, with some options to turn certain technically legal shapes on/off.

Meanwhile, you could always add another simpler validation to (for instance) specifically disallow foo@bar with a much simpler regex.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top