[studs-user] Re: studs-user digest, Vol 1 #30 - 1 msg
Status: Beta
Brought to you by:
mojavelinux
|
From: Timur V. <ti...@e-...> - 2005-07-03 14:06:55
|
Hello, studs-user-request.
Sunday, July 3, 2005, 7:37:50 AM, you wrote:
surlsn> Today's Topics:
surlsn> 1. How to implement validation? (Adriano Bonat)
surlsn> Hello!
surlsn> I'm doing some tests in Golflog sample application, and could not
surlsn> implement a validation at the present moment.
I use a following method:
Abstract class BaseActionForm
<?php
import('studs.action.ActionForm');
import('studs.action.ActionMessages');
import('studs.action.ActionMessage');
=20
class BaseActionForm extends ActionForm
{
var $id;
=20
function setId($value)
{
$this->id =3D $value;
}
=20
function getId()
{
return $this->id;
}
=20
function validate(&$mapping, &$request)
{
if ($this->isSubmited($mapping->name))
{
$errors =3D $this->isValid($mappingm, $requ=
est);
=20
if ($errors)
{
BeanUtils::populate($form, $_REQUES=
T);
=20
return $errors;
}
else
return null;
}
{
$this->firsFormCall($mapping, $request);
=20
return null;
}
}
=20
function isSubmited($frm_name =3D null, $method =3D 'POST')
{
return isset($_REQUEST[$frm_name]) && (is_null($met=
hod) || $_REQUEST[$frm_name] =3D=3D $method); =20
}
function &isValid(&$mapping, &$request)
{
$errors =3D& new ActionMessages();
=20
return $errors;
}
=20
function firsFormCall(&$mapping, &$request)
{
}
=20
function isItemExists($obj)
{
if (is_null($obj))
return false;
=20
return $obj->getId() !=3D $this->getId();
} =20
}
?>
And at the end class NewsForm (for example) I overload isValid method
class NewsForm extends BaseActionForm
{
...
function &isValid(&$mapping, &$request)
{
$errors =3D& parent::isValid($mapping, $request);
=20
if (!$this->getAnno())
$errors->add(c('ActionMessages::GLOBAL_MESS=
AGE'), new ActionMessage('news.errors.anno'));
=20
if (!$this->getText())
$errors->add(c('ActionMessages::GLOBAL_MESS=
AGE'), new ActionMessage('news.errors.text'));
=20
return $errors;
}
...
}
struts-config.xml changes:
<action
path =3D "/admin/news"
type =3D "app.admin.NewsAction"
name =3D "adminNewsForm"
scope =3D "request"
validate =3D "true"
input =3D "edit"
>
<forward name=3D"browse" path=3D"/WEB-INF/content/page/=
admin/news.browse.psp" />
<forward name=3D"edit" path=3D"/WEB-INF/content/page/ad=
min/news.edit.psp" />
<forward name=3D"next" path=3D"/admin/news.html" redire=
ct=3D"true" />
</action>
....
<message-resources parameter=3D"application" />
application.properties:
news.errors.date=3DDate error
news.errors.anno=3DAnno error
news.errors.text=3DText error
And at the psp page errors output:
<html:messages id=3D"message" scope=3D"request">
<div class=3D"error-block"><c:out value=3D"${message}" /></div>
</html:messages>
--
Send plain text only to timurv at e-kazan dot ru
PGP key: http://wwwkeys.us.pgp.net:11371/pks/lookup?op=3Dget&search=3D0x42=
5B9F0F
--
Timur Vafin
|