Skip to content

Fix #309 -- handle submit buttons form* attributes #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ target/
node_modules

*.min.js
.python-version
34 changes: 18 additions & 16 deletions s3file/static/s3file/js/s3file.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,7 @@
})
}

function clickSubmit (e) {
var submitButton = e.currentTarget
var form = submitButton.closest('form')
var submitInput = document.createElement('input')
submitInput.type = 'hidden'
submitInput.value = submitButton.value || '1'
submitInput.name = submitButton.name
form.appendChild(submitInput)
}

function uploadS3Inputs (form) {
function uploadS3Inputs (form, submitter) {
window.uploading = 0
form.loaded = 0
form.total = 0
Expand All @@ -136,6 +126,22 @@
window.uploading += 1
uploadFiles(form, input, input.name)
})

if (submitter) {
// override form attributes with submit button attributes
form.action = submitter.getAttribute('formaction') || form.action
form.method = submitter.getAttribute('formmethod') || form.method
form.enctype = submitter.getAttribute('formEnctype') || form.enctype
form.formnovalidate = submitter.getAttribute('formnovalidate') || form.novalidate
form.target = submitter.getAttribute('formtarget') || form.target
// add submit button value to form
var submitInput = document.createElement('input')
submitInput.type = 'hidden'
submitInput.value = submitter.value || '1'
submitInput.name = submitter.name
form.appendChild(submitInput)
}

waitForAllFiles(form)
}

Expand All @@ -147,11 +153,7 @@
forms.forEach(function (form) {
form.addEventListener('submit', function (e) {
e.preventDefault()
uploadS3Inputs(e.target)
})
var submitButtons = form.querySelectorAll('input[type=submit], button[type=submit]')
Array.from(submitButtons).forEach(function (submitButton) {
submitButton.addEventListener('click', clickSubmit)
uploadS3Inputs(e.target, e.submitter)
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_file_insert_submit_value(
with wait_for_page_load(driver, timeout=10):
save_button.click()
assert "save" in driver.page_source
assert "formaction" not in driver.page_source

driver.get(live_server + self.create_url)
file_input = driver.find_element(By.XPATH, "//input[@name='file']")
Expand All @@ -197,6 +198,7 @@ def test_file_insert_submit_value(
save_button.click()
assert "save_continue" in driver.page_source
assert "continue_value" in driver.page_source
assert "formaction" in driver.page_source

@pytest.mark.selenium
def test_progress(self, driver, live_server, upload_file, freeze_upload_folder):
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% csrf_token %}
{{ form }}
<input type="submit" name="save" value="Save"/>
<button type="submit" name="save_continue" value="continue_value">Save &amp; continue</button>
<button type="submit" name="save_continue" value="continue_value" formaction="?formaction=1">Save &amp; continue</button>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0"
aria-valuemax="100">0%
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def form_valid(self, form):
"file": self.request.FILES.getlist("file"),
"other_file": self.request.FILES.getlist("other_file"),
},
"QUERY_STRING": self.request.GET,
},
status=201,
encoder=FileEncoder,
Expand All @@ -45,6 +46,7 @@ def form_valid(self, form):
"file": self.request.FILES.getlist("file"),
"other_file": self.request.FILES.getlist("other_file"),
},
"QUERY_STRING": self.request.GET,
},
status=201,
encoder=FileEncoder,
Expand Down