Add email validation regex setting!

This commit is contained in:
BoHung Chiu 2022-04-12 22:17:34 +08:00
parent ee94cdec5a
commit e7aafff59b
9 changed files with 94 additions and 6 deletions

View File

@ -426,6 +426,7 @@ class Admin::AsksController < OrbitAdminController
param_clone = param.clone
param_clone['default_setting'].each { |k, v| param_clone['default_setting'][k] = (v == 'true'? true : false) if param_clone['default_setting'][k].class==String}
param_clone.delete('custom_fields')
param_clone.delete('email_regex')
custom_fields = ask_setting['custom_fields'].clone
params_custom_fields = (params.require(collection_name).require('custom_fields') rescue {})
params_custom_fields.each do |k,v|

View File

@ -39,10 +39,12 @@ class AsksController < ApplicationController
csrf_value = OrbitHelper.request.session[:_csrf_token] || SecureRandom.base64(32)
token_tag = hidden_field_tag('authenticity_token',csrf_value)
switch_form = cal_switch_form(ask_settings,categories,token_tag,referer_url)
email_regex = @default_ask_setting.email_regex_enable ? @default_ask_setting.email_regex.gsub("\\A","^").gsub("\\z","$") : nil
{
'fields' => defalt_fields,
'extras'=>{
'switch_form' => switch_form
'switch_form' => switch_form,
'email_regex' => email_regex
}
}
else
@ -141,6 +143,7 @@ class AsksController < ApplicationController
switch_form = cal_switch_form(ask_settings,categories,token_tag,referer_url)
tags = module_app.tags
ask_question = AskQuestion.new(id: nil)
email_regex = @default_ask_setting.email_regex_enable ? @default_ask_setting.email_regex.gsub("\\A","^").gsub("\\z","$") : nil
{
'layout_type' => 'index',
'ask_question' => ask_question,
@ -149,7 +152,8 @@ class AsksController < ApplicationController
'module_app' => module_app,
'switch_form' => switch_form,
'ask_setting' => ask_setting,
'default_ask_setting' => @default_ask_setting
'default_ask_setting' => @default_ask_setting,
'email_regex' => email_regex
}
elsif @layout_type == 'published_index'
page_number = OrbitHelper.page_number.to_i
@ -178,10 +182,15 @@ class AsksController < ApplicationController
temp_params = create_params
all_to_save = []
ask_setting = AskCategorySetting.where(category_id: params['ask_question']['category_id']).first
email_regex = nil
if ask_setting.nil?
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
email_regex = ask_setting.email_regex
else
email_regex = AskSetting.first.email_regex rescue nil
end
email_regex = nil if !(ask_setting.email_regex_enable) || !(ask_setting.default_setting['mail'])
ask_setting.custom_fields.select{|k,v| v['type']=='file' || v['type']=='image'}.each do |k,v|
file = temp_params['custom_values'][k]
if !file.blank?
@ -199,7 +208,7 @@ class AsksController < ApplicationController
temp_params['custom_values'][to_save[0]] = [temp_params['custom_values'][to_save[0]].original_filename ,to_save[1].file.url]
end
end
if flag
if (email_regex ? ::Regexp.new(email_regex).match?(temp_params["mail"].to_s) : true) && flag
@ask_question.update_attributes(temp_params)
build_email(@ask_question)
redirect_to "#{params[:referer_url]}?method=thank"
@ -346,7 +355,7 @@ class AsksController < ApplicationController
</div>
</div>"
end.join
"<form id=\"#{form_id}\" action=\"#{form_url}\" enctype=\"multipart/form-data\" method=\"post\" class=\"form-horizontal\" accept-charset=\"UTF-8\" style=\"#{is_hidden ? 'display: none;' : ''}\">
"<form id=\"#{form_id}\" action=\"#{form_url}\" enctype=\"multipart/form-data\" method=\"post\" class=\"form-horizontal ask-question-form\" accept-charset=\"UTF-8\" style=\"#{is_hidden ? 'display: none;' : ''}\">
#{token_tag}
<div data-level=\"0\" data-list=\"fields\">
#{tmp}

View File

@ -35,6 +35,8 @@ class AskSetting
def field_name_translations(k)
I18n.available_locales.map{|v| [v.to_s,self.field_name(k,v.to_s)]}.to_h
end
field :email_regex_enable, type: Boolean, default: false
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'
field :sort_number, type: Hash, default: {}
field :default_setting, type: Hash,default: {ask_category_id: true,name: true,sex: false,mail: true,phone: false,appointment: false,recaptcha: false,agree_show: true,agree_usage: false}
field :default_setting_field_name, type: Hash,default: {}
@ -49,7 +51,7 @@ class AskSetting
attrs.delete(k)
end
end
attrs.except("_type","_id","uid","updated_at", "created_at")
attrs.except("_type","_id","uid","updated_at", "created_at", "email_regex")
end
def self.get_disp_fields_infos(cat=nil)
ask_setting = nil

View File

@ -98,6 +98,27 @@
set_input_name("#{form_type}")
%>
<fieldset>
<% if @has_email_regex %>
<div class="control-group">
<div class="control-label">
<label for="ask_email_regex"><%= t("ask.email_regex") %>:</label>
</div>
<div class="controls">
<%= f.check_box :email_regex_enable, {:id=>"email_regex_enable"} %><%= t("ask.enable") %>
<div style="clear: both;"></div>
<%= f.text_field :email_regex, {:id=>"ask_email_regex", :style => "width: 30em;"}.merge(f.object.email_regex_enable ? {} : {:disabled=>"disabled"}) %>
</div>
</div>
<script>
$("#email_regex_enable").click(function(){
if($(this).prop("checked")){
$("#ask_email_regex").removeAttr("disabled");
}else{
$("#ask_email_regex").attr("disabled","disabled");
}
})
</script>
<% end %>
<table>
<thead>
<tr>

View File

@ -25,5 +25,5 @@
})
</script>
<%= form_for @ask_setting,method: 'post',url: @url,html: { class: 'form-horizontal main-forms previewable' } do |f| %>
<%= render partial: 'ask_setting',locals:{ask_setting: @ask_setting,f: f,submit_text: t('submit')} %>
<%= render partial: 'ask_setting',locals:{ask_setting: @ask_setting,f: f,submit_text: t('submit'), :@has_email_regex => true} %>
<% end %>

View File

@ -7,6 +7,7 @@
ask_setting = data["ask_setting"]
set_input_name('ask_question')
@default_ask_setting = data['default_ask_setting']
email_regex = data['email_regex']
%>
<% if data['layout_type'] != 'index' %>
<%= render template: "asks/#{data['layout_type']}",:locals => { :data => data, :ask_setting => ask_setting } %>
@ -143,5 +144,29 @@ $(function(){
// }
})
$(document).ready(function(){
var email_regex = "<%=email_regex%>";
if(email_regex.length != 0){
email_regex = new RegExp(email_regex);
var submit_flag = false;
$(".ask-question-form").each(function(i, form){
var $form = $(form);
if($form.length != 0 && $form.find('#ask_question_mail').length != 0){
$form.submit(function(){
if(!submit_flag){
submit_flag = true;
if(email_regex.test($form.find('#ask_question_mail').val())){
submit_flag = false;
}else{
alert("<%=t('ask.please_check_email_format')%>");
submit_flag = false;
return false;
}
}
})
}
})
}
})
</script>
<% end %>

View File

@ -2,6 +2,9 @@ en:
situation: Situation
sex: Sex
ask:
please_check_email_format: "Please check email format!"
enable: Enable
email_regex: Email validation regex
please_save: Please click "Save Order" button when you done.
save_order: Save Order
order_fields: Order Fields

View File

@ -6,6 +6,9 @@ zh_tw:
errors:
verification_failed: 驗證碼錯誤
ask:
please_check_email_format: "請檢察email是否正確!"
enable: 啟用
email_regex: Email驗證regex
please_save: 調整完後, 請點選"儲存順序"
save_order: 儲存順序
order_fields: 排序欄位

View File

@ -74,5 +74,29 @@
}
}
})
$(document).ready(function(){
var email_regex = "{{email_regex}}";
if(email_regex.length != 0){
email_regex = new RegExp(email_regex);
var submit_flag = false;
$(".ask-question-form").each(function(i, form){
var $form = $(form);
if($form.length != 0 && $form.find('#ask_question_mail').length != 0){
$form.submit(function(){
if(!submit_flag){
submit_flag = true;
if(email_regex.test($form.find('#ask_question_mail').val())){
submit_flag = false;
}else{
alert("<%=t('ask.please_check_email_format')%>");
submit_flag = false;
return false;
}
}
})
}
})
}
})
</script>
</div>