Making custom model validations in extjs4.

Adding a custom validation to the Ext.data.validations is quite simple.

The are a few validations already, but not one that validates, that a number value lies within a minimum and maximum. I will call the new validation for range

Ext.data.validations is a singleton and the new range validation is installed by adding an error message and a validation function:

Ext.data.validations.rangeMessage = 'Value must be between {0} and {1}' ;

Ext.data.validations.range = function(config, value) {
   if (value === undefined) {
      return false;
   }
   var min = config.min,
   max = config.max;
   if ((value < min) || (value > max)) {
      config.message = Ext.String.format(this.rangeMessage, min, max);
      return false;
   } else {
      return true;
   }
}

The validation is configured like this in the validations of the model:

{
   field: 'price',
   type: 'range',
   min: 5,
   max: 100
}

This is also the config parameter of the validation function and the value parameter is the value of the field. So we have a pricfield where the number has to be between 5 and 100.

This range validation is a bit more complicated than the other validations, because it has an error message with parameters and it is calculated in the validation function. The calculated message is stored in config.message. When the model is validated the model.validate() method first checks for a config.message, then for Ext.data.validations.rangeMessage.

Running tomcat7 with maven2/3

Here I will describe how to setup Maven with Tomcat 7. Also I will show a small Ant script for deploying simple view changes like changes in css, html, js etc.  –  changes that don’t require a rebuild of the war file.

I could let the tomcat-maven-plugin run tomcat for me using the

mvn tomcat:run

, but I already have a tomcat installed, that I want to use. I need to tell the plugin about it in the pom.xml:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>tomcat-maven-plugin</artifactId>
	<version>1.1</version>
	<configuration>
		<server>local_tomcat7</server>
		<url>http://localhost:8080/manager/text</url>
		<path>/appname</path>
	</configuration>
</plugin>

Maven looks for ‘local_tomcat7’ in ~/.m2/setting.xml, where the username and password for the tomcat manager can be found in the servers section:

<servers>
	<server>
		<id>local_tomcat7</id>
		<username>admin</username>
		<password>mypw</password>
	</server>
</servers>

Last we need to create the manager user in tomcat. This is done in …/apache-tomcat-7.0.12/conf/tomcat-users.xml:

<tomcat-users>
	<user name="admin" password="mypw" roles="manager-script,manager-gui" />
</tomcat-users>

Now the setup is ready for running, deploying and redeploying on tomcat via the tomcat manager.
When working on the view part of the application, it is not really necessary to build a new war file and redeploy it via the manager – its a bit slow. Lets say I only made changes in a java-script file, then I really only need to copy that file directly to …\apache-tomcat-7.0.12\webapps\appname. For this I’m using a small ant script – build.xml:

<?xml version="1.0"?>
<project name="appname" basedir=".">
    <property name="appserver.home" value="C:/java/apache/apache-tomcat-7.0.12"/>
    <property name="deploy.path" value="${appserver.home}/webapps"/>
    <property name="web.dir" value="src/main/webapp"/>
    <property name="name" value="appname"/>
    <target name="copyFilesToServer" description="Deploy files">
        <copy todir="${deploy.path}/${name}" preservelastmodified="true">
            <fileset dir="${web.dir}">
                <include name="**/*"/>
            </fileset>
        </copy>
    </target>
</project>

, with this script it is very fast to deploy simple changes in the view.