fix tags validation on create

This commit is contained in:
Kyle Drake 2014-11-14 17:03:40 -08:00
parent 134790dcbc
commit 66fdbab21e
3 changed files with 7 additions and 7 deletions

2
app.rb
View file

@ -495,7 +495,7 @@ post '/create_validate' do
if !params[:field].match /^username$|^password$|^email$|^new_tags_string$/ if !params[:field].match /^username$|^password$|^email$|^new_tags_string$/
return {error: 'not a valid field'}.to_json return {error: 'not a valid field'}.to_json
end end
site = Site.new(params[:field] => params[:value]) site = Site.new(params[:field] => params[:value])
site.valid? site.valid?

View file

@ -762,28 +762,28 @@ class Site < Sequel::Model
@new_filtered_tags = [] @new_filtered_tags = []
if ((new? ? 0 : tags_dataset.count) + new_tags.length > 5) if ((new? ? 0 : tags_dataset.count) + new_tags.length > 5)
errors.add :tags, 'Cannot have more than 5 tags for your site.' errors.add :new_tags_string, 'Cannot have more than 5 tags for your site.'
end end
new_tags.each do |tag| new_tags.each do |tag|
tag.strip! tag.strip!
if tag.match(/[^a-zA-Z0-9 ]/) if tag.match(/[^a-zA-Z0-9 ]/)
errors.add :tags, "Tag \"#{tag}\" can only contain letters (A-Z) and numbers (0-9)." errors.add :new_tags_string, "Tag \"#{tag}\" can only contain letters (A-Z) and numbers (0-9)."
break break
end end
if tag.length > Tag::NAME_LENGTH_MAX if tag.length > Tag::NAME_LENGTH_MAX
errors.add :tags, "Tag \"#{tag}\" cannot be longer than #{Tag::NAME_LENGTH_MAX} characters." errors.add :new_tags_string, "Tag \"#{tag}\" cannot be longer than #{Tag::NAME_LENGTH_MAX} characters."
break break
end end
if tag.match(/ /) if tag.match(/ /)
errors.add :tags, "Tag \"#{tag}\" cannot have spaces." errors.add :new_tags_string, "Tag \"#{tag}\" cannot have spaces."
break break
end end
if tag.split(' ').length > Tag::NAME_WORDS_MAX if tag.split(' ').length > Tag::NAME_WORDS_MAX
errors.add :tags, "Tag \"#{tag}\" cannot be more than #{Tag::NAME_WORDS_MAX} word." errors.add :new_tags_string, "Tag \"#{tag}\" cannot be more than #{Tag::NAME_WORDS_MAX} word."
break break
end end

View file

@ -380,7 +380,6 @@
$('#createSiteForm').on('submit', function(obj) { $('#createSiteForm').on('submit', function(obj) {
$("input[name=blackbox_answer]").val("<%= @blackbox_question %>") $("input[name=blackbox_answer]").val("<%= @blackbox_question %>")
$.post('/create_validate_all', $(obj.target).serialize(), function(errors) { $.post('/create_validate_all', $(obj.target).serialize(), function(errors) {
console.log(errors)
if(errors.length == 0) { if(errors.length == 0) {
$.post('/create', $('#createSiteForm').serialize(), function(res) { $.post('/create', $('#createSiteForm').serialize(), function(res) {
if(res.result == 'ok') if(res.result == 'ok')
@ -412,6 +411,7 @@
if(res.result == 'ok') { if(res.result == 'ok') {
return $(obj.target).tooltip('hide') return $(obj.target).tooltip('hide')
} }
$(obj.target).attr('data-original-title', res.error) $(obj.target).attr('data-original-title', res.error)
$(obj.target).tooltip('show') $(obj.target).tooltip('show')
}) })