Commit ddfbf3f340c2e0a0d6c4a7d1bcb053444dd901f3
Committed by
Luciano Prestes
1 parent
8f54d03e
Exists in
master
and in
5 other branches
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>
Showing
3 changed files
with
43 additions
and
13 deletions
Show diff stats
lib/database_helper.rb
| ... | ... | @@ -5,7 +5,7 @@ module DatabaseHelper |
| 5 | 5 | list_databases = [] |
| 6 | 6 | |
| 7 | 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 | 9 | database = SoftwareDatabase.new |
| 10 | 10 | database.database_description_id = new_database[:database_description_id] |
| 11 | 11 | database.version = new_database[:version] |
| ... | ... | @@ -48,12 +48,21 @@ module DatabaseHelper |
| 48 | 48 | end |
| 49 | 49 | |
| 50 | 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 | 57 | Proc::new do |
| 52 | 58 | content_tag('table', |
| 53 | 59 | content_tag('tr', |
| 54 | 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 | 68 | content_tag('tr', | ... | ... |
public/mpog-software-validations.js
| 1 | 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 | 24 | function database_autocomplete() { |
| 3 | 25 | jQuery(".database_autocomplete").autocomplete({ |
| 4 | 26 | source : function(request, response){ |
| ... | ... | @@ -8,12 +30,6 @@ |
| 8 | 30 | data: {query: request.term}, |
| 9 | 31 | success: function(result){ |
| 10 | 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 | 34 | error: function(ajax, stat, errorThrown) { |
| 19 | 35 | console.log('Link not found : ' + errorThrown); |
| ... | ... | @@ -24,11 +40,12 @@ |
| 24 | 40 | minLength: 1, |
| 25 | 41 | |
| 26 | 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 | ... | ... |