diff --git a/lib/ext/person.rb b/lib/ext/person.rb index baef80b..8a3e7b6 100644 --- a/lib/ext/person.rb +++ b/lib/ext/person.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require_dependency 'person' class Person @@ -58,17 +60,19 @@ class Person end def validate_full_name - reg_firsts_char = /(^|\s)([a-z]|[0-9])/ - reg_special_char = /[^\w\*\s]/ + full_validation = /([^\w\*\s*])|(^|\s)([a-z]|[0-9])/ + partial_validation = /[^\w\*\s]/ invalid = false return false if self.name.blank? - self.name.split(" ").each do |value| + validation_name = replace_some_special_chars(self.name) + + validation_name.split(" ").each do |value| invalid = if value.length > 3 - reg_firsts_char.match(value) || reg_special_char.match(value) + full_validation.match(value) else - reg_special_char.match(value) + partial_validation.match(value) end end @@ -82,4 +86,24 @@ class Person def software? false end + + private + + def replace_some_special_chars text + text.gsub(/([áàâãéèêíïóôõöú])/) do |value| + if( ["á","à","â","ã"].include?(value) ) + "a" + elsif( ["é","è","ê"].include?(value) ) + "e" + elsif( ["í","ï"].include?(value) ) + "i" + elsif ( ["ó","ô","õ","ö"].include?(value) ) + "o" + elsif( ["ú"].indexOf(value) ) + "u" + else + value + end + end + end end diff --git a/public/mpog-user-validations.js b/public/mpog-user-validations.js index b0ed485..0c596a3 100644 --- a/public/mpog-user-validations.js +++ b/public/mpog-user-validations.js @@ -148,17 +148,37 @@ } function set_full_name_validation() { + // Sorry, I know its ugly. But I cant get ([^\w\*\s*])|(^|\s)([a-z]|[0-9]) + // to ignore Brazilian not so much special chars in names + function replace_some_special_chars(text) { + return text.replace(/([áàâãéèêíïóôõöú])/g, function(value){ + if( ["á","à","â","ã"].indexOf(value) != -1 ) + return "a"; + else if( ["é","è","ê"].indexOf(value) != -1 ) + return "e"; + else if( ["í","ï"].indexOf(value) != -1 ) + return "i"; + else if ( ["ó","ô","õ","ö"].indexOf(value) != -1 ) + return "o"; + else if( ["ú"].indexOf(value) != -1 ) + return "u"; + else + return value; + }); + } + function is_invalid_formated(text) { - var reg_firsts_char = /(^|\s)([a-z]|[0-9])/g; - var reg_special_char = /[^\w\*\s*]/g; - var invalid = false; + var full_validation = /([^\w\*\s*])|(^|\s)([a-z]|[0-9])/; // no special chars and do not initialize with no capital latter + var partial_validation = /[^\w\*\s*]/; // no special chars + text = replace_some_special_chars(text); var slices = text.split(" "); + var invalid = false; for(var i = 0; i < slices.length; i++) { if( slices[i].length > 3 ) { - invalid = reg_firsts_char.test(slices[i]) || reg_special_char.test(slices[i]); + invalid = full_validation.test(slices[i]); } else { - invalid = reg_special_char.test(slices[i]); + invalid = partial_validation.test(slices[i]); } if(invalid) break; diff --git a/test/unit/mpog_person_test.rb b/test/unit/mpog_person_test.rb index 04892d1..1a619bf 100644 --- a/test/unit/mpog_person_test.rb +++ b/test/unit/mpog_person_test.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require File.dirname(__FILE__) + '/../../../../test/test_helper' class MpogSoftwarePluginPersonTest < ActiveSupport::TestCase @@ -8,6 +10,13 @@ class MpogSoftwarePluginPersonTest < ActiveSupport::TestCase assert_equal true, p.save end + should 'save person with a valid full name with accents' do + p = Person::new :name=>'Jônatàs dâ Sîlvã Jösé', :identifier=>"jonatas-jose-da-silva" + p.user = fast_create(:user) + + assert_equal true, p.save + end + should 'not save person whose name has not capital letter' do p = Person::new :name=>"simple name" assert !p.save, _("Name Should begin with a capital letter and no special characters") -- libgit2 0.21.2