Validations for a Code type field

7 years 8 months ago #285599 by cpaschen
Validations for a Code type field was created by cpaschen
I have a code field that builds it's value from a contact of 3 other values that users enter:
$composite = [cb_reviewer_id_adjective] . [cb_reviewer_id_noun] . [cb_reviewerid_number];
return $composite;

However, I also need to use some code to validate the generated ($composite) value.
And it appears that using the INTEGRATIONS | CB CODE FIELD validation doesn't have any effect.

I've added:
$result = false;
return $result;

But the field still validates.

I've also tried using a Query Validation and that seems to have no effect also.


Do the validations not work on code types?

Please Log in to join the conversation.

7 years 8 months ago #285615 by krileon
Replied by krileon on topic Validations for a Code type field
You need to treat all substitutions as strings. So your usage should be as follows, but it won't do anything as it expects a true or false response.
$composite = '[cb_reviewer_id_adjective][cb_reviewer_id_noun][cb_reviewerid_number]';
return $composite;

Your below example is working fine on our demo site. Please ensure CB Code Field plugin within CB > Plugin Management is published and public.

$result = false;
return $result;

Note the validation will be ignored if the field is empty and you do not have the field set to be required.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 8 months ago - 7 years 8 months ago #285638 by cpaschen
Replied by cpaschen on topic Validations for a Code type field
The previous '$composite' was adding the proper content, but I changed my code to match yours. (and it is still displaying the proper results).
So I know that it does have some 'stuff' in that field.
I have also ensured that the CB Code Field plugin is published and public.
And the the code field is set to required.

One thing that I just noticed (by changing the code to $composite='test') is that the code is run when the edit window first opens.
So at that point, are all those substitutions blank (because I haven't added any new/changed data)? Or are they initially populated with the values that were last saved?
If they are initially empty, and the validation runs only on that first run of the code, then it would explain why the validation isn't working properly.

And I'm guessing that when it tries to run on save it probably already is set as having validated (just speculation).

I did have ajax validation enabled but disabled with no results.

I also tested to see if the order of the CB Code Field plugin made any difference (I moved it to the top and the bottom) and that didn't change anything either.

I also tried testing this on demo.cbdemosites.com, and it isn't working for me there either.

The field is

cb_krcodetest


Let me make sure that I understand the 'intended' results of this code (and make sure that they match my expectations).

If I add the INTEGRATIONS | CB CODE FIELD:
$result = false;
return $result;
I'm expecting that whenever you try to save a record it should fail (and display the text in the 'Error Message' text entry).

That is not happening on my site or the demo.

Am I confused on how this should work (or is there a bug somewhere)?

Please Log in to join the conversation.

7 years 8 months ago #285645 by cpaschen
Replied by cpaschen on topic Validations for a Code type field
Also, I tried to use a CB Query Field Validation:
SELECT * FROM `kr_bad_words_list` WHERE `word` LIKE '[value]' AND state = 1

with
Validate On: Empty Results

(and I confirmed that I entered values that matched something in the 'bad_words_list' table)

But that didn't work either.

I thought about possibly doing this via Auto Actions, but that got rather convoluted as soon as I tried.

Please Log in to join the conversation.

7 years 8 months ago - 7 years 8 months ago #285681 by krileon
Replied by krileon on topic Validations for a Code type field

I'm expecting that whenever you try to save a record it should fail (and display the text in the 'Error Message' text entry).

Correct.

That is not happening on my site or the demo.

Works perfectly fine on our demo site. You need to either supply a value to the field or ensure it's marked required. A non-required empty field will ignore that validation (because it's not required to have a value so there's nothing to validate). Your $composite usage is not valid. It expects a true or false boolean response. You're returning a giant string so due to the IF check it probably is just treating it as valid, always. You'd need to better clarify what you're trying to validate for me to suggest anything further, but you're supposed to write PHP that does your validation and if it matches your validation return true and if not then return false.

Also, I tried to use a CB Query Field Validation:

You either need to match the words exactly using a = operator or be sure you've used a wildcard for your LIKE usage. No idea how that table is setup so can't say for sure exactly if your query is working right. Your query should be doing a COUNT as well; as is you're returning a bunch of rows and there's nothing to validate from that. So your query probably should be the following.

SELECT COUNT(*) FROM `#__bad_words_list` WHERE `word` = '[value]' AND `state` = 1

For the query validation it's important to understand single row results will behave as follows: "If the query returns only 1 result then the value of the result will be used.". This means it'll try to use the value returned by the query as its validation. The COUNT solves this (in addition to improving the performance of the query).


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 8 months ago #285750 by cpaschen
Replied by cpaschen on topic Validations for a Code type field
Were you able to take a look at the field that I created on the demo site?
(It looks like it got removed since I posted it).

I've recreated the field on the demo site (demo.cbdemosites.com).

Here's how I have configured the field:

field name : cb_krcodetest

Global
* Publish: Yes
* Type: Code
* Tab: Fields
* Required: Required
* Read Only: Yes
* Show on Profile: Yes, 1 Line with Title
* Show on Registration: No
* Show on Profile Edit: Yes, 1 Line with Title

Parameters

Code:
$result= 'the answer';
return $result;

Integrations - CB Code Field

* Code Validation: Enabled
* Code (see below)
* Success Message: "VALID input for krcodetest"
* Error Message: "Not a valid input for krcodetest"
Ajax Validation: Disabled

Code:
$result = false;
return $result;

My understanding is that this should cause any attempt to edit and save the profile to fail. However, on the demo site I'm getting:
"Your settings have been saved." message.
This is not the Success or the Error message related to the condition.
Therefore I'm guessing that the condition code is not being run.

The actual code of the field IS being run (the value IS being stored in the db).

Can you please take a look at that field on the demo site and let me know if you are actually getting an error (which I expect) or if you can find a problem in the configuration of the field?

I really need to figure out HOW to make a code validation work on a code field type.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.178 seconds

Facebook Twitter LinkedIn