Last active
October 31, 2016 18:47
-
-
Save 7iomka/3b8d04d7922f42e8c3c68868091bac4e to your computer and use it in GitHub Desktop.
SETTINGS FOR ALL FORMS INSIDE REMODA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************** SETTINGS FOR ALL FORMS INSIDE REMODAL *************************************/ | |
$(document).on('opening', '.remodal', function(e) { | |
// $(e.target).hasClass('remodal--action'); | |
}); | |
$('.remodal--action').each(function() { | |
var $context = $(this), | |
$form = $(this).find('form'), | |
$form_id = $context.data('remodal-id'); | |
$form.submit(function(e) { | |
e.preventDefault(); | |
}); | |
var rules = {}, | |
messages = {}; | |
var namespaces = ["user_name", "user_phone", "user_email"]; | |
$.each(namespaces, function(i, value) { | |
var $form_elements = $('input[name^="' + value + '"], textarea[name^="' + value + '"]', $form); | |
$form_elements.each(function() { | |
var elem_name = $(this).attr('name'); | |
var message; | |
switch (value) { | |
case "user_name": | |
message = "Заполните Ваше имя"; | |
break; | |
case "user_phone": | |
message = "Не указан телефон"; | |
break; | |
case "user_email": | |
message = "Укажите корректный email"; | |
break; | |
default: | |
message = "Заполните данное поле"; | |
break; | |
} | |
rules[elem_name] = { | |
required: true | |
}; | |
if (value === 'user_phone') { | |
$('input[name^="' + value + '"]').mask("+7 (999) 999 99 99"); | |
rules[elem_name].usPhoneFormat = true; | |
} | |
messages[elem_name] = { | |
required: message | |
} | |
}); | |
}) | |
// console.log(rules, messages); | |
$form.validate({ | |
rules: rules, | |
messages: messages, | |
submitHandler: function(form) { | |
submitRemodalForm(form, $form_id, $context); | |
} | |
}); | |
}); | |
function submitRemodalForm(form, task, $context) { | |
/** | |
* formPopupNumberId - is way to identify what type of form is present form - callback or baseOrder | |
*/ | |
var $form = $(form); | |
/* prepare serialized array for the addition of a form type identifier */ | |
var form_data = $(form).serializeArray(); | |
form_data.push({name: "task", value: task}); | |
form_data = $.param(form_data); | |
if (task === 'order-now') { | |
sendOrder(); | |
// return; | |
} else { | |
$.ajax({ | |
url: '/ajax.php', | |
type: 'POST', | |
data: form_data, | |
beforeSend: function(r) { | |
$form.hide(); | |
$('.remodal--action__title', $context).html('Отправка заявки...'); | |
$('.remodal__description', $context).html(''); | |
$('.submit-loader', $context).removeClass('submit-loader--paused'); | |
} | |
}).always(function(r) { | |
}).done(function(r) { | |
// alert('done'); | |
$form.trigger('reset'); | |
setTimeout(function() { | |
$('.remodal--action__title', $context).html('Благодарим!'); | |
$('.remodal__description', $context).html('Ваша заявка успешно отправлена!'); | |
$('.submit-loader', $context).addClass('submit-loader--paused'); | |
$('.success-icon', $context).fadeIn(800); | |
}, 1000); | |
}).fail(function(request, textStatus, errorThrown) { | |
// alert('fail'); | |
console.log(request.responseText); | |
console.log(textStatus); | |
console.log(errorThrown); | |
}); | |
} // end else | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment