RE: getting validation errors on initial display of a form

2002-03-25 Thread Dimitar Stavrakov

In the struts-config.xml try setting validate=false for the initial action
names and then set it to true for the submit action on the form of the
names.jsp . This will disable the validate() call when the form is first
initialized :

  action-mappings
action
  path=/app/names
  type=com.starrcs.app.SANameAction
  name=nameForm
  scope=request
  validate=false
  input=/pages/app/Name.jsp
  forward
name=continue
path=/pages/app/Name.jsp/
/action


-Original Message-
From: Dick Starr [mailto:[EMAIL PROTECTED]] 
Sent: Monday, January 21, 2002 9:20 PM
To: struts-user
Subject: getting validation errors on initial display of a form

I am new to Struts and am using Ted's sample logon app as my basis for
learning. His part works fine. I added a new jsp page that contains two
fields, created a form bean  an action form with validation edits. My
problem is that when I execute my form via
http://localhost:8080/starrd/app/names.do I get validation errors appearing
immediately (before I enter any data in the form). I can't believe thats
normal: I must be doing something wrong.

Thanks in advance for the help.

Dick Starr

Here's the pertinent parts of my struts-config:

  form-beans
  form-bean
name=nameForm
type=com.starrcs.app.SANameForm/

  global-forwards
forward
  name=names
  path=/app/names.do/

  action-mappings
action
  path=/app/names
  type=com.starrcs.app.SANameAction
  name=nameForm
  scope=request
  validate=true
  input=/pages/app/Name.jsp
  forward
name=continue
path=/pages/app/Name.jsp/
/action

Here's my Name.jsp:

!-- Name.jsp 2002/01/21 RJS --
%@ page language=java %
%@ taglib uri=/tags/struts-bean prefix=bean %
%@ taglib uri=/tags/struts-html prefix=html %
htmlheadtitlebean:message key=app.name.title//title/head
html:base/
body
html:errors/
html:form action=/app/names focus=name
table border=0 width=100%
trth align=rightName:/thtd align=left
  html:text property=name//td
/tr
trth align=rightAddress line one:/thtd align=left
  html:text property=addr1//td
tr
td align=righthtml:submit property=submit value=Submit//td
td align=lefthtml:reset//td
/tr
/table
/html:form
/body
/html

%--

Allow user to submit username and password to logon action.

--%

Here's the pertinent parts of SANameAction.java:

public final class SANameAction extends Action
{
  public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
  {
// Obtain fields from web tier
String name = ((SANameForm) form).getName();
String addr1 = ((SANameForm) form).getAddr1();

// Log this event, if appropriate
if (servlet.getDebug() = SAConstants.DEBUG)
{
  StringBuffer message =
new StringBuffer(SANameAction: name = );
  message.append(name);
  message.append(, addr1 = );
  message.append(addr1);
  servlet.log(message.toString());
}

// Forward control to the welcome URI
// specified in the configuration.
return (mapping.findForward(SAConstants.WELCOME));
  }

} // End SANameAction

Here's the pertinent parts of SANameForm.java:

public final class SANameForm extends ActionForm
{
  //  Instance Variables

  private String name = null;
  private String addr1 = null;

  //  Properties

  public String getName()
  {
return (this.name);
  }

  public void setName(String name)
  {
this.name = name;
  }

  public String getAddr1()
  {
return (this.addr1);
  }

  public void setAddr1(String addr1)
  {
this.addr1 = addr1;
  }

//  Public Methods
public void reset(ActionMapping mapping,
HttpServletRequest request)
{
  setName(null);
  setAddr1(null);
}

public ActionErrors validate(ActionMapping mapping,
 HttpServletRequest request)
{
  ActionErrors errors = new ActionErrors();
  if ((name == null) || (name.length()  1))
errors.add(name,
  new ActionError(app.error.name.required));
  if ((addr1 == null) || (addr1.length()  1))
errors.add(addr1,
  new ActionError(app.error.addr1.required));
  return errors;
}

} // End SANameForm

When I first execute the form via  http://localhost:8080/starrd/app/names.do
I immediately (before entering any data or executing the submit button) I
get validation errors displayed for name and addr1 and get the following in
my log file:

2002-01-21 20:19:19 action: Processing a GET for /app/names
2002-01-21 20:19:19 action:  Looking for ActionForm bean under attribute
'nameForm'
2002-01-21 20:19:19 action:  Creating new ActionForm instance of class
'com.starrcs.app.SANameForm'
2002-01-21 20:19:19 action:  Storing instance under attribute 'nameForm' in
scope 'request'
2002-01-21 20:19:19 action:  Populating bean properties 

RE: getting validation errors on initial display of a form

2002-03-25 Thread Dave Newton

Don't go to the action page (the .do), go to the .jsp page! The action
is just the target of the form, not the page you start from.

(At least that's what I've figured out?!)

Dick Starr said:
 My problem is that when I execute my form via 
 http://localhost:8080/starrd/app/names.do I get validation errors 
 appearing immediately (before I enter any data in the form). I can't 
 believe thats normal: I must be doing something wrong.

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: getting validation errors on initial display of a form

2002-03-23 Thread Anant Sagar

Since you are calling names.do   directly, formbean calls validate() method before 
forwarding to appropriate action class and hence the erros are displayed.
Call your form using this  http://localhost:8080/starrd/pages/app/Name.jsp   initially 
 
and set action=/app/names while  submitting. 
Hope that may help .

Anant Sagar

- Original Message - 
From: Dick Starr [EMAIL PROTECTED]
To: struts-user [EMAIL PROTECTED]
Sent: Tuesday, January 22, 2002 8:50 AM
Subject: getting validation errors on initial display of a form


 I am new to Struts and am using Ted's sample logon app as my basis for
 learning. His part works fine. I added a new jsp page that contains two
 fields, created a form bean  an action form with validation edits. My
 problem is that when I execute my form via
 http://localhost:8080/starrd/app/names.do I get validation errors appearing
 immediately (before I enter any data in the form). I can't believe thats
 normal: I must be doing something wrong.
 
 Thanks in advance for the help.
 
 Dick Starr
 
 Here's the pertinent parts of my struts-config:
 
   form-beans
   form-bean
 name=nameForm
 type=com.starrcs.app.SANameForm/
 
   global-forwards
 forward
   name=names
   path=/app/names.do/
 
   action-mappings
 action
   path=/app/names
   type=com.starrcs.app.SANameAction
   name=nameForm
   scope=request
   validate=true
   input=/pages/app/Name.jsp
   forward
 name=continue
 path=/pages/app/Name.jsp/
 /action
 
 Here's my Name.jsp:
 
 !-- Name.jsp 2002/01/21 RJS --
 %@ page language=java %
 %@ taglib uri=/tags/struts-bean prefix=bean %
 %@ taglib uri=/tags/struts-html prefix=html %
 htmlheadtitlebean:message key=app.name.title//title/head
 html:base/
 body
 html:errors/
 html:form action=/app/names focus=name
 table border=0 width=100%
 trth align=rightName:/thtd align=left
   html:text property=name//td
 /tr
 trth align=rightAddress line one:/thtd align=left
   html:text property=addr1//td
 tr
 td align=righthtml:submit property=submit value=Submit//td
 td align=lefthtml:reset//td
 /tr
 /table
 /html:form
 /body
 /html
 
 %--
 
 Allow user to submit username and password to logon action.
 
 --%
 
 Here's the pertinent parts of SANameAction.java:
 
 public final class SANameAction extends Action
 {
   public ActionForward perform(ActionMapping mapping,
 ActionForm form,
 HttpServletRequest request,
 HttpServletResponse response)
 throws IOException, ServletException
   {
 // Obtain fields from web tier
 String name = ((SANameForm) form).getName();
 String addr1 = ((SANameForm) form).getAddr1();
 
 // Log this event, if appropriate
 if (servlet.getDebug() = SAConstants.DEBUG)
 {
   StringBuffer message =
 new StringBuffer(SANameAction: name = );
   message.append(name);
   message.append(, addr1 = );
   message.append(addr1);
   servlet.log(message.toString());
 }
 
 // Forward control to the welcome URI
 // specified in the configuration.
 return (mapping.findForward(SAConstants.WELCOME));
   }
 
 } // End SANameAction
 
 Here's the pertinent parts of SANameForm.java:
 
 public final class SANameForm extends ActionForm
 {
   //  Instance Variables
 
   private String name = null;
   private String addr1 = null;
 
   //  Properties
 
   public String getName()
   {
 return (this.name);
   }
 
   public void setName(String name)
   {
 this.name = name;
   }
 
   public String getAddr1()
   {
 return (this.addr1);
   }
 
   public void setAddr1(String addr1)
   {
 this.addr1 = addr1;
   }
 
 //  Public Methods
 public void reset(ActionMapping mapping,
 HttpServletRequest request)
 {
   setName(null);
   setAddr1(null);
 }
 
 public ActionErrors validate(ActionMapping mapping,
  HttpServletRequest request)
 {
   ActionErrors errors = new ActionErrors();
   if ((name == null) || (name.length()  1))
 errors.add(name,
   new ActionError(app.error.name.required));
   if ((addr1 == null) || (addr1.length()  1))
 errors.add(addr1,
   new ActionError(app.error.addr1.required));
   return errors;
 }
 
 } // End SANameForm
 
 When I first execute the form via  http://localhost:8080/starrd/app/names.do
 I immediately (before entering any data or executing the submit button) I
 get validation errors displayed for name and addr1 and get the following in
 my log file:
 
 2002-01-21 20:19:19 action: Processing a GET for /app/names
 2002-01-21 20:19:19 action:  Looking for ActionForm bean under attribute
 'nameForm'
 2002-01-21 20:19:19 action:  Creating new ActionForm instance of class
 'com.starrcs.app.SANameForm'
 2002-01-21 20:19:19 action:  Storing instance under attribute 'nameForm' in
 scope 'request'
 2002-01-21 20:19:19 action:  Populating bean 

RE: getting validation errors on initial display of a form

2002-01-22 Thread Witt, Mike (OH35)

I'm pretty new to struts also, but here is what I've found:

If you set validate to true in the action mapping, the validation will 
run before the action.  If you set it to false, you may run the validate 
method of the form yourself at any time.  Also, it is interesting to note 
that if you forward to a .jsp, it only runs the action the first time, 
successive forwards to the jsp do not run the action.  In order to run 
the action, you usually want to forward to the action (/do/myAction).

In my situation, I had a form which I initially wanted to fill with data 
for editing, and then take the data and update the database.  To do 
this, I set up one action with two action mappings.  The first action 
mapping was to load the data and the second to take the data and update 
the database.  The first action mapping, I set validate to false and 
the second I set validate to true.  I also used the param attribute of 
the action within the struts config to set these differently depending 
on the action (so that my servlet could tell which action I was doing.  

Here is some sample stuff:

struts-config actions:

action path=/messageEdit 
name=messageEditForm 
type=com.xxx.purchasing.main.webapp.MessageEditAction 
scope=request 
validate=false 
input=/WEB-INF/pages/message_edit.jsp 
parameter=edit
  forward name=success path=WEB-INF/pages/message_edit.jsp /
/action

action path=/messageUpdate 
name=messageEditForm 
type=com.xxx.purchasing.main.webapp.MessageEditAction
scope=request 
parameter=update 
validate=true 
input=/do/messageEdit
  forward name=success path=/do/messageMaintenance /
/action

In my message_edit.jsp, I have:

html:form method=POST action=/messageUpdate focus=title

HTH and it's probably not perfect, but it works,

Mike Witt

-Original Message-
From: Dick Starr [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 22, 2002 8:51 AM
To: struts-user
Subject: getting validation errors on initial display of a form


I am new to Struts and am using Ted's sample logon app as my basis for
learning. His part works fine. I added a new jsp page that contains two
fields, created a form bean  an action form with validation edits. My
problem is that when I execute my form via
http://localhost:8080/starrd/app/names.do I get validation errors appearing
immediately (before I enter any data in the form). I can't believe thats
normal: I must be doing something wrong.

Thanks in advance for the help.

Dick Starr

Here's the pertinent parts of my struts-config:

  form-beans
  form-bean
name=nameForm
type=com.starrcs.app.SANameForm/

  global-forwards
forward
  name=names
  path=/app/names.do/

  action-mappings
action
  path=/app/names
  type=com.starrcs.app.SANameAction
  name=nameForm
  scope=request
  validate=true
  input=/pages/app/Name.jsp
  forward
name=continue
path=/pages/app/Name.jsp/
/action

Here's my Name.jsp:

!-- Name.jsp 2002/01/21 RJS --
%@ page language=java %
%@ taglib uri=/tags/struts-bean prefix=bean %
%@ taglib uri=/tags/struts-html prefix=html %
htmlheadtitlebean:message key=app.name.title//title/head
html:base/
body
html:errors/
html:form action=/app/names focus=name
table border=0 width=100%
trth align=rightName:/thtd align=left
  html:text property=name//td
/tr
trth align=rightAddress line one:/thtd align=left
  html:text property=addr1//td
tr
td align=righthtml:submit property=submit value=Submit//td
td align=lefthtml:reset//td
/tr
/table
/html:form
/body
/html

%--

Allow user to submit username and password to logon action.

--%

Here's the pertinent parts of SANameAction.java:

public final class SANameAction extends Action
{
  public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
  {
// Obtain fields from web tier
String name = ((SANameForm) form).getName();
String addr1 = ((SANameForm) form).getAddr1();

// Log this event, if appropriate
if (servlet.getDebug() = SAConstants.DEBUG)
{
  StringBuffer message =
new StringBuffer(SANameAction: name = );
  message.append(name);
  message.append(, addr1 = );
  message.append(addr1);
  servlet.log(message.toString());
}

// Forward control to the welcome URI
// specified in the configuration.
return (mapping.findForward(SAConstants.WELCOME));
  }

} // End SANameAction

Here's the pertinent parts of SANameForm.java:

public final class SANameForm extends ActionForm
{
  //  Instance Variables

  private String name = null;
  private String addr1 = null;

  //  Properties

  public String getName()
  {
return (this.name);
  }

  public void setName(String name)
  {
this.name = name;
  }

  public