Commits

Kenneth Love committed d9a06c1

better form field handling. form errors. redirect to key on successful save.

Comments (0)

Files changed (5)

             if r.exists(key):
                 return jsonify(flash=key + ' already exists.')
 
-            r.set(key, value)
-            return jsonify(flash=key + ' was saved successfully.')
-        else:
-            return jsonify(flash='Motherfuckin\' Errors!')
+            try:
+                r.set(key, value)
+                flash('%s was saved successfully.' % key)
+                return redirect('#%s' % key)
+            except:
+                return jsonify(flash=key + ' was not saved successfully.')
 
     return render_template('new_key.html', form=form)
 
 }
 a:hover { text-decoration:underline; }
 
+.errors {
+  list-style:none;
+}
+  .errors li {
+    background:rgb(133, 27, 31);
+    color: #fff;
+    margin-top:10px;
+    padding:10px;
+  }
+
 #masthead {
   overflow:hidden;
   padding:10px;
     }
     #setup_form input[type=text], #new_key_form input[type=text] {
       border:1px solid rgba(30,30,30,0.5);
-      padding:1em;
-      width:100%;
+      padding:1em 2%;
+      width:96%;
     }
 
 footer {

static/js/redbeard.js

         });
     });
 
-    $('#new_key').live('click', function(e) {
-        var link = $(this).attr('href');
-        e.preventDefault();
-        $.get(link, function(data) {
-            $('#right').html(data);
-        });
-    });
+    //$('#new_key').live('click', function(e) {
+    //    var link = $(this).attr('href');
+    //    e.preventDefault();
+    //    $.get(link, function(data) {
+    //        $('#right').html(data);
+    //    });
+    //});
 
 });

templates/_partials.html

+{% macro render_field(field) %}
+<fieldset>
+	{% if field.errors %}
+	<ul class="errors">
+		{% for error in field.errors %}
+		<li>{{ error }}</li>
+		{% endfor %}
+	</ul>
+	{% endif %}
+	{{ field.label }}
+	{{ field(**kwargs)|safe }}
+</fieldset>
+{% endmacro %}

templates/new_key.html

+{% extends "layout.html" %}
+{% block leftblock %}
 <article>
 	<header>
 		<h1>Add New Key</h1>
 	</header>
+	{% from "_partials.html" import render_field %}
 	<form action="/new" method="post" id="new_key_form">
 		{{ form.csrf }}
-		{{ form.key_name.label }}: {{ form.key_name }}
-		<br>
-		{{ form.key_value.label }}: {{ form.key_value }}
-		<br>
+		{{ render_field(form.key_name) }}
+		{{ render_field(form.key_value) }}
 		<input type="submit" value="Submit">
 	</form>
 
 </article>
-
-<script>
-	$(function() {
-
-		$("#new_key_form").submit(function(e) {
-			e.preventDefault();
-
-			var link = $(this).attr("action"),
-				flashes = $("#flashes");
-
-			flashes.empty();
-
-			$.ajax({
-				url: link,
-				type: "POST",
-				data: {
-					"key_name": $(this).find("input#key_name").val(),
-					"key_value": $(this).find("input#key_value").val()
-				},
-				dataType: "json",
-				success: function(data) {
-					$("<li>").text(data['flash']).appendTo(flashes);
-				}
-			});
-		});
-
-	});
-
-</script>
+{% endblock %}