Add error handling code for edit form

This commit is contained in:
Erik Michaels-Ober 2011-03-27 12:59:02 -07:00
parent f557460f1b
commit ca6a961097
1 changed files with 96 additions and 20 deletions

View File

@ -411,26 +411,102 @@ $(function() {
return false;
});
$('#edit_form').live('submit', function() {
$.post('/users.json', {
'id': $('#id').val(),
'hydrant_id': activeHydrantId,
'commit': $('#edit_form_submit').val(),
'utf8': '✓',
'authenticity_token': $('#edit_form input[name="authenticity_token"]').val(),
'_method': 'put',
'user': {
'email': $('#user_email').val(),
'name': $('#user_name').val(),
'organization': $('#user_organization').val(),
'voice_number': $('#user_voice_number').val(),
'sms_number': $('#user_sms_number').val(),
'password': $('#user_password').val(),
'password_confirmation': $('#user_password').val(),
'current_password': $('#user_current_password').val()
}
}, function(data) {
activeInfoWindow.setContent(data);
});
var errors = []
if(!/[\w\.%\+\]+@[\w\]+\.+[\w]{2,}/.test($('#user_email').val())) {
errors.push($('#user_email'));
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
} else {
$('#user_email_label').removeClass('error');
$('#user_email').removeClass('error');
}
if($('#user_name').val() === '') {
errors.push($('#user_name'));
$('#user_name_label').addClass('error', 500);
$('#user_name').addClass('error', 500);
} else {
$('#user_name_label').removeClass('error');
$('#user_name').removeClass('error');
}
if($('#user_password').val() && ($('#user_password').val().length < 6 || $('#user_password').val().length > 20)) {
errors.push($('#user_password'));
$('#user_password_label').addClass('error', 500);
$('#user_password').addClass('error', 500);
} else {
$('#user_password_label').removeClass('error');
$('#user_password').removeClass('error');
}
if($('#user_current_password').val().length < 6 || $('#user_current_password').val().length > 20) {
errors.push($('#user_current_password'));
$('#user_current_password_label').addClass('error', 500);
$('#user_current_password').addClass('error', 500);
} else {
$('#user_current_password_label').removeClass('error');
$('#user_current_password').removeClass('error');
}
if(errors.length > 0) {
errors[0].focus();
} else {
$.post('/users.json', {
'id': $('#id').val(),
'hydrant_id': activeHydrantId,
'commit': $('#edit_form_submit').val(),
'utf8': '✓',
'authenticity_token': $('#edit_form input[name="authenticity_token"]').val(),
'_method': 'put',
'user': {
'email': $('#user_email').val(),
'name': $('#user_name').val(),
'organization': $('#user_organization').val(),
'voice_number': $('#user_voice_number').val(),
'sms_number': $('#user_sms_number').val(),
'password': $('#user_password').val(),
'password_confirmation': $('#user_password').val(),
'current_password': $('#user_current_password').val()
}
}, function(data) {
if(data.errors) {
if(data.errors.email) {
errors.push($('#user_email'));
$('#user_email_label').addClass('error', 500);
$('#user_email').addClass('error', 500);
}
if(data.errors.name) {
errors.push($('#user_name'));
$('#user_name_label').addClass('error', 500);
$('#user_name').addClass('error', 500);
}
if(data.errors.organization) {
errors.push($('#user_organization'));
$('#user_organization_label').addClass('error', 500);
$('#user_organization').addClass('error', 500);
}
if(data.errors.voice_number) {
errors.push($('#user_voice_number'));
$('#user_voice_number_label').addClass('error', 500);
$('#user_voice_number').addClass('error', 500);
}
if(data.errors.sms_number) {
errors.push($('#user_sms_number'));
$('#user_sms_number_label').addClass('error', 500);
$('#user_sms_number').addClass('error', 500);
}
if(data.errors.password) {
errors.push($('#user_password'));
$('#user_password_label').addClass('error', 500);
$('#user_password').addClass('error', 500);
}
if(data.errors.current_password) {
errors.push($('#user_current_password'));
$('#user_current_password_label').addClass('error', 500);
$('#user_current_password').addClass('error', 500);
}
errors[0].focus();
} else {
activeInfoWindow.setContent(data);
}
});
}
return false;
});
});