added validations to page create. and also now can make pages without assigning modules .. frontend will be blank

This commit is contained in:
Harry Bomrah 2014-07-18 14:43:02 +08:00
parent 747f794a18
commit b1ed03b01c
7 changed files with 125 additions and 25 deletions

View File

@ -0,0 +1,81 @@
var _number_of_validators_ = 0;
var FormValidator = function(form){
var fv = this,
elements_data = {};
this.form = form;
this.elements = null;
this.validate_functions = {
required : function(value,element){
switch(element.attr("type")){
case "text":
return (value == "" ? false : true);
break;
case "checkbox":
return element.is(":checked");
break;
}
},
nospace : function(value){
return (/\s/.test(value) ? false : true);
},
email : function(){
}
}
this.initialize = function(){
fv.elements = form.find("*[data-fv-validation]");
_number_of_validators_++;
_putFieldsValidatorAndMessage();
_attachSubmitHandler();
}
var _attachSubmitHandler = function(){
fv.form.on("submit",function(){
$.each(elements_data,function(key,element){
var validators = element.validators,
messages = element.messages,
el = fv.form.find("#" + key);
for(i = 0; i < validators.length; i++){
var error_span = (fv.form.find("div[for=" + key + "]").length ? $("div[for=" + key + "]") : $("<div class='validator_error_class text-error' for='" + key + "'></div>"));
if(typeof fv.validate_functions[validators[i]] == "function"){
if(!fv.validate_functions[validators[i]](el.val(),el)){
error_span.text(messages[i]);
el.after(error_span);
break;
}else{
error_span.remove();
}
}else{
console.info("Not validating for " + validators[i] + ". Skipping.");
}
}
})
if(fv.form.find("div.validator_error_class").length){
return false;
}else{
return true;
}
})
}
var _putFieldsValidatorAndMessage = function(){
$.each(fv.elements,function(i,element){
var element = $(element),
validators = element.data("fv-validation").split(";").slice(0,-1),
messages = element.data("fv-messages").split(";").slice(0,-1),
id = (typeof element.attr("id") == "undefined" ? _generateElementId(element) : element.attr("id"));
elements_data[id] = {};
elements_data[id].validators = validators;
elements_data[id].messages = messages;
})
}
var _generateElementId = function(element){
var name = element.attr("name").replace(/\[+(.*?)\]+/g,"$1") + "_" + _number_of_validators_;
element.attr("id",name);
return name;
}
this.initialize();
}

View File

@ -278,14 +278,14 @@ class PagesController < ApplicationController
if original_view != "home"
viewarea = doc.css("*[data-content='true']")[0]
viewarea.inner_html = render_to_string(original_view)
viewarea.inner_html = render_to_string(original_view) rescue "<div></div>"
end
link = doc.css("link")[0]
link.attributes["href"].value = current_site.favicon.url.nil? ? "/assets/favicon.ico" : current_site.favicon.url
final_html_for_render = doc.to_html
else
final_html_for_render = render_to_string(original_view)
final_html_for_render = render_to_string(original_view) rescue "<div></div>"
end
final_html_for_render = change_to_language(final_html_for_render)
final_html_for_render

View File

@ -1,3 +1,4 @@
<%= javascript_include_tag 'validator' %>
<% node = Page.root %>
<% unless node.nil? %>
<ol id='<%= node.id %>' class="sortable item-groups root">

View File

@ -1,23 +1,25 @@
<% if params[:action] == "edit" %>
<h4><%= @page.name %></h4>
<% end %>
<div class="control-group">
<% if params[:action] == "new" %>
<label class="control-label">Page id :</label>
<div class="controls">
<%= f.text_field :page_id, data: {"fv-validation" => "required;nospace;pageid_validation;", "fv-messages" => "Cannot be empty.;Cannot have blank spaces.;Page id already taken.;"} %>
</div>
<% end %>
</div>
<div class="control-group">
<% I18n.available_locales.each do |locale| %>
<label class="control-label">Page name (<%= t(locale.to_s) %>) :</label>
<div class="controls">
<%= f.fields_for :name_translations do |n| %>
<%= n.text_field locale, :value=>@page.name_translations[locale] rescue nil %>
<%= n.text_field locale, data: {"fv-validation" => "required;", "fv-messages" => "Cannot be empty.;"}, :value=>@page.name_translations[locale] rescue nil %>
<% end %>
</div>
<% end %>
</div>
<div class="control-group">
<% if params[:action] == "new" %>
<label class="control-label">Page id :</label>
<div class="controls">
<%= f.text_field :page_id %>
</div>
<% end %>
<label class="control-label"><%= t(:module) %> :</label>
<div class="controls">
<%= f.select(:module, @modules.map{|m| [t(OrbitApp::Module::Registration.find_by_key(m.key).module_label),m.key]},{:include_blank => true},:class=>"module_select") %>

View File

@ -1,12 +1,26 @@
<% if params[:action] == "edit" %>
<h4><%= @page.name %></h4>
<% end %>
<div class="control-group">
<% if params[:action] == "new" %>
<label class="control-label">Page id :</label>
<div class="controls">
<%= f.text_field :page_id, data: {"fv-validation" => "required;nospace;pageid_validation;", "fv-messages" => "Cannot be empty.;Cannot have blank spaces;Page id already taken.;"} %>
</div>
<% end %>
<div class="controls">
<%= f.hidden_field :parent_page, value: (params[:parent_page] || @page.parent_page_id) %>
<%= f.hidden_field :page_type, value: "link" %>
</div>
</div>
<div class="control-group">
<% I18n.available_locales.each do |locale| %>
<label class="control-label">Page name (<%= t(locale.to_s) %>) :</label>
<div class="controls">
<%= f.fields_for :name_translations do |n| %>
<%= n.text_field locale, :value=>@page.name_translations[locale] rescue nil %>
<%= n.text_field locale, data: {"fv-validation" => "required;", "fv-messages" => "Cannot be empty.;"}, :value=>@page.name_translations[locale] rescue nil %>
<% end %>
</div>
<% end %>
@ -33,18 +47,6 @@
</div>
<% end %>
</div>
<div class="control-group">
<% if params[:action] == "new" %>
<label class="control-label">Page id :</label>
<div class="controls">
<%= f.text_field :page_id %>
</div>
<% end %>
<div class="controls">
<%= f.hidden_field :parent_page, value: (params[:parent_page] || @page.parent_page_id) %>
<%= f.hidden_field :page_type, value: "link" %>
</div>
</div>
<div class="control-group" id="enable-menu">
<div class="controls">
<label class="control-label">Activation :</label>

View File

@ -14,3 +14,8 @@
</div>
</div>
<% end %>
<script type="text/javascript">
$(document).ready(function(){
var fromvalidator = new FormValidator($("form.edit_page"));
})
</script>

View File

@ -1,4 +1,4 @@
<%= form_for @page, url: {action: "create"},:class =>"form-horizontal", remote: true do |f| %>
<%= form_for @page, url: {action: "create"},:class =>"form-horizontal", :remote => true do |f| %>
<% if params[:type] == "page" %>
<%= render "form", {:f=> f} %>
<% elsif params[:type] == "link" %>
@ -14,3 +14,12 @@
</div>
</div>
<% end %>
<script type="text/javascript">
$(document).ready(function(){
var pageids = <%= Page.all.collect{|page| page.page_id}.to_json.html_safe %>
var fromvalidator = new FormValidator($("#new_page"));
fromvalidator.validate_functions.pageid_validation = function(value){
return (pageids.indexOf(value) == -1 ? true : false);
}
})
</script>