var user_profile = (function ($, window, jQuery) { var _vars_ = {}; return { set_vars: function (options) { $.extend(_vars_, options || {}); }, get_vars : function(){ return _vars_; }, _modal : function(){ //config app.modal.focusout_solution("on"); var dialog = bootbox.dialog({ "title": "Mise à jour le profil de l'utilisateur", "className" : "user-profile-form my-modal-with-sm", "message": $("#userProfileForm"), "backdrop": 'static', "animate": true, "show": false, "buttons" : [ { "label": app.get_vars()._app.btn._close, "class": "btn btn-default", "callback": function () { } }, { "label": "Sauvegarder les modifications", "class": "btn btn-success w-mb", "callback": function () { user_profile._validation(); return false; } } ] }).on('shown.bs.modal', function(){ $("#first_name").focus(); app._form._populate_values({ dataHolder : $("#user_profile"), dataIndex : "user_data", fields : ["first_name", "last_name", "email_address", "username"] }); // submit form on keypress or enter app._form._form_submit_on_keypress("userProfileForm", function(){ user_profile._validation(); }); }).on('hide.bs.modal', function(e) { user_profile._hide_reset("#userProfileModal"); app.modal.focusout_solution("off"); $(this).data('bs.modal', null); $(this).off('hidden.bs.modal'); }) .modal('show') // show before init .init(function(){ // app._form._reset(this); }); }, _hide_reset : function(form){ app._form._resetFormValidation(form); $(this).data('bs.modal', null); $(form).off('hidden.bs.modal'); }, save : function(form){ $.ajax({ url : app.get_vars().baseurl+"update_my_profile", type : "POST", data : $(form).serializeArray(), dataType : "json", success : function(result){ //reset validation if(app.isalive(result)) { app._notify(result.mtype, result.message); if (result.mtype == "success") { user_profile._hide_reset("#userProfileModal"); $('.user-profile-form').modal('hide'); window.setTimeout(function () { window.location.href = app.get_vars().baseurl + "my_profile"; }, 2000); } else { var x = 0; $.map(result.mdetail, function (input, i) { if (input.field == "new_password") { app._form._populate_field_error("new_password", input.message, x); app._form._populate_field_error("confirm_new_password", input.message, x); } else if (input.field == "fullname") { app._form._populate_field_error("first_name", input.message, x); app._form._populate_field_error("last_name", input.message, x); } else{ app._form._populate_field_error(input.field, input.message, x); } x++; }); } } } }); }, _validation : function(){ var new_pwd = $("#new_password").val().trim(); var confirm_pass_length = $("#confirm_new_password").val().trim().length; var confirm_new_pwd = $("#confirm_new_password").val().trim(); var msg = (confirm_pass_length === 0 ? "Ce champ est obligatoire." : app.get_vars()._app.account.pwd_did_not_match ) var options = { boot_box : { _continue: function () { user_profile.save($("form#userProfileForm")); }, message: app.get_vars().my_profile.cma_msg.update_profile, id : "update_confirm_box" }, validation : { form_id : "form#userProfileForm", "custom" : { "rules": { "check_confirm_pwd": { "_fnc": function (value, element, options) { if( confirm_pass_length !== 0 ) { if(new_pwd === confirm_new_pwd ){ return true; } else { return false; } } else if(new_pwd != ""){ return false; } return true; }, msg: msg }, "check_new_pwd": { "_fnc": function (value, element, options) { if(value.trim() != "") { return true; } else{ return true; } }, msg: app.get_vars()._app.account.pwd_did_not_match }, } }, rules : { "first_name": { minlength:false, required: true }, "last_name" : { minlength : false, required : true }, "username" : { required : true }, "old_password" : { required : true }, "email_address" : { required : false, email : true }, "confirm_new_password": { check_confirm_pwd : true // minlength : 5 }, "new_password" : { check_new_pwd : true // minlength : 5 } } } }; app._form._validate(options); } };//end of return // Pass in jQuery. })(jQuery, this); $(function() { $("#new_password").on("keyup", function(){ if($(this).val().trim() === ""){ app._form._resetValidationByField("confirm_new_password"); } }); });