我在Ruby中创建了一个房地产计算器,用于研究生课程中的最终项目。该项目将在大约24小时内(星期一)到期,所以我非常感谢您的帮助。
该应用程序运行良好,但我需要使用JS添加客户端表单验证。我让JS工作就像我希望它以基本的html形式工作,但现在我需要将这个JS功能应用于我们的团队在Ruby on Rails环境中使用的表单。
我不确定如何准确地合并这两个表单以及放置JS的位置,以便init函数不会相互覆盖或导致错误。我会假设所有JS代码都在App / assets / javascript文件夹中,但是我对JS如何正确链接到我正在创建的每个表单感到有点困惑,因为我必须为三种表单执行此操作,因为我有三个实体:投资者实体,房地产实体和抵押实体。
我在本地为这三个实体中的每个实体进行了所有JS验证,并且我将一个用于抵押贷款。当我在本地创建和测试JS验证时,它们各自位于各自的文件夹中,但是在ruby git repo上,我认为所有JS代码都以相同的方式共享同一个JS文件夹......我不知道很多关于RoR中的JS,但我认为该assets / js文件夹中的所有JS都是活动的。
我很担心,因为如果我将所有JS验证代码都放在该文件夹中或将其合并到一个文件中,那么如果所有三个实体都在调用具有相同名称的函数,那么是否存在冲突(“ initPage“)?
所以...嗯..我想如果我只是将这个initPage()重命名为3个独立的函数,如下所示:
我粘贴了工作所需的所有JS代码,但实际上只有警告定义和一个函数initPage()对于每个实体都是不同的。我假设utils.js可以放在assets / js文件夹中,但是这三个实体的其他JS代码怎么样?
我(?):
THX !!
我需要添加JS验证的Ruby中的抵押表单:(对不起使用Bootstrap的所有标记)
<% provide(:title, 'Create mortgage') %>
<div class="container">
<div class="page-header">
<h1>Create mortgage</h1>
</div>
<div class="row">
<%= render 'layouts/sidebar' %>
<div class="col-md-9">
<%= form_for(@mortgage) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="col-md-12">
<div class="col-md-6">
<fieldset class="property_create_group">
<label for="mortgage_name">Mortgage name</label>
<%= f.text_field :name, :class => "form-control" %>
</fieldset>
</div>
<div class="col-md-2 clear-both1">
<fieldset class="property_create_group">
<label for="mortgage_amount">Amount</label>
<%= f.number_field :amount, :class => "form-control" %>
</fieldset>
</div>
<div class="col-md-2">
<fieldset class="property_create_group">
<label for="mortgage_terms">Term (yr)</label>
<%= f.number_field :terms, :class => "form-control" %>
</fieldset>
</div>
<div class="col-md-2">
<fieldset class="property_create_group">
<label for="mortgage_interestRate">Interest</label>
<%= f.number_field :interestRate, :class => "form-control" %>
</fieldset>
</div>
</div>
<div class="col-md-4 col-md-offset-1">
<%= f.submit "Create mortgage", class: "btn btn-large btn-success" %>
</div>
<% end %>
</div><!-- /col-md-9-->
</div><!-- /row-->
</div><!-- /container-->
Mortgage.html:
<head>
<link rel="stylesheet" type="text/css" href="css/forms.css" />
<script src="scripts/utils.js" type="text/javascript"></script>
<script src="scripts/mortgage.js" type="text/javascript"></script>
</head>
<body>
<div id="content">
<form action="#" method="post">
<fieldset><label for="mname">Mortgage name</label>
<input name="mname" id="mname" type="text" /></fieldset>
<fieldset><label for="amount">Amount</label>
<input name="amount" id="amount" type="text" /></fieldset>
<fieldset><label for="terms">Terms</label>
<input name="terms" id="terms" type="text" /></fieldset>
<fieldset><label for="interestRate">Interest Rate</label>
<input name="interestRate" id="interestRate" type="text" /></fieldset>
<input type="submit" id="createMortgage" value="Create Mortgage" disabled="disabled" />
</fieldset>
</form>
</div>
</body>
</html>
Mortgage.js:
window.onload = initPage;
var warnings = {
"mname" : {
"required": "Please enter a name for your mortgage.",
"err" : 0
},
"amount" : {
"required": "Please enter the amount of the mortgage.",
"numbers" : "Only numbers are allowed in the mortgage amount.",
"err" : 0
},
"terms" : {
"required": "Please enter the terms (in years) of the mortgage. ",
"numbers" : "Only numbers (in years) can be given for the mortgage terms.",
"err" : 0
},
"interestRate" : {
"required": "Please enter the amount of the interest rate.",
"numbers" : "Only numbers are allowed in the interest rate.",
"err" : 0
}
}
function initPage() {
addEventHandler(document.getElementById("mname"), "blur", fieldIsFilled);
addEventHandler(document.getElementById("amount"), "blur", fieldIsFilled);
addEventHandler(document.getElementById("amount"), "blur", fieldIsNumbers);
addEventHandler(document.getElementById("terms"), "blur", fieldIsFilled);
addEventHandler(document.getElementById("terms"), "blur", fieldIsNumbers);
addEventHandler(document.getElementById("interestRate"), "blur", fieldIsFilled);
addEventHandler(document.getElementById("interestRate"), "blur", fieldIsNumbers);
}
function fieldIsFilled(e) {
var me = getActivatedObject(e);
if (me.value == "") {
warn(me, "required");
} else {
unwarn(me, "required");
}
}
function fieldIsLetters(e) {
var me = getActivatedObject(e);
var nonAlphaChars = /[^a-zA-Z]/;
if (nonAlphaChars.test(me.value)) {
warn(me, "letters");
} else {
unwarn(me, "letters");
}
}
function fieldIsNumbers(e) {
var me = getActivatedObject(e);
var nonNumericChars = /[^0-9]/;
if (nonNumericChars.test(me.value)) {
warn(me, "numbers");
} else {
unwarn(me, "numbers");
}
}
function warn(field, warningType) {
var parentNode = field.parentNode;
var warning = eval('warnings.' + field.id + '.' + warningType);
if (parentNode.getElementsByTagName('p').length == 0) {
var p = document.createElement('p');
field.parentNode.appendChild(p);
var warningNode = document.createTextNode(warning);
p.appendChild(warningNode);
} else {
var p = parentNode.getElementsByTagName('p')[0];
p.childNodes[0].nodeValue = warning;
}
document.getElementById("createMortgage").disabled = true;
}
function unwarn(field, warningType) {
if (field.parentNode.getElementsByTagName("p").length > 0) {
var p = field.parentNode.getElementsByTagName("p")[0];
var currentWarning = p.childNodes[0].nodeValue;
var warning = eval('warnings.' + field.id + '.' + warningType);
if (currentWarning == warning) {
field.parentNode.removeChild(p);
}
}
var fieldsets =
document.getElementById("content").getElementsByTagName("fieldset");
for (var i=0; i<fieldsets.length; i++) {
var fieldWarnings = fieldsets[i].getElementsByTagName("p").length;
if (fieldWarnings > 0) {
document.getElementById("createMortgage").disabled = true;
return;
}
}
document.getElementById("createMortgage").disabled = false;
}
Utils.js:
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function getActivatedObject(e) {
var obj;
if (!e) {
// early version of IE
obj = window.event.srcElement;
} else if (e.srcElement) {
// IE 7 or later
obj = e.srcElement;
} else {
// DOM Level 2 browser
obj = e.target;
}
return obj;
}
function addEventHandler(obj, eventName, handler) {
if (document.attachEvent) {
obj.attachEvent("on" + eventName, handler);
} else if (document.addEventListener) {
obj.addEventListener(eventName, handler, false);
}
}
答案 0 :(得分:0)
您可以尝试在client_side_validation gem上使用lok。它不再被维护,但它应该让你的生活更轻松,而无需编写任何额外的js。 https://github.com/bcardarella/client_side_validations