PK "f=0c$ $ manifestversion=1.0 frameworkVersions=1.1 PK ܴ=W:< < README.textileh1. HTML5 Validation p. The HTML5 validation module provides a drop-in replacement for HTML @@ elements called @#{input /}@ which automatically adds HTML5 validation attributes to the final output based on your Play! model validation annotations. h2. Example p. Suppose you have a Model class called @User@ which has a field called @name@ declared as bq. @Required @MaxSize(8) @Match("[a-z]*") public String name; p. you can replace your existing @@ element with the following: pre. #{input for:'user.name', id:'YourID', class:'class1 class2', value:field.value /} The tag will then output the following HTML code: pre. p. *Note*: The @${field.value}@ expression will actually be resolved to the current value of the users name. @field.value@ is here used in conjunction with the "built-in field tag":http://www.playframework.org/documentation/1.1/tags#field but you can use other values like @${user?.name}@ too. PK =f0 0 1 app/tags/html5validation/HTML5ValidationTags.java/* Project: play-html5-validation * Package: tags.html5validation * File : HTML5ValidationTags * Created: Dec 5, 2010 - 7:27:42 PM * * * Copyright 2010 Sebastian Hoß * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package tags.html5validation; import groovy.lang.Closure; import java.io.PrintWriter; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.Map; import play.Play; import play.data.validation.Email; import play.data.validation.Match; import play.data.validation.Max; import play.data.validation.MaxSize; import play.data.validation.Min; import play.data.validation.Range; import play.data.validation.Required; import play.data.validation.URL; import play.db.Model; import play.exceptions.TemplateCompilationException; import play.templates.FastTags; import play.templates.GroovyTemplate.ExecutableTemplate; /** *
The HTML5 validation tags provide a simple #{input /}
tag which can be used as a drop-in
* replacement for existing HTML5 <input>
elements.
The #{input /}
will try to map existing data validation annotations from your Play! model
* to the HTML5 input element and thus provide near codeless client-side validation without using JavaScript.
On top of that it supports all available attributes from the original HTML5 input element and auto-
* fills the name
attribute by default.
For that to work you have to specify the model instance and its field you want to map by using the
* for attribute:
*
* #{input for:'user.name', value:field.value /}
In addition to the {@link #STANDARD_ATTRIBUTES standard attributes} this tags supports the following extra * attributes: *
#{input /}
tag must be properly closed.Username validation
*Suppose you have a {@link Model} called User
which has a field called
* name
declared as
@Required
* @MaxSize(8)
* public String name;
and you pass an instance of that class called user around, you then can
* specify the field from that instance inside the #{input /}
as follows:
#{input for:'user.name', id:'YourID', class:'class1 clas2', value:field.value /}
The tag will then output the following HTML code:
<input name="user.name" value="${field.value}" id="YourID" class="class1 class2" required
* maxlength="8">
Note: The ${field.value}
expression will actually be resolved to the
* current value of that instance field.
Using additional attributes
*When you want to give the final <input>
some additional attributes like
* data-validate
you use the attributes attribute from the #{input /}
* tag:
#{input for:'user.name', value:field.value, attributes:'data-validate="..."' /}
This produces the following HTML code:
<input name="user.name" value="${user.name}" data-validate="...">
<input>
elements. */
private static final ListGenerates an HTML5 <input>
element with some values derived from a given field.
Print all standard attributes which have to be specified by the user itself.
* * @param args The map containing the wanted attributes. * @param out The print writer to use. */ private static void printStandardAttributes(Map, ?> args, PrintWriter out) { for (String attribute : STANDARD_ATTRIBUTES) { if (args.containsKey(attribute) && args.get(attribute) != null) { printAttribute(attribute, args.get(attribute).toString(), out); } } } /** *Prints validation attributes for a given field.
* * @param fieldname The name of the field. * @param args The tag attributes. * @param body The tag body. * @param out The print writer to use. * @throws SecurityException Thrown when either the field or the getter for the field can't be reached. * @throws NoSuchFieldException Thrown when the field can't be reached. * @throws ClassNotFoundException Thrown when the class could not be found. */ private static void printValidationAttributes(Map, ?> args, PrintWriter out) throws ClassNotFoundException, SecurityException, NoSuchFieldException { final String fieldname = args.get("for").toString(); final String[] components = fieldname.split("\\."); Class> clazz = null; for (Class> current : Play.classloader.getAllClasses()) { if (current.getSimpleName().equalsIgnoreCase(components[0])) { clazz = current; } } final Field field = clazz.getField(components[1]); // Print the name of the field printAttribute("name", fieldname, out); // Mark readonly if (Modifier.isFinal(field.getModifiers()) || args.containsKey("readonly")) { printAttribute("readonly", "readonly", out); } // Print the validation data if (field.isAnnotationPresent(Required.class)) { printAttribute("required", "required", out); } if (field.isAnnotationPresent(Min.class)) { final Min min = field.getAnnotation(Min.class); printAttribute("min", String.valueOf(min.value()), out); } if (field.isAnnotationPresent(Max.class)) { final Max max = field.getAnnotation(Max.class); printAttribute("max", String.valueOf(max.value()), out); } if (field.isAnnotationPresent(Range.class)) { final Range range = field.getAnnotation(Range.class); printAttribute("min", String.valueOf(range.min()), out); printAttribute("max", String.valueOf(range.max()), out); } if (field.isAnnotationPresent(MaxSize.class)) { final MaxSize maxSize = field.getAnnotation(MaxSize.class); printAttribute("maxlength", String.valueOf(maxSize.value()), out); } if (field.isAnnotationPresent(Match.class)) { final Match match = field.getAnnotation(Match.class); printAttribute("pattern", match.value(), out); } if (field.isAnnotationPresent(URL.class)) { printAttribute("type", "url", out); } if (field.isAnnotationPresent(Email.class)) { printAttribute("type", "email", out); } } /** *Prints additional attributes passed in through the attributes
attribute.
Prints a single attribute using a given print writer.
* * @param name The name of the attribute to print. * @param value The value of the attribute to print. * @param out The print writer to use. */ private static void printAttribute(final String name, final String value, final PrintWriter out) { out.print(" " + name + "=\"" + value + "\""); } }PK =: 2 bin/tags/html5validation/HTML5ValidationTags.class 2O (tags/html5validation/HTML5ValidationTags play/templates/FastTags STANDARD_ATTRIBUTES Ljava/util/List; Signature $Ljava/util/List