Source

webwork / docs / validationexample.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>WebWork Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
</head>
<body>
  <div id="page-logo">
    <a href="index.html"><img src="logo-small.png" border="0"/></a>
  </div>
    <div class="snip-title">
	  <h1 class="snip-name">VisitorFieldValidatorExample
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 for more info on VisitorFieldValidator go here:
<a href="fieldvalidator.html">Using the VisitorFieldValidator</a><p class="paragraph"/>I've been using the validator in webwork and have found myself duplicating a few of the validations throughout the app, so after asking a few questions I was told what to do.<p class="paragraph"/>basically I have a creation form to create a Car, and then I have another to Edit and Update the Car, I wanted to create just one validation file that would validate that Car, I was using 2 validation files, CreateCarAction-validation.xml and UpdateCarAction-validation.xml and both were identical.<p class="paragraph"/>what I had to do was:<p class="paragraph"/>first i had to expose the Car object in my action, then I had to create a validation file for the action, and placed this file in the same package as the CarAction in this example;<p class="paragraph"/>CarAction-validation.xml
<div class="code"><pre>&#60;!DOCTYPE validators PUBLIC <span class="java&#45;quote">"&#45;//OpenSymphony Group//XWork Validator 1.0//EN"</span> <span class="java&#45;quote">"http://www.opensymphony.com/xwork/xwork&#45;validator&#45;1.0.dtd"</span>&#62;
&#60;validators&#62;
    &#60;field name=<span class="java&#45;quote">"car"</span>&#62;
        &#60;field&#45;validator type=<span class="java&#45;quote">"visitor"</span>&#62;
            &#60;message&#62;Car Visitor: &#60;/message&#62;
        &#60;/field&#45;validator&#62;
    &#60;/field&#62;
&#60;/validators&#62;</pre></div><p class="paragraph"/>next I had to create Car-validation.xml file and put this file in the same package as the Car class<p class="paragraph"/>Car-validation.xml
<div class="code"><pre>&#60;!DOCTYPE validators PUBLIC <span class="java&#45;quote">"&#45;//OpenSymphony Group//XWork Validator 1.0//EN"</span> <span class="java&#45;quote">"http://www.opensymphony.com/xwork/xwork&#45;validator&#45;1.0.dtd"</span>&#62;
&#60;validators&#62;<p class="paragraph"/>    &#60;field name=<span class="java&#45;quote">"name"</span>&#62;
        &#60;field&#45;validator type=<span class="java&#45;quote">"requiredstring"</span>&#62;
            &#60;message&#62;Name is REQUIRED&#60;/message&#62;
        &#60;/field&#45;validator&#62;
    &#60;/field&#62;
    &#60;field name=<span class="java&#45;quote">"model"</span>&#62;
        &#60;field&#45;validator type=<span class="java&#45;quote">"requiredstring"</span>&#62;
            &#60;message&#62;Model is REQUIRED&#60;/message&#62;
        &#60;/field&#45;validator&#62;
    &#60;/field&#62;<p class="paragraph"/>&#60;/validators&#62;</pre></div><p class="paragraph"/>so now if the input strings car.model and car.name are empty you should get fieldErrors added to the car.name and car.model fields, you just need to make sure that in your action you expose the Car object.<p class="paragraph"/>heres the CarAction.java file:
<div class="code"><pre><span class="java&#45;keyword">public</span> class CarAction <span class="java&#45;keyword">extends</span> ActionSupport &#123;<p class="paragraph"/>    <span class="java&#45;keyword">private</span> Car car = <span class="java&#45;keyword">new</span> Car();<p class="paragraph"/>    <span class="java&#45;keyword">public</span> Car getCar() &#123;
        <span class="java&#45;keyword">return</span> car;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> void setCar(Car car) &#123;
        <span class="java&#45;keyword">this</span>.car = car;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> execute() &#123;
        <span class="java&#45;keyword">return</span> SUCCESS;
    &#125;<p class="paragraph"/>
&#125;</pre></div>
heres my Car bean:
<div class="code"><pre><span class="java&#45;keyword">public</span> class Car &#123;
    <span class="java&#45;keyword">private</span> <span class="java&#45;object">String</span> name;
    <span class="java&#45;keyword">private</span> <span class="java&#45;object">String</span> model;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> Car() &#123;&#125;<p class="paragraph"/>   <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> getName() &#123;
        <span class="java&#45;keyword">return</span> name;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> void setName(<span class="java&#45;object">String</span> name) &#123;
        <span class="java&#45;keyword">this</span>.name = name;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> <span class="java&#45;object">Integer</span> getId() &#123;
        <span class="java&#45;keyword">return</span> id;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> void setId(<span class="java&#45;object">Integer</span> id) &#123;
        <span class="java&#45;keyword">this</span>.id = id;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> <span class="java&#45;object">String</span> getModel() &#123;
        <span class="java&#45;keyword">return</span> model;
    &#125;<p class="paragraph"/>    <span class="java&#45;keyword">public</span> void setModel(<span class="java&#45;object">String</span> model) &#123;
        <span class="java&#45;keyword">this</span>.model = model;
    &#125;
&#125;</pre></div>
and then the car.vm file:
<div class="code"><pre>&#60;form method=<span class="java&#45;quote">"get"</span>&#62;<p class="paragraph"/>&#60;table border=<span class="java&#45;quote">"1"</span> align=<span class="java&#45;quote">"center"</span>&#62;<p class="paragraph"/>    #tag( TextField <span class="java&#45;quote">"label=Car ID"</span> <span class="java&#45;quote">"name=car.id"</span> <span class="java&#45;quote">"value=car.id"</span> )
    #tag( TextField <span class="java&#45;quote">"label=Car Name"</span> <span class="java&#45;quote">"name=car.name"</span> <span class="java&#45;quote">"value=car.name"</span> )
    #tag( TextField <span class="java&#45;quote">"label=Car Model"</span> <span class="java&#45;quote">"name=car.model"</span> <span class="java&#45;quote">"value=car.model"</span> )<p class="paragraph"/>    #tag( Submit <span class="java&#45;quote">"value='Submit Car'"</span> <span class="java&#45;quote">"align=center"</span>)<p class="paragraph"/>&#60;/table&#62;<p class="paragraph"/>&#60;/form&#62;</pre></div>
  </div>
</body>
</html>