Directory

← Back

Questionnaire

Questionnaire add-on provide simple way to create questionnaires such as customer feedback or employee survey.

Author

Rating

Questionnaire add-on provide simple way to create questionnaires such as customer feedback or employee survey. User just create question set and set those to questionnaire component.

Questionnaire should contain at least one question and multiple choice questions should contain at least two alternative answers. Question behavior can be declared with changing its type. These types are TEXTFIELD, TEXTAREA, CHECKBOX and RADIOBUTTON.

Each question type could be set required and user answer length is limited. Length limitation can be customized but default values will be used if not set. Textfield default max length is 255 characters and textarea default max length is 4000 characters. User answers are in plain text format in any question type. Radiobutton is always required due its nature. First alternative answer will be set selected by default.

Validator messages and required field indicators can contain HTML so these can be customized e.g. images and other resources.

Sample code

QuestionSet questionSet = new QuestionSet();
questionSet.setText("Testing addon");
questionSet.setDescription("This is questionnaire testing.");
questionSet.setId(1);
questionSet.setSubmitButtonText("Send");

Question q1 = new Question(1, "Enter your name");
q1.setAnswerMaxLength(100);
q1.setRequired(true);
q1.setRequiredIndicator("*");
q1.setRequiredError("Name is required!");
questionSet.add(q1);

Question q2 = new Question(2, "Description", QuestionType.TEXTAREA);
q2.setAnswerMaxLength(4000);
questionSet.add(q2);

Question q3 = new Question(3, "Interestings", QuestionType.CHECKBOX);
q3.setRequired(true);
q3.setRequiredIndicator("*");
q3.setRequiredError("Interestings are required");
q3.addAnswer("Sports");
q3.addAnswer("Music");
q3.addAnswer("Movies");
q3.addAnswer("Books");
questionSet.add(q3);


final Questionnaire questionnaire = new Questionnaire();
questionnaire.addClickListener(new SubmitButtonClickListener() {
	@Override
	public void buttonClick(SubmitButtonClickEvent event) {
		final List<UserAnswer> answers = questionnaire.getUserAnswers();
		StringBuilder sb = new StringBuilder();
		if (answers != null) {
			sb.append("Save to database:");
			for (UserAnswer answer : answers) {
				if (sb.length() > 0) {
					sb.append("\n");
				}
				sb.append(answer.getQuestionId() + ": "
						+ answer.getValue());
			}
		} else {
			sb.append("No data");
		}
		Notification.show(sb.toString());
	}
});
questionnaire.setDataSource(questionSet);
layout.addComponent(questionnaire);
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        QuestionSet questionSet = new QuestionSet();
        questionSet.setText("Questionnaire add-on online demo form");
        questionSet
                .setDescription("Lorem ipsum dolor sit amet...");
        questionSet.setId(1);
        questionSet.setSubmitButtonText("Send");

        Question q1 = new Question(1, "Enter your name");
        q1.setAnswerMaxLength(100);
        q1.setRequired(true);
        q1.setMaxLengthError("<span class=\"validator-error\">Maximum length is 100 character</span>");
        q1.setRequiredIndicator("<span title=\"This is required field\" class=\"required\" />");
        q1.setRequiredError("<span class=\"validator-error\">This is required field</div>");
        questionSet.add(q1);
...


@styles.css

.required {
	background-image:url('required.png');
	background-repeat:no-repeat;
	background-size:8px 8px;
	background-position: left center;
	padding: 2px;
	height: 10px;
	float: left;
}
.validator-error {
	font-size: 12px;
	color: red;
	background-color: pink;
	background-image:url('error.png');
	background-repeat:no-repeat;
	background-size:8px 8px;
	background-position: left center;
	padding: 2px;
	height: 10px;
	color: black;
	float: left;
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Validation html-indicator/error bug fix

Released
2013-03-18
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.0+
Browser
Firefox
Google Chrome
Internet Explorer
Online