Skip to Content Skip to Menu

CB Code Field Validation

  • autobahn
  • autobahn
  • OFFLINE
  • Posts: 202
  • Thanks: 38
  • Karma: 4
3 years 11 months ago - 3 years 11 months ago #322898 by autobahn
CB Code Field Validation was created by autobahn
I have two dropdown input fields, Make and Model, which form a Field Group. I am trying to use CB Code Field Validation on these two fields and have run into a problem.

I can force a selection for 'Make' by specifying that it is a Required field.

I cannot specify that 'Model' is a Required field as this is dependent on the value selected for 'Make'.

How do I test to see whether a value has been selected for 'Model'?

These lines of code work as expected:
if ('[cb_make]' != "None" && '[value]' == "None") {return false;}
AND
if ('[value]' != "xxxx") {return false;}

To test whether any value at all has been entered I have tried:

if (!isset('[value]')) {return false;}
if (empty('[value]')) {return false;}
if (is_null('[value]')) {return false;}
if ('[value]' == "") {return false;}


none of which cause a validation failure and an error message. Instead the database is updated and I get a message confirming that.

Am I using the wrong command? wrong syntax?

I am running PHP 7.3.3 under XAMPP on a Win 10 PC, Joomla 3.9.24, CB 2.5.0
Last edit: 3 years 11 months ago by autobahn.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48674
  • Thanks: 8313
  • Karma: 1446
3 years 11 months ago #322910 by krileon
Replied by krileon on topic CB Code Field Validation
If nothing is selected it should just be an empty string. So for the example the below should be fine for excluding empty.

Code:
if ( '[value]' == '' ) { return true; }

You need to return true as we want it to pass and skip the validation. If you return false it fails the validation. Be sure you also have additional fields configured like the following example if using the ajax validation feature.

Ajax Validation: Enabled
Additional Fields: cb_make


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 or Create an account to join the conversation.

  • autobahn
  • autobahn
  • OFFLINE
  • Posts: 202
  • Thanks: 38
  • Karma: 4
3 years 11 months ago #322918 by autobahn
Replied by autobahn on topic CB Code Field Validation
Thank you for your quick reply.

I had already checked that:

Ajax Validation: Enabled
Additional Fields: cb_make

Returning false for an empty string is what I want to do in this instance, since if Make is not "None" then Model must not be empty (i.e. it's value must be selected from the dropdown list). I have tried again, replacing my double quotes with single quotes

if ('[value]' == '') {return false};

but with the same result.

Is it possible that I have misunderstood the point at which the validation code runs? If I select a value from the Model dropdown it is evident that something is triggered as the little rotating symbol appears briefly. If I don't select any Model, or if I select the empty option, then the little rotating symbol does not appear; clicking on the Update button doesn't appear to trigger the validation code.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48674
  • Thanks: 8313
  • Karma: 1446
3 years 11 months ago - 3 years 11 months ago #322932 by krileon
Replied by krileon on topic CB Code Field Validation
If the field isn't marked required then you hit a special case in the validation that's applied to all validation. A not required field is always allowed to be empty as it's an optional field. I don't think you'll be able to have what you're wanting without the field being marked 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.
Last edit: 3 years 11 months ago by krileon.
The following user(s) said Thank You: autobahn

Please Log in or Create an account to join the conversation.

  • autobahn
  • autobahn
  • OFFLINE
  • Posts: 202
  • Thanks: 38
  • Karma: 4
3 years 11 months ago #322954 by autobahn
Replied by autobahn on topic CB Code Field Validation
I wondered if that was the case and it is good to have it confirmed:)

I've since been trying all sorts of different approaches to solving the overarching problem. Basically, for any given member the set of fields that make up the Field Group can occur 0-n times; in the majority of cases it occurs only once but our system allocates space for three sets per member and we are wasting a lot of disk space. However no occurrences is common and several members require space for up to 9 sets. The multi-field validation was the most obvious route for what I am trying to achieve and I will try other ways.

If it's of any help to someone who runs into a similar requirement, I'd just like to mention that a) you guys have already posted (some time ago and on a different thread) that CB Conditional is not valid for fields within Field Groups and b) a Field Group can be optional, even if fields within it are required. This information has pointed me to a possible solution and may be of help to someone else.

Thank you for your time and patience, and while it's not possible to achieve the validation I was attempting the information you provided in response is invaluable.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum