I have integrated confirmation window to my Symfony form with javascript function.
This is my start form definition:
{{ form_start(form, {'attr': {'role': 'form', 'id':'editForm'} } ) }}
And in javascript:
$('#editForm').on('submit', function (e) {
e.preventDefault();
validateEditForm();
});
function validateEditForm()
{
var nameInput;
nameInput = prompt('Please input name to confirm edit:');
var name = document.getElementById('edit_name').value;
if (nameInput === name || Number(nameInput) === name) {
$('#editForm').submit();
} else if (nameInput === null || nameInput === "") {
alert('You must enter name to confirm!');
return false;
} else {
alert("Entered name and default name don't match!");
return false;
}
One case is not working.
When I enter correct name in the box then Please input workspace name to confirm edit: appears again. Then I click OK again and the form is submitted.. Is there something wrong with my if else checks?
Edit: Tried to do it in two separate functions and used e.preventDefault(); and the behaviour was the same.
I change validateEditForm method as returning boolean. On the form submit event, validateEditForm method decides whether submit the form or not.
$('#editForm').on('submit', function (e) {
if(!validateEditForm()) {
e.preventDefault();
}
});
function validateEditForm()
{
var nameInput;
nameInput = prompt('Please input name to confirm edit:');
var name = document.getElementById('edit_name').value;
if (nameInput === name || Number(nameInput) === name) {
return true;
} else if (nameInput === null || nameInput === "") {
alert('You must enter name to confirm!');
return false;
} else {
alert("Entered name and default name don't match!");
return false;
}