Commit ddfbf3f340c2e0a0d6c4a7d1bcb053444dd901f3

Authored by Fabio Teixeira
Committed by Luciano Prestes
1 parent 8f54d03e

fixes_software: Add validation to dynamic table autocomplete field

Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
lib/database_helper.rb
@@ -5,7 +5,7 @@ module DatabaseHelper @@ -5,7 +5,7 @@ module DatabaseHelper
5 list_databases = [] 5 list_databases = []
6 6
7 new_databases.each do |new_database| 7 new_databases.each do |new_database|
8 - unless SoftwareHelper.all_table_is_empty? new_database, ["database_description_id"] 8 + unless SoftwareHelper.all_table_is_empty? new_database
9 database = SoftwareDatabase.new 9 database = SoftwareDatabase.new
10 database.database_description_id = new_database[:database_description_id] 10 database.database_description_id = new_database[:database_description_id]
11 database.version = new_database[:version] 11 database.version = new_database[:version]
@@ -48,12 +48,21 @@ module DatabaseHelper @@ -48,12 +48,21 @@ module DatabaseHelper
48 end 48 end
49 49
50 def self.database_html_structure(database_data) 50 def self.database_html_structure(database_data)
  51 + database_name = if database_data[:database_description_id].blank?
  52 + ""
  53 + else
  54 + DatabaseDescription.find(database_data[:database_description_id]).name
  55 + end
  56 +
51 Proc::new do 57 Proc::new do
52 content_tag('table', 58 content_tag('table',
53 content_tag('tr', 59 content_tag('tr',
54 content_tag('td', label_tag(_("database Name: ")))+ 60 content_tag('td', label_tag(_("database Name: ")))+
55 - content_tag('td', text_field_tag("database_autocomplete", "", :class=>"database_autocomplete"))+  
56 - content_tag('td', hidden_field_tag("database[][database_description_id]", database_data[:database_description_id], :class=>"database_description_id")) 61 + content_tag('td',
  62 + text_field_tag("database_autocomplete", database_name, :class=>"database_autocomplete") +
  63 + content_tag('div', _("Pick an item on the list"), :class=>"autocomplete_validation_message hide-field")
  64 + )+
  65 + content_tag('td', hidden_field_tag("database[][database_description_id]", database_data[:database_description_id], :class=>"database_description_id", data:{label:database_name}))
57 )+ 66 )+
58 67
59 content_tag('tr', 68 content_tag('tr',
public/mpog-software-validations.js
1 (function(){ 1 (function(){
  2 + function get_hidden_description_field(autocomplete_field, klass) {
  3 + var field = jQuery(autocomplete_field);
  4 + field = field.parent().parent().find(klass);
  5 + return field;
  6 + }
  7 +
  8 + function verify_autocomplete(field, klass) {
  9 + var field = jQuery(field);
  10 + var selected = get_hidden_description_field(field, klass);
  11 + var message_error = jQuery(field).parent().find(".autocomplete_validation_message");
  12 +
  13 + if( field.length === 0 || selected.val().length === 0 ) {
  14 + message_error.removeClass("hide-field");
  15 + selected_value.val("");
  16 +
  17 + message_error.show();
  18 + } else {
  19 + field.val(selected.attr("data-label"));
  20 + message_error.hide();
  21 + }
  22 + }
  23 +
2 function database_autocomplete() { 24 function database_autocomplete() {
3 jQuery(".database_autocomplete").autocomplete({ 25 jQuery(".database_autocomplete").autocomplete({
4 source : function(request, response){ 26 source : function(request, response){
@@ -8,12 +30,6 @@ @@ -8,12 +30,6 @@
8 data: {query: request.term}, 30 data: {query: request.term},
9 success: function(result){ 31 success: function(result){
10 response(result); 32 response(result);
11 -  
12 - /*if( result.length == 0 ) {  
13 - jQuery('#institution_empty_ajax_message').switchClass("hide-field", "show-field");  
14 - } else {  
15 - jQuery('#institution_empty_ajax_message').switchClass("show-field", "hide-field");  
16 - }*/  
17 }, 33 },
18 error: function(ajax, stat, errorThrown) { 34 error: function(ajax, stat, errorThrown) {
19 console.log('Link not found : ' + errorThrown); 35 console.log('Link not found : ' + errorThrown);
@@ -24,11 +40,12 @@ @@ -24,11 +40,12 @@
24 minLength: 1, 40 minLength: 1,
25 41
26 select : function (event, selected) { 42 select : function (event, selected) {
27 - jQuery(this)  
28 - .parent().parent()  
29 - .find(".database_description_id")  
30 - .val(selected.item.id); 43 + var description = get_hidden_description_field(this, ".database_description_id");
  44 + description.val(selected.item.id);
  45 + description.attr("data-label", selected.item.label);
31 } 46 }
  47 + }).blur(function(){
  48 + verify_autocomplete(this, ".database_description_id");
32 }); 49 });
33 } 50 }
34 51
public/style.css
@@ -156,3 +156,7 @@ @@ -156,3 +156,7 @@
156 color: red; 156 color: red;
157 content: ' (*)' 157 content: ' (*)'
158 } 158 }
  159 +
  160 +.autocomplete_validation_message {
  161 + color: red;
  162 +}
159 \ No newline at end of file 163 \ No newline at end of file