Commit ddfbf3f340c2e0a0d6c4a7d1bcb053444dd901f3

Authored by Fabio Teixeira
Committed by Luciano Prestes
1 parent 8f54d03e
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

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 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  
... ...
public/style.css
... ... @@ -156,3 +156,7 @@
156 156 color: red;
157 157 content: ' (*)'
158 158 }
  159 +
  160 +.autocomplete_validation_message {
  161 + color: red;
  162 +}
159 163 \ No newline at end of file
... ...