Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Enable / Disable a control on Vanilla Form

BeyBey
edited December 2011 in Vanilla 2.0 - 2.8

Is it possible to disable / deactivate a control (say, drop-down or text box) on a vanilla form - say, on the Render_Before or Render_After events?

If so, any example(s) is(are) appreciated.

Best Answers

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    easy way is to use javascript/jquery

    There was an error rendering this rich post.

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    edited December 2011 Answer ✓

    what i would do is :

    in my hooks.php in Base_Render_Before() Event

    $Sender->Head->AddString(' < script type="text/javascript" >
    jQuery(document).ready(function($) {
    $('#Form_CategoryID').attr('disabled' , 'disabled');
    });
    < /script>');

    the above code with be added to your head section and disable the Select with ID - 'Form_CategoryID'

    There was an error rendering this rich post.

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    do you have it live? i can check it out if the string was added or not?

    There was an error rendering this rich post.

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    edited December 2011 Answer ✓

    AddString did add it to your page. Then it might have been an ID problem or something else i guess

    my fault i think try $("#Form_CategoryID").attr("disabled", true);

    There was an error rendering this rich post.

Answers

  • BeyBey
    edited December 2011

    To clarify (for example, the category control on a new discussion page).
    Note i replaced angled brackets with [ and ]

    At present:

    [select id="Form_CategoryID" name="Discussion/CategoryID"]

    In the Render_Before event, i want it to make it like:

    [select id="Form_CategoryID" name="Discussion/CategoryID" disabled="disabled"]

    How can this be done?

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    easy way is to use javascript/jquery

    There was an error rendering this rich post.

  • I was thinking along those lines (and noticed similar code using Attr(..) function in numerous VF jscript files) but, being a "newbee", i am struggling with some basic holes in understanding such as:

    1. Location of the .js file (i would imagine the plugin's folder but am not sure)...

    2. What is the object sent to the jscript file ($Sender->Form ?).

    3. I assume the code should be invoked in the Render_Before event but, again, not sure

    I will continue exploring this line of thought but if you have a 'short cut' or more specific example, either will be greatly appreciated.

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    edited December 2011 Answer ✓

    what i would do is :

    in my hooks.php in Base_Render_Before() Event

    $Sender->Head->AddString(' < script type="text/javascript" >
    jQuery(document).ready(function($) {
    $('#Form_CategoryID').attr('disabled' , 'disabled');
    });
    < /script>');

    the above code with be added to your head section and disable the Select with ID - 'Form_CategoryID'

    There was an error rendering this rich post.

  • Feedback from my trials and tribulations...

    1. As is, it crashed - i needed to change all single quotes within the outmost AdString to double quotes. Then, it did not crash... But:

    2. The string was added to the header and was visible - but no action taken. So...

    3. I changed the verb to AddScript instead of AddString. Result: no crash and no action either.
      So, i poked around and saw various usages of $(frm).find({specs}), so...

    4. I tried several 'find's by changing $('#Form_CategoryID')... to
      $(frm).find("[name$=Form_CategoryID"])... or $(frm).find("#Form_CategoryID"])... none of which worked.

    What i think is happening is that the script is either not added (I don't see it in FireBug) or does not run (I introduced into the script '1/0;' statement at the end to see if it is an issue of not finding the control or not running and i got no crash - so, i think, the script is not running.

    Sorry for the lengthy response...

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    Answer ✓

    do you have it live? i can check it out if the string was added or not?

    There was an error rendering this rich post.

  • No, I host it locally.

    FireBug is great - i can see lots of scripts there (12, in fact, in the Head section) but did not see the one i tried to add. When i used 'AddString' - the script string was visible on the page (I did not need FireBug).

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭
    edited December 2011 Answer ✓

    AddString did add it to your page. Then it might have been an ID problem or something else i guess

    my fault i think try $("#Form_CategoryID").attr("disabled", true);

    There was an error rendering this rich post.

  • I think your initial use of the setting is correct and is not the issue. For example:

    In line 76 of discussion.js there is a statement:

      $(frm).find(':submit').attr('disabled', 'disabled');
    

    I tried this syntax in one of my attempts (see trials and tribulations above) and did not have any success.

    It seems to me that the script does not get executed.

    Using FireBug (learning new things everyday) I was able to locate it on the page...

    I find the script in the DOM tab with the path of:

    window > frames > document > head > childNodes

    It is there in one of the nodes but hibernating, i guess. How do i kick it to spring into action?

  • Saho,

    Finally, i figured it out.

    Your idea was totally valid the problem was some imbalanced parentheses and braces, etc. (aside of large gaps in my knowledge of jQuery and how the .js scripts work to begin with).

    After taking the time to learn just enough of jQuery and using FireBug to sort out the issues (and gain a good balance on my parentheses) i was able to rise to the challenge and disable the Category drop down on the new discussion post page.

    I got the exact effect i wanted, namely, the selected category was visible but the drop down was disabled so it could not be changed (this was the purpose of this saga).

    But my happiness was short lived as I realized an 'interesting feature' or Vanilla bug. When the drop down is disabled (though a selection exists), Vanilla considers it unselected and assigns the discussion the Root category.

    In my book it's a bug.

    Following this disappointment and now armed with some jQuery knoledge I took the approach of removing all but the selected option. Now, I have my cake and can eat it too: The user cannot change the category (in some cases) and Vanilla posts the correct value.

    Again, i thank you for your help.

    Bey

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭

    Hey Bey can post this issue with Vanilla devs at Github. they can come up with solution :)

    There was an error rendering this rich post.

  • As i am new to this, a link would be appreciated... My cursory search did not yield a page with Github Vanilla (and i am on my way out of town).

    Thanks

  • sahotataransahotataran Developer, Bay Area - CA ✭✭✭

    There was an error rendering this rich post.

  • hbfhbf wiki guy? MVP

    my guess is that as a disabled field, the post may not be returning the value to the controller. i would do a little snooping to see if this is the case.

    if it is you could hide the drop down field rather than disable it, then the result should be posted properly. you could add a text field to the view to display the value of your hidden field, in effect making it read only, but visible.

  • Thanks.
    I am curious about the result of the snooping.

    I am happy with the solution i came up with as it involves less changes on my part and involves less changes to the user's experience. By removing all but the desired item from the drop down list I accomplish the same thing. Also, I am finished doing all the work and can move on to the next task. Still, thanks for sharing yet another approach that may be useful in some other scenario.

Sign In or Register to comment.