[Solved] 2 checkbox Validation conditions

Status
Not open for further replies.

AlainR

Member
Hi!

I need to validate 2 checkbox min and max and I do not know what rule to use.
The user will have 3 choices and he must select two, neither more nor less.

What validation rule should I apply?
Perhaps Is Greater Than or Less validation, but I do not see how the set for that.

Thanks in advance for the help.
Cordially.

Thank you hugh :
A PHP validation, with PHP code:

Code:
return count($formModel->formData['youtable___yourelement']) === 2;

Replace yourtable___yourelement with your full checkbox element name.

-- hugh
 
Last edited:
Write your own javascript validation. Using jQuery...
return (jQuery("input[name^='table_name___element_name']:checked").length == 2);

EDIT: Ooops. I'm just realizing there is no way (as far as I can see) to add a simple javascript validation! Maybe someone should write a javascript validation plugin? I'm really surprised there isn't one - or am I missig something?

You could always just add an alert using the element javascript plugin...
JavaScript:
if (jQuery(this).find("input:checked").length != 2) {
    alert('Please select 2 checkbox options');
}
But that doesn't really prevent the user from selecting more or less than 2 does it?

I suppose the only way would be to use the php validation plugin (but you would think that a call back to the server just to validate the number of checkboxes checked would not be needed.)

According to the Wiki, that would be as simple as this 'PHP code'...
return (count($data)==2);

But testing that just now (with the latest github code) there seems to be a bug that returns the incorrect value for $data. in the 'PHP code' of the php validation plugin.
It's returning 2 different values for $data - the correct value in the 'Condition' but an incorrect value in the 'PHP code'.

I will post a new thread regarding this new bug. ( Because I know this used to work just fine.)
 
Last edited:
Hmmm, for reasons I have no clue about, at the start of the PHP validation, we do ...

Code:
        // For multi-select elements
        if (is_array($data))
        {
            $data = implode('', $data);
        }

... which flatten the checkbox array into a string with no delim. No idea why. But I'm loathe to change it, as I'm sure that would break backward compat for everyone using it on multi-select elements.

However, you can still get at the form data itself, so ...

Code:
return count($formModel->formData['youtable___yourelement']) === 2;

-- hugh
 
Hmmm, for reasons I have no clue about, at the start of the PHP validation, we do ...

Code:
        // For multi-select elements
        if (is_array($data))
        {
            $data = implode('', $data);
        }

... which flatten the checkbox array into a string with no delim. No idea why. But I'm loathe to change it, as I'm sure that would break backward compat for everyone using it on multi-select elements.
[...]
How could that possibly be anything other than a typo? I'm sure it was meant to include a comma as a delimiter. What good or what use would that string ever be??? - and how could it break anything "for everyone else" that isn't already broken? What could you possibly ever do with that string after it has been imploded without a delimiter - unless you knew what the length of each 'piece' of the original array actually was??? This just makes no sense - and I know it hasn't been like this until recently.

FOLLOWUP EDIT: I just looked at various snapshots of github zip updates that I still have saved (back as far as Set 2015) - and this was always there. But I'm still almost certain it hasn't always been this way - as I remember doing a similar test with $data in the php plugin and getting the expected results back when I questioned you on how to work with the form data in that plugin a few years ago.

And, in all of the Fabrik code, this nonsense implode function is ONLY used in validation rules (9 of them). I didn't look at them all, but for the php plugin (php.php) - in the validate() function where this implode is used on the data, both $data and $repeatCounter are then passed to the _eval function - yet neither $data nor $repeatCounter is used in that function. Instead, the _eval function gets the data from $formModel->formData yet again.

So I assume that $data and $repeatCounter is only passed to the _eval function in case $data or $repeatCounter was used in the eval code itself - which only adds to my suspicion that this MUST be a typo or worse yet, just code that wasn't thought through too well - and IMO it needs to be removed - probably in all 9 instances. Maybe this is why so many users have problems getting their validations to work??? If it was done thinking it would allow users to use their php code to find a string in an array of $data - then turning the $data array into a concatenated string doesn't help - especially with an array of numbers. That's what the php in_array() functon is for.
 
Last edited:
Like I said, I have no clue why we do that, and yes, it's wrong. But it's been that way since birth in 2011 ...

https://github.com/Fabrik/fabrik/bl...42e/plugins/fabrik_validationrule/php/php.php

BTW, you don't have to look through snapshots, you can just browse a file on github, and either look at the "blame" (which shows the last commit any given line of code was changed on), and/or look at the commit history.

If you want to submit a PR removing that, I'll merge it. It'll break backward compat, and a few people's validations will probably stop working, but it makes no sense as is.

-- hugh
 
Hi!
Thank you very much for the answers, but.... my english is so bad, I don't understand/follow your discussion.... What do I need to do ? where do I copy/cut your code ?
I will try until next week.
Thanks again and best regards.
 
A PHP validation, with PHP code:

Code:
return count($formModel->formData['youtable___yourelement']) === 2;

Replace yourtable___yourelement with your full checkbox element name.

-- hugh
 
Thank you very much, but it doesn't work when I edit the element, add a "notempty" validation with "return count($formModel->formData['formations___AteliersPratiques']) === 2;" condition.

But, another condition works : because this element is only shown depending another field selection, the condition "return $_POST['formations___Formation'][0] === 'Comment apprendre a une personne autiste a devenir autonome ?'" works fine.

So I have 2 questions :
  1. what conditions must be put to validate the min and max 2 choices?
  2. how to combine the two conditions?
    Is that enough to write something like this : "return $_POST['formations___Formation'][0] === 'Comment apprendre a une personne autiste a devenir autonome ?' and count($formModel->formData['formations___AteliersPratiques']) === 2;" ?
    For the moment, it doesn't work, but if I write "or" instead of "and", the first condition works fine, but not the second.
Thanks again.
 
The "Condition" is different, that tells Fabrik whetehr to apply the validation or not.

To count checkboxes, do what I said in my last post, and use a PHP validation, with that in the PHP code (NOT the condition).

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Staff online

Members online

Back
Top