Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This document describes how valiation works in Stripes, how you specify validations, and how ActionBeans interact with validation. It includes the following sections:

Table of Contents
layout
layoutlist
minLevel2
listanchortrue

Overview

Validation is conceptually broken into three areas in Stripes:

...

  • actionPath.fieldName.valueFailedExpression
  • actionPath.fieldName.errorMessage
  • fieldName.valueFailedExpression
  • fieldName.errorMessage
  • actionPath.valueFailedExpression
  • validation.expression.valueFailedExpression

NOTE: Some containers may not permit the usage of "this" within EL expressions by default (e.g. Tomcat 7).  In those cases, you can either check your container's documentation to see if it offers a configuration option to skip identifier checking – or you can use "self" as a substitute for "this".

@Validate(converter=PercentageTypeConverter.class)

...

Code Block
languagejs
/** 
 * 
 * Jquery addapter class so we can use built in Jquery validator. 
 * NOTE: this is only skeleton, implementing only small part of possible validation, 
 * Usage: 
 * <s:field-metadata var="metaData"> 
 * $(function(){$.fn.stripesValidation('${metaData.formId}', ${metaData});}); 
 * </s:field-metadata> 
 * dependancies: 
 * jquery.1.3.1 
 * jquery.form 
 * jquery.validate 
 */ 
(function($){ 
	/** 
	 * Processess Stripes meta data and converting it so jquery 
	 * validation plugin is used for client side validation 
	 * @param formId id of a form to be processed 
	 * @param metaData metadata that is produced by stripes tag 
	 */ 
	$.fn.stripesValidation = function(formId, metaData) { 
		var form = $('#' + formId); 
		var mask_count = 0; 
	for (var fieldName in metaData) { 
		var field = form.find('[name=' + fieldName + ']'); 
		addValidation(field, metaData[fieldName]); 
		// run validation: 
		form.validate(); 
	} 

	function addValidation(field, obj) { 
		for (var prop in obj) { 
			debug(prop); 
			switch(prop){ 
				case 'label': 
					break; 
				case 'required': 
					if (obj[prop]) { // add only if true 
						field.addClass(prop); 
					} 
					break; 
				case 'minlength': // should already be there 
					field.attr(prop, obj[prop]); 
					break; 
				case 'maxlength': // should already be there 
					field.attr(prop, obj[prop]); 
					break; 
				case 'mask': 
					setMask(field, obj[prop]); 
					break; 
				case 'typeConverter': 
					setConverter(field, obj[prop]); 
					break; 
				default: 
					debug('missing this:' + prop + ':' + [obj[prop]]); 
			} 
		}
	} 

	/** 
	 * Adds regular expression validation 
	 * @param field field reference 
	 * @param mask regular expression mask 
	 */ 
	function setMask(field, mask) { 
		mask_count++; 
		var methodRef = 'maskMethod' + mask_count; 
		field.addClass(methodRef); 
		$.validator.addClassRules({ 
			methodRef: { 
				methodRef: true 
			} 
		}); 
		$.validator.addMethod(methodRef, function (value) { 
			return mask.test(value); 
		}, 'Value is invalid'); 
	} 

	/** 
	 * Add converter mappings 
	 * @param field field reference 
	 * @param converter converter used by stripes for given field 
	 */ 
	function setConverter(field, converter) { 
		if (converter == 'EmailTypeConverter') { 
			field.addClass('email'); 
		} 
		else { 
			debug('missing converter mapping:' + converter); 
		} 
	} 

	function debug(msg){ 
		if (window.console && window.console.log) { 
			window.console.log(msg); 
		} 
	} 
}; 
})(jQuery); 

...