How do I disable the submit button until the user has chosen a file?
I have a form on my page:
<table>
<form method="post" enctype="multipart/form-data" id="messageupload">
<@spring.bind "messageUploadCommand.*"/>
<tr>
<td><@spring.formInput 'messageUploadCommand.multipartFile' '' 'file' /></td>
<td><button type="submit" id="searchButton">Upload</button></td>
<td><@spring.formSingleSelect
'messageUploadCommand.messageFormat',
messageFormats, '' />
<@spring.showErrors '<br>', 'error' /> </td>
</tr>
</form>
</table>
If the user submits the form before choosing a file I get
freemarker.template.TemplateException: Error executing macro: formSingleSelect required parameter: options is not specified.
at freemarker.core.Macro$Context.sanityCheck(Macro.java:211)
at freemarker.core.Macro$Context.runMacro(Macro.java:169)
at freemarker.core.Environment.visit(Environment.java:603)
at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.Environment.process(Environment.java:190)
at freemarker.template.Template.process(Template.java:237)
at ...
This is how I did it:
I added the file-chooser onchange script via a Freemarker variable, and disabled the button by default. The onchange script tests for the length of the value, i.e. the filename, being zero.