diff --git a/lib/database_helper.rb b/lib/database_helper.rb index 933d1f1..28d42e2 100644 --- a/lib/database_helper.rb +++ b/lib/database_helper.rb @@ -5,7 +5,7 @@ module DatabaseHelper list_databases = [] new_databases.each do |new_database| - unless SoftwareHelper.all_table_is_empty? new_database, ["database_description_id"] + unless SoftwareHelper.all_table_is_empty? new_database database = SoftwareDatabase.new database.database_description_id = new_database[:database_description_id] database.version = new_database[:version] @@ -48,12 +48,21 @@ module DatabaseHelper end def self.database_html_structure(database_data) + database_name = if database_data[:database_description_id].blank? + "" + else + DatabaseDescription.find(database_data[:database_description_id]).name + end + Proc::new do content_tag('table', content_tag('tr', content_tag('td', label_tag(_("database Name: ")))+ - content_tag('td', text_field_tag("database_autocomplete", "", :class=>"database_autocomplete"))+ - content_tag('td', hidden_field_tag("database[][database_description_id]", database_data[:database_description_id], :class=>"database_description_id")) + content_tag('td', + text_field_tag("database_autocomplete", database_name, :class=>"database_autocomplete") + + content_tag('div', _("Pick an item on the list"), :class=>"autocomplete_validation_message hide-field") + )+ + content_tag('td', hidden_field_tag("database[][database_description_id]", database_data[:database_description_id], :class=>"database_description_id", data:{label:database_name})) )+ content_tag('tr', diff --git a/public/mpog-software-validations.js b/public/mpog-software-validations.js index 51f810d..4d50d17 100644 --- a/public/mpog-software-validations.js +++ b/public/mpog-software-validations.js @@ -1,4 +1,26 @@ (function(){ + function get_hidden_description_field(autocomplete_field, klass) { + var field = jQuery(autocomplete_field); + field = field.parent().parent().find(klass); + return field; + } + + function verify_autocomplete(field, klass) { + var field = jQuery(field); + var selected = get_hidden_description_field(field, klass); + var message_error = jQuery(field).parent().find(".autocomplete_validation_message"); + + if( field.length === 0 || selected.val().length === 0 ) { + message_error.removeClass("hide-field"); + selected_value.val(""); + + message_error.show(); + } else { + field.val(selected.attr("data-label")); + message_error.hide(); + } + } + function database_autocomplete() { jQuery(".database_autocomplete").autocomplete({ source : function(request, response){ @@ -8,12 +30,6 @@ data: {query: request.term}, success: function(result){ response(result); - - /*if( result.length == 0 ) { - jQuery('#institution_empty_ajax_message').switchClass("hide-field", "show-field"); - } else { - jQuery('#institution_empty_ajax_message').switchClass("show-field", "hide-field"); - }*/ }, error: function(ajax, stat, errorThrown) { console.log('Link not found : ' + errorThrown); @@ -24,11 +40,12 @@ minLength: 1, select : function (event, selected) { - jQuery(this) - .parent().parent() - .find(".database_description_id") - .val(selected.item.id); + var description = get_hidden_description_field(this, ".database_description_id"); + description.val(selected.item.id); + description.attr("data-label", selected.item.label); } + }).blur(function(){ + verify_autocomplete(this, ".database_description_id"); }); } diff --git a/public/style.css b/public/style.css index e1b698a..038a277 100644 --- a/public/style.css +++ b/public/style.css @@ -156,3 +156,7 @@ color: red; content: ' (*)' } + +.autocomplete_validation_message { + color: red; +} \ No newline at end of file -- libgit2 0.21.2