PDA

View Full Version : Enable/disable forms/fields depending on user level



LeonM
7 Jan 2011, 2:43 AM
I have a form with multiple tabs that allows users to update their account information and various other preferences.

Depending on the level of the user (guest/registered user/local admin/superuser) I want to do 2 things:
- Hide certain tabs.
- Disabled some fields (user should still be able to see the value)

The data is read/written by AJAX requests using JSON encoding.

The tabs:
For the tabs I figured to use a separate reader for each tab, and then hide (or not enable) a tab whenever the AJAX request for that form returns an empty array. Is this a good practice, or should I use a different approach?

The fields:
I don't have a clue how to tackle this one, as far as I can find there is no "disableField" or "writable" property that I can add to my JSON string for each field.
I could add additional fields to the reader like this:
"Value1",
"Value1Enabled",
"Value2",
"Value2Enabled"
And then write a function that checks each field and disables the corresponding field in the form whenever a FALSE value is encountered. But that doesn't sound like the most elegant solution.

Any thoughts?

Condor
7 Jan 2011, 2:58 AM
Is this one single form with multiple tabs with fields on them? Then why use a store?

You should just load the form and in the actioncomplete event update things like tab visibility and field disabled state.

LeonM
7 Jan 2011, 3:39 AM
Is this one single form with multiple tabs with fields on them? Then why use a store?

You should just load the form and in the actioncomplete event update things like tab visibility and field disabled state.

Well, one tab contains a grid, so I figured I had to use a TabPanel instead of a single form with tab layout. As far as I know, it is not possible to split a single form over multiple tabs of a TabPanel (or is it?).

So each tab in the tabpanel contains it's own form, which has it's own reader (not a store, that was actually a typo in the startpost).

Now that I think of it, I could do the following:
- Tab is hidden by default
- form autoLoads.
- JSON success property returns TRUE if user is allowed to see tab.
- Tab containing the form is displayed

I shall have a look at the actioncomplete event, I suppose I need to make a function that checks the JSON string for "fieldDisabled == true" and then disable the appropriate field? Or is there a better solution?

Condor
7 Jan 2011, 3:52 AM
Well, one tab contains a grid, so I figured I had to use a TabPanel instead of a single form with tab layout. As far as I know, it is not possible to split a single form over multiple tabs of a TabPanel (or is it?).

Just create a FormPanel and put the TabPanel inside it. The tabpanel will need to be created with deferredRender:false to make all fields (even the invisible ones) available for loading/submitting.


I shall have a look at the actioncomplete event, I suppose I need to make a function that checks the JSON string for "fieldDisabled == true" and then disable the appropriate field? Or is there a better solution?

You could just look through all fields and check if they should be enabled, e.g.

formPanel.on('actioncomplete', function(form, action){
form.items.each(function(field){
if (field.setDisabled && field.getName) {
var disabled = action.result.data[field.getName() + 'Disabled'];
field.setDisabled(disabled === true);
}
});
});