Page 1 of 1

Hide fields based on user

PostPosted: Fri Aug 11, 2017 9:15 pm
by njw
Is there a simple way to hide fields ona form based on the user id? I can see that I can set up separate forms for each user, but the ability to hide fields would simplify maintenance!

Many thanks


Re: Hide fields based on user

PostPosted: Mon Aug 14, 2017 9:08 pm
by njw
I decided the simpler way was to create a different form for each user, with the fields relevant to each shown. However, that appears to mean that i need a different home screen for each user? Or is there a simpler way?

Many thanks for any help


If it helps, the application is for judging an awards event and I need each judge to be abel to rank the entrants without seeing the other judge's rankings.

Re: Hide fields based on user

PostPosted: Fri Aug 18, 2017 3:08 pm
by mobilemcclintic
Neil, if you want to hide/show specific fields based on user name, user group, or access level, you can do the following:
On the item's All tab (where it shows what form it's on, how big it is, etc) in the Display Condition do something like this

SELECT IF(('#nu_user_id#' = bobb' OR '#nu_access_level#' = 'FOO' OR '#nu_user_group#' = 'CODE'), '1','0')

if the user id (think it's their login, i only use it for globeadmin) matches bobb, it is visible.
If the user's access level code matches FOO, it is visible
if the user's group code matches CODE, it is visible.
This basically matches Excels' IF formula, if(condition to check, what to do if true, what to do if false) 1 will mean show, 0 will mean hide.

Something you can do that works good for tabs (spend some time with tabs and tab numbers, it'll make the drag and drop gui easier to use) is to show or hide a tab based on a user/group/access level.
In the form's Custom Code tab under javascript, i use the following inside function nuLoadEdit()

// Only allow Sales and Tech Support to view FOO tab.
if('#nu_user_id#' != 'globeadmin' && '#nu_user_group#' != 'SAL' && '#nu_user_group#' != 'TS'){
$('#tab5').remove(); //--eg. the Foo tab

to enable/disable fields (if disabled they can see it but not change it),

if('#nu_user_id#' != 'globeadmin' && '#nu_user_group#' != 'SAL' && '#nu_user_group#' != 'TS'){

More out of scope than what you asked, but you can create custom functions that set, enable, change, etc fields as well.
They're pretty easy on main forms, and a bit more complicated on subforms.

On field to watch, add onchange event with foofunction();

on custom code tab for form under javascript, create the function:

function (){
if ($('#fieldblah').val() === 'Beth'){

I'm rambling, sorry. I am also just learning javascript, so my code isn't perfect, but I know when I add new requirements to my forms, I have trouble sometimes getting answers here, so I try and share what I can when I can.

Re: Hide fields based on user

PostPosted: Fri Aug 18, 2017 3:19 pm
by mobilemcclintic
Neil, just another thought after I stopped my rambling.
Maybe you could create a voter's card/ranking card with the scoring system on it as 1 form. Add a Judge field with default value of select '#nu_user_name#'.
Create a form to use to manage contestants.
on the SQL for the browse of the voters cards/ranking card, filter to only include records that have the judge's name/id assigned on it. That way, they only see scores for what they have ranked. At the same time, they can still add another new scorecard.
There is an added bonus that you don't have to query separate fields for separate judges when you tally them up. Also, adding more contestants is as easy as just entering a new name into the contestant form.

Re: Hide fields based on user

PostPosted: Thu Aug 24, 2017 11:15 pm
by njw
Many thanks mobilemcclinic. I had got to somewhere similar to you - using the Custom code. That kind of works, other than #nu_user_id# has a hash value of some kind, not the login user id. I can probably use #nu_user_name# insteaad, but that'll mean changing the code every time I change a judge.

For this application, I don't think tabs would work, but it is a clever idea!

Ah well.

Re: Hide fields based on user

PostPosted: Sat Aug 26, 2017 2:27 pm
by mobilemcclintic
'#nu_user_id#' is the index field of the user.
Don't throwing it out there, if you were to hide everybody else's score cards,
select * from scorecard where judge = '#nu_user_id#' on the form sql would only show a judge his own scorecards.
(assuming you had a lookup or something that stored the judge's id in field judge)

If you wanted to only let a judge score a person once AND not see the names of people they already scored, you could get creative and do something like create a lookup or dropdown using mysql for the contestants and use a sub query inside the lookup/dropdown query to only show un-scored contenstants...
for dropdown:
select contestant_id, contestant_name from contestant where contestant_id not in (select contestant from scorecard where judge = '#nu_user_id#')
as I hinted at above, if a judge already scored a contestant and opened up the form, the name would show blank because the query excludes the names/ids of contestants that have already been done.

I believe a lookup would require a form for the contestants to be created (not a bad idea)

Re: Hide fields based on user

PostPosted: Wed Sep 13, 2017 5:47 pm
by sindyciaray
For this application, I don't think tabs would work, but it is a clever idea! read this PhenQ post

Re: Hide fields based on user

PostPosted: Tue Sep 19, 2017 5:25 pm
by sysysy
i totally agre with njw about that.....