0

I modified the feedback form of dspace code to add a category that will ask the user for their 'user type'.

    Select category = form.addItem().addSelect("category");
    category.setLabel("Please select your category");
    category.addOption("UG","Undergraduate/BS");
    category.addOption("MS","MS Student");
    category.addOption("PHD","PhD Student");
    category.addOption("FAC","Faculty");
    category.addOption("RES","Researcher");
    category.addOption("TRA","Trainee");
    category.addOption("BUS","Businessman/Private");
    category.addOption("FF","Fish farmer");
    category.addOption("OT","Other");

    String itemSelected = parameters.getParameter("category","");
    if (StringUtils.equals(itemSelected,"OT"))
    {
        TextArea other = form.addItem().addTextArea("other");
        other.setHelp("Write here if you selected Other");
        other.setValue(parameters.getParameter("other",""));
    }

My goal here is to display a text area only if the user selected Other. Also, the text area should be a required field if the user choose other. How can I achieve this? I would also like to try using radio buttons instead of select.

[EDIT] For those who are not familiar with DSpace, here's the original code that I modified from DSpace github: feedback form

euler
  • 1,401
  • 2
  • 18
  • 39
  • In terms of your first goal, what's the code you posted doing (or not doing) now? In terms of your second goal, the typical approach would be to disable the your "submit" button until some number of characters have been typed into the text field. – drew moore Sep 21 '14 at 01:16
  • @drewmoore, there's no text area being displayed if the user selected "Other". I have taken care of the required field by using addError if the category selected is "Other" and TextArea "other" is empty. – euler Sep 21 '14 at 02:13
  • This page is being created using a framework of GUI components built into DSpace. These components are ultimately rendered as a web page. I do not know if the component library will allow you to add a text area that is dependent on another field. If you are unable to create the behavior that you desire within the Java code, you can insert some jQuery code on to your page that will manipulate the widget behavior when rendered as a web page. Click the "feedback" link in the footer of http://demo.dspace.org/xmlui/ to see an example of this page. – terrywb Sep 22 '14 at 16:01
  • @terrywb, if I'll use javascript, how can I pass the values entered in the text area to send the feedback action? – euler Sep 22 '14 at 16:29
  • @euler, I presume that you will need to create the text area in the Java code in order to pass the values. (I am guessing since I have not modified that part of DSpace.) I recommend using the javaScript to control the hiding and showing of the text area and the enforcement of required fields. – terrywb Sep 22 '14 at 16:45

2 Answers2

3

You can use radio buttons the same way you used the select:

Radio category = form.addItem().addRadio("category");
category.setLabel("Please select your category");
category.addOption("UG", "Undergraduate/BS");
...

Like terrywb said, the java code renders the page server side. You could make a 2 step process where the first step only submits the category. But including a hidden TextArea by default and then showing it using javascript is more user friendly.

Item item = form.addItem("item-name","hidden");
TextArea other = item.addTextArea("other");

Add a reference to the javascript file in the PageMeta:

public void addPageMeta(PageMeta pageMeta) throws SAXException,
            WingException, UIException, SQLException, IOException,
            AuthorizeException
    {       
        pageMeta.addMetadata("javascript", "static", null, true).addContent("static/js/feedbackform.js");
        // leave the rest
        ...
    }

Put the javascript in /dspace/modules/xmlui/src/main/webapp/static/js/feedbackform.js so it can be served.

Also parameters.getParameter("category",""); won't work out of the box. The parameters instance variables contains parameter given to it by cocoon in /aspects/ArtifactBrowser/sitemap.xmap:

<map:match pattern="feedback">
        <map:act type="SendFeedbackAction">
                <map:transform type="FeedbackForm">
                        <map:parameter name="comments" value="{comments}"/>
                        <map:parameter name="email" value="{email}"/>
                        <map:parameter name="page" value="{page}"/>
                </map:transform>

                <map:serialize type="xml"/>
        </map:act>
        <map:transform type="FeedbackSent"/>
        <map:serialize type="xml"/>
</map:match>

The SendFeedbackAction gets the data from the request:

Request request = ObjectModelHelper.getRequest(objectModel);
String page = request.getParameter("page");

And provides the value for {page} by adding it to the map that it returns:

    // Check all data is there
    if ((address == null) || address.equals("")
        || (comments == null) || comments.equals(""))
    {
            // Either the user did not fill out the form or this is the
            // first time they are visiting the page.
            Map<String,String> map = new HashMap<String,String>();
            map.put("page",page);
            ...
            return map;

The same happens for comment, email and all other fields you would like to see. Enforcing a required field should be done in there as well.

Antoine Snyers
  • 682
  • 3
  • 7
  • Thanks for pointing out editing the sitemap, that also solved my issue of not retaining values if I clicked submit with some fields missing. I choose select because radio buttons take up more space. I'm not sure if I should post this as a separate question, but I wonder if can I add the title (or any metadata) of the referring page assuming for example that this page can only come from an item page? I've seen that the url of the referring page is captured and passed to SendFeedbackAction. Please advice. Thanks a lot... – euler Sep 25 '14 at 03:03
  • You could try parsing the referer url for the handle and use `HandleManager` to get the item object. – Antoine Snyers Sep 25 '14 at 10:53
0

If I were you I would use a JTextArea instead of TextArea...

public void valueChanged(ListSelectionEvent e) {
    if ( e.getSource() == item) { /*Item is whatever the selected choice you want is*/ ) {
        other.setVisible(true);
    } else {
        other.setVisible(false);
    }
}

Be sure your class extends ActionListener! And that JTextArea other is public! ;)

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
  • I modified the [feedback form](https://github.com/DSpace/DSpace/blob/master/dspace-xmlui/src/main/java/org/dspace/app/xmlui/aspect/artifactbrowser/FeedbackForm.java) of [DSpace](http://www.dspace.org) to add a category. Sorry, I'm not a java programmer and I don't know if this is possible to add JTextArea if the code already have `public class FeedbackForm extends AbstractDSpaceTransformer implements CacheableProcessingComponent`? – euler Sep 21 '14 at 07:34
  • Just `import javax.swing.JTextArea` at the top! – Aaron Esau Sep 21 '14 at 23:00
  • Sorry, where should I add extends ActionListener? I add extends ActionListener in my class and my IDE says "must either be declared abstract or implement abstract method 'actionPerformed(ActionEvent)' in 'ActionListener" – euler Sep 22 '14 at 00:45