2984 PO 5) Utilise client side validation to maintain the selected file when trying to submit the plagiarism analysis without writing a comment.

Browsers, as a security measure, will clear out the file input and prevent it from being set using JavaScript.
This commit is contained in:
Andreas Svanberg 2023-10-19 14:51:41 +02:00
parent 88bbf06514
commit b5a3e59a53
2 changed files with 36 additions and 4 deletions
view/src/main/java/se/su/dsv/scipro

@ -0,0 +1,33 @@
package se.su.dsv.scipro.components;
import org.apache.wicket.markup.html.form.SubmitLink;
/**
* This will submit the form using <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit">requestSubmit()</a>
* instead of <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit">submit()</a>.
* <p>
* {@code requestSubmit()} will act as if a regular {@code <button type="submit">Submit</button>} is clicked,
* running all client-side validation and if it all passes, raise a {@code SubmitEvent},
* which when received back down to the form will submit it if it has not been prevented using {@code preventDefault()}.
*/
public class ClientValidatingSubmitLink extends SubmitLink {
public ClientValidatingSubmitLink(String id) {
super(id);
}
@Override
protected final boolean shouldTriggerJavaScriptSubmitEvent() {
return false;
}
/**
* @implNote
* This is reliant on the implementation of {@link org.apache.wicket.markup.html.form.Form#getJsForSubmitter}.
*/
@Override
protected final CharSequence getTriggerJavaScript() {
return super.getTriggerJavaScript()
.toString()
.replace("f.submit()", "f.requestSubmit()");
}
}

@ -5,11 +5,9 @@ import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.feedback.FencedFeedbackPanel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
@ -19,6 +17,7 @@ import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import se.su.dsv.scipro.components.ClientValidatingSubmitLink;
import se.su.dsv.scipro.file.FileDescription;
import se.su.dsv.scipro.file.FileReference;
import se.su.dsv.scipro.file.FileService;
@ -124,7 +123,7 @@ public class UploadTextMatchingPanel extends GenericPanel<Project> {
plagiarismDetected.setOutputMarkupPlaceholderTag(true);
add(plagiarismDetected);
buttons.add(new SubmitLink("no_plagiarism") {
buttons.add(new ClientValidatingSubmitLink("no_plagiarism") {
@Override
public void onSubmit() {
noPlagiarismDetected();
@ -144,7 +143,7 @@ public class UploadTextMatchingPanel extends GenericPanel<Project> {
"include_text_matching_document_in_student_feedback", Model.of(false));
plagiarismDetected.add(includeTextMatchingDocumentInStudentFeedback);
plagiarismDetected.add(new SubmitLink("send_feedback_to_students") {
plagiarismDetected.add(new ClientValidatingSubmitLink("send_feedback_to_students") {
@Override
public void onSubmit() {
minorPlagiarismDetected();