ref: refs/heads/master
commit d2fffbdfb821ea3c97b94028a9c6e0a7543f0e81
Author: Duncan Mac-Vicar P
Date: Wed Jul 15 13:28:19 2009 +0200
start of client side (with ajax support and remoting) form validation
---
webclient/app/controllers/hosts_controller.rb | 15 +-
webclient/app/controllers/sessions_controller.rb | 2 +-
webclient/app/models/host.rb | 3 +
webclient/app/views/hosts/edit.html.erb | 15 +-
webclient/app/views/hosts/new.html.erb | 21 +-
webclient/app/views/layouts/main.rhtml | 1 +
webclient/config/routes.rb | 4 +
webclient/public/javascripts/jquery.validate.js | 1131 ++++++++++++++++++++++
8 files changed, 1188 insertions(+), 4 deletions(-)
diff --git a/webclient/app/controllers/hosts_controller.rb b/webclient/app/controllers/hosts_controller.rb
index 7e9be7a..75dfa2c 100644
--- a/webclient/app/controllers/hosts_controller.rb
+++ b/webclient/app/controllers/hosts_controller.rb
@@ -1,3 +1,5 @@
+require 'uri'
+
class HostsController < ApplicationController
layout 'main'
@@ -5,7 +7,8 @@ class HostsController < ApplicationController
def index
begin
@hosts = Host.find(:all)
- rescue
+ rescue Exception => e
+ logger.error e.to_s
redirect_to "/migrate"
end
end
@@ -58,4 +61,14 @@ class HostsController < ApplicationController
end
redirect_to :action => 'index'
end
+
+ def validate_uri
+ logger.error params[:host][:url]
+ uri = URI.parse(params[:host][:url]) rescue nil
+ unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
+ render :text => 'false'
+ return
+ end
+ render :text => 'true'
+ end
end
diff --git a/webclient/app/controllers/sessions_controller.rb b/webclient/app/controllers/sessions_controller.rb
index 4514189..9dc5cd4 100644
--- a/webclient/app/controllers/sessions_controller.rb
+++ b/webclient/app/controllers/sessions_controller.rb
@@ -63,7 +63,7 @@ class SessionsController < ApplicationController
begin
self.current_account, auth_token = Account.authenticate(params[:login],
params[:password],
- host.uri)
+ host.url)
# error handling when login to the service is pretty
# important to get meaningful error messages to the user
rescue Errno::ECONNREFUSED => e
diff --git a/webclient/app/models/host.rb b/webclient/app/models/host.rb
index 660db64..d9e96eb 100644
--- a/webclient/app/models/host.rb
+++ b/webclient/app/models/host.rb
@@ -1,2 +1,5 @@
+require 'uri'
+
class Host < ActiveRecord::Base
+ #validates_format_of :url, :with => URI.regexp
end
diff --git a/webclient/app/views/hosts/edit.html.erb b/webclient/app/views/hosts/edit.html.erb
index e9a1176..9015139 100644
--- a/webclient/app/views/hosts/edit.html.erb
+++ b/webclient/app/views/hosts/edit.html.erb
@@ -1,5 +1,18 @@
<h1><%=_("Editing YaST-Web-Service")%></h1>
+<script type="text/javascript">
+$(document).ready(function() {
+ $("#edit_host").validate({
+ rules: {
+ uri: {
+ required: true,
+ url: true
+ }
+ }
+ });
+});
+</script>
+
<% form_for(@host) do |f| %>
<%= f.error_messages %>
@@ -16,7 +29,7 @@
</tr>
<tr>
<td class="first" width="30"><strong><%=_("Url")%></strong></td>
- <td class="last"><%= f.text_field :url %></td>
+ <td class="last"><%= f.text_field :uri, class=> "required" %></td>
</tr>
<tr class="bg">
<td class="first"><strong><%=_("Description")%></strong></td>
diff --git a/webclient/app/views/hosts/new.html.erb b/webclient/app/views/hosts/new.html.erb
index cba9a96..869de67 100644
--- a/webclient/app/views/hosts/new.html.erb
+++ b/webclient/app/views/hosts/new.html.erb
@@ -1,5 +1,24 @@
<h1><%=_("New WebYaST Host")%></h1>
+<script type="text/javascript">
+$(document).ready(function() {
+ $("#new_host").validate({
+ rules: {
+ "host[name]" : { required : true },
+ "host[url]": {
+ required: true,
+ remote: "/validate_uri"
+ }
+ },
+ // set this class to error-labels to indicate valid fields
+ success: function(label) {
+ // set as text for IE
+ label.html(" ").addClass("checked");
+ }
+ });
+});
+</script>
+
<% form_for(@host) do |f| %>
<%= f.error_messages %>
@@ -13,7 +32,7 @@
<!-- FIXME: move to a partial and share with 'edit' -->
<tr>
<td class="first" width="20"><strong><%=_("Name")%></strong></td>
- <td class="last"><%= f.text_field :name %></td>
+ <td class="last"><%= f.text_field :name, :class => "required" %></td>
</tr>
<tr>
<td class="first" width="30"><strong><%=_("Url")%></strong></td>
diff --git a/webclient/app/views/layouts/main.rhtml b/webclient/app/views/layouts/main.rhtml
index 67c426d..96bda9f 100644
--- a/webclient/app/views/layouts/main.rhtml
+++ b/webclient/app/views/layouts/main.rhtml
@@ -12,6 +12,7 @@
<script type="text/javascript" language="javascript" charset="utf-8" src="/inc/jquery.timers.js"></script>
<script type="text/javascript" language="javascript" charset="utf-8" src="/javascripts/jquery.jqplot.js"></script>
<script type="text/javascript" language="javascript" charset="utf-8" src="/javascripts/plugin/jqplot.pieRenderer.js"></script>
+ <script type="text/javascript" language="javascript" charset="utf-8" src="/javascripts/jquery.validate.js"></script>
<script type="text/javascript" language="javascript" charset="utf-8" src="/inc/jquery.ui.custom.js"></script>
Reply