1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Validation only if element is visible?

Discussion in 'Professional Support' started by joit, Feb 16, 2017.

  1. joit

    joit Active Member

    Level: Community
    Hi,

    I need to have a validation on an element that only validates if the element is not hidden with Javascript onload hide...

    Is it possible?

    Thanks
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Community
    You can add a validation condition which is "replicating" your JS show condition.
     
  3. joit

    joit Active Member

    Level: Community
    sounds good, can you please give me an example?
     
  4. troester

    troester Well-Known Member Staff Member

    Level: Community
    Condition must return false if the validation should not be run.

    So assuming your validation is on element1
    and you have an element2 with JS "show element1 if this(element2)=='1'"
    set the condition of your element1 to something like

    if ('{your-table___element2_raw}' == '1') return true; //ie run the validation
    return false; //dont' run
     
    ontarget likes this.
  5. joit

    joit Active Member

    Level: Community
    So true! The javascript just checks, if the username is empty or not (new account or account changes). So I probably don't need the if condition, just the state of the username field.

    If I want to validate is this:
    element user is filled out AND (user group is 10 OR 12)

    So I tried something like this, but it won't work!

    return ('{abcd_users___username}' != '') && ('{userdetails___user_group}'==10 || '{userdetails___user_group}'==12);

    do you have a tip for me?
     
  6. joit

    joit Active Member

    Level: Community
    *friendly bump*
     
  7. troester

    troester Well-Known Member Staff Member

    Level: Community
    Is user_group a usergroups element?
    Then {userdetails___user_group} will return a string with groups comma separated "1,5,12,13"
     
  8. joit

    joit Active Member

    Level: Community
    the user group part works fine, I tested that already. It doesn't work in combination with
    "check if username isn't empty"
     
  9. troester

    troester Well-Known Member Staff Member

    Level: Community
    Ah, you are using {abcd_users___username}, I assume this is no element of your form but you have a user element userdetails___xy storing the userID?
    Use this one for checking.
     
    joit likes this.
  10. joit

    joit Active Member

    Level: Community
    Thanks, that did the trick!!!
     
  11. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    BTW, I'm working on a little improvement to add a "Don't validate if hidden by FX" option to the validations. So if you have a Javascript FX which hides an element or a group, the form will automatically track that, and (optionally) skip validation if an element was hidden on submit.

    Won't be ready for a while, as it's not entirely trivial, but it's on the way. Something I've been meaning to do for literally years.

    -- hugh
     
    mattsh and joit like this.
  12. bggann

    bggann Member

    Level: Community
    Jumpin' in here cause I'm not sure if I have the same need.

    I want to hide/show a group based upon a dropdown. The idea is to more easily customize what needs to be filled out for a particular type of entry. I was worried that if I have elements in a hidden group that have "not empty" validations - they would still run. I was going to try it and see - but.....
     
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Yes, correct. This thread is about exactly that. You need to check in the "Condition" for the validations on any fields in your (possibly) hidden group, to see if the value of the dropdown means that they would be hidden.

    -- hugh
     
  14. bggann

    bggann Member

    Level: Community
    Okay - thanks. This, and my "hide tabs" challenge may drive me another way. There are actually quite a number of fields for each "group" and it would be a bit of coding to add this to each validation. It may be simpler to create groups and forms that are separate, using linked elements (child/parent) for the "common" ones. I need to think about that. The issue is we want some unified reporting - and if I separate them too much, that breaks.....
     
  15. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    I'll be committing a change today some time to automate hiding / showing a tab when you hide / show its group.

    And I'm working on a way of automating that "don't validate if hidden by JS FX" thing, although I can't promise an ETA for that.

    -- hugh
     
  16. bggann

    bggann Member

    Level: Community
    Hey Hugh,

    I know you said no "eta" on this. Any update. Since you fixed the tabs, I'm wanting to move forward with the system using the hidden tabs, but of course, I need to handle validation for hidden fields. It is made a bit more complex because some of the tabs will show copies (child elements) of some of the fields on a hidden tab.
     
  17. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Still no eta.

    Sent from my HTC 10 using Tapatalk
     
  18. achartier

    achartier Active Member

    Level: Community
    How is this coming Hugh?
     
  19. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    OK, I just took an hour and figure out a relatively easy way to do it, which is fairly generic:

    https://github.com/Fabrik/fabrik/commit/616b16473ba9c7282bc377b2f99cd30bea254884

    The new "Validate Hidden" option defaults to Yes, so backward compatible with the existing behavior to always validate. If set to No, validation will be skipped if the element itself, or any ancestor (like the group or tab) has the fabrikHide class.

    The element's status is checked during submission of the form, where we now iterate through all the formElements, and do a jQuery closest('.fabrikHide') on each element, and if that isn't empty, add the element name to a hidden input, that the sever side shouldValidate() method checks.

    This works with the built in JS FX, as they add and remove that class to the element or group. It also makes it fairly easy for people to manipulate in custom code, by just adding or removing that class to whatever they want.

    NOTE - I haven't tested this with repeat groups, multipages or tabbed templates yet. Just simple forms with simple test cases - JS FX to hide/show elements and groups.

    Have a go, let me know.

    -- hugh
     
  20. troester

    troester Well-Known Member Staff Member

    Level: Community
    @ontarget: meanwhile the validations have an option "validate hidden" (although I'm not sure if this is working also if you are hiding via JS)
     

Share This Page