Can I use visibility rules with user group?

  • 5uwebsite
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 125
  • Thank you received: 2

5uwebsite created the topic: Can I use visibility rules with user group?

Hi there,

Visibility rule is great! It can be used to deal with many different situations. May I ask whether I can use the current user's user group as a condition? For example, the user of the "Public" group can see an element, while the user of the "Registered" group will see another element. Is that possible?

Thanks,
#233018
  • Posts: 2483
  • Karma: 86
  • Thank you received: 335

mihaela replied the topic: Can I use visibility rules with user group?

Hello,

Yes, it is possible to achieve what you want.

Here is how you can achieve what you want.
If user is logged in (registered), he will have a user ID assigned. On the other hand if the user is not logged in when filling in the form he won't have assigned user ID.

Therefore, you can make this work in a way that you fill a hidden form field with user's ID and then show/hide fields depending on the value of that field.

So, to achieve what you want, follow this steps:

1) Create a Hidden input field in your form named "userid". In the Value field in that element's properties put the following code:
<?php $user = JFactory::getUser(); return "$user->id"; ?>

2) Let's assume that you have fields "field1" and "field2" and that you want to display field1 for public and field2 for logged in users.
Then you would write this visibility rules:
turn off element field1 if userid isnot 0
turn off element field2 if userid is 0

turn on element field1 if userid is 0
turn on element field2 if userid isnot 0

This is an example. You can write as many visibility rules as you want.

The important part is that public will have user ID "0", and logged in users (registered) won't.

Let me know whether this helped and whether you've managed to set this for your form.

Regards,

Mihaela
#233097
  • 5uwebsite
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 125
  • Thank you received: 2

5uwebsite replied the topic: Can I use visibility rules with user group?

Hi Mihaela,

Now we have another question about turn on and off conditional set up.

There are two fields that only can display one each at the time in the form for different user group (both of the user groups are registered groups).

Please check this screenshot's link: mrkr.io/s/5c339dc03262a8237cbc02be/0

There are two user groups with id from Joomla backend:

Non-verified member (ID: 10) and Verified member (ID: 11)

The question is how to write the turn on/off, when I want"Used_or_Leased" radio button field display in the form when a user logged in as a "Non-verified member".

On the other hand, I want a login user who is in verified user group to see "Certificated_or_show" field only.

Here is the formula I write but it does not work:
turn off element Used_or_Leased if userid isnot 10
turn off element Certificated_or_show if userid is 10
turn on element Used_or_Leased if userid is 10
turn on element Certificated_or_show if userid isnot 10


Please help us take a look, and provide the solution.

Thank you for your patient.
#233108
  • Posts: 2483
  • Karma: 86
  • Thank you received: 335

mihaela replied the topic: Can I use visibility rules with user group?

Hello again,

In that case you need to write an SQL query to get the id of the usergroup the user belongs to.

Do get that id do as follows.
Go to that form's Advanced properties > More options > Form pieces and in Before Form section set Type to Custom.
In the code area below put code like this:
$this->execPieceByName('ff_InitLib');

$user = JFactory::getUser(); 
$user_id ="$user->id"; 

if($user_id == '0' ) {
    $group_id = 0;
    }
else {
    $db = JFactory::getDBO();
    $db->setQuery("SELECT group_id FROM #__user_usergroup_map WHERE user_id =". $user_id);
    $group_id = $db->loadResult(); 
    }

ff_setValue('usergroupid', $group_id);
NOTE: The code above will fill the field named usergroupid with the usergroup id.
In case the user isn't logged in, the field would be filled with 0.

Then you would write visibility rules like this:
turn off element Used_or_Leased if usergroupid isnot 10
turn off element Certificated_or_show if usergroupid isnot 11
turn on element Used_or_Leased if usergroupid is 10
turn on element Certificated_or_show if usergroupid is 11

Let me know whether this worked as you wanted.

Regards,

Mihaela
#233174
  • 5uwebsite
  • Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 125
  • Thank you received: 2

5uwebsite replied the topic: Can I use visibility rules with user group?

Hi Mihaela,

Thank you for your response.

I have followed your instruction and added the code, please check the screenshot's link:
mrkr.io/s/5c3650dfc26e6c2e5551916a/0

Also, I rewrite the visibility rules (you gave it to me) and here is the screenshot's link:
mrkr.io/s/5c3650bfc26e6c2e55519169/0

But, when I log in one of the registered group as a Non-verified member (ID: 10) and open the form, the field "Certicated_or_show" also display at the front-end.
(It should only display "Used_or_Leased" field when a Non-verified member (ID:10) fill the form; and display "Certicated_or_show" field when Verified member (ID: 11) login in and fill the form)

*Different user groups (Non-verified member (ID:10) and Verified member (ID: 11) ) show different fields ("Used_or_Leased" field and "Certicated_or_show" field) and one field only at a time.

*The field's label is in Chinese, but the name is Certificated_or_show, please check the image:
mrkr.io/s/5c36531e4c4ef023655007e0/0

Just want to make sure the visibility rules code is in a correct order
#233190
  • Posts: 2483
  • Karma: 86
  • Thank you received: 335

mihaela replied the topic: Can I use visibility rules with user group?

Hello again,

I understand how you want this to work.

The visibility rules I've provided should work.

Here are few things that might not be set correctly so please check the following:

1) Go to Advanced properties of the "Certicated_or_show" element and check whether you have checked the "Turn off" checkbox. Do the same for "Used_or_Leased" element.

2) Check whether you have an element in your form named "usergroupid" which will be filled with the group id.

Let me know whether you had everything set like that or this resolved the issue.

Regards,

Mihaela
#233205
Moderators: ForumSupporttomeperica
Time to create page: 0.071 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here