|
From: <ty...@me...> - 2005-09-13 20:38:59
|
Hi again Steven,
One other thing that we just worked on in our sandbox was a simple Regula=
r
Expression Function for using the ValangVisitor interface. Now we can us=
e
this to handle all kinds of difference scenarios
(phone/email/creditcard/ssn/etc). I have included an example of how we
are wiring it up, for anyone who is interested. I know know there was
talk about something similar the other day.
Let me know if you think this would be useful for Valang. Or if there
would be better way to implement this.
Thanks,
Tyler
<bean id=3D"visitorValidator"
class=3D"org.springmodules.validation.ValangValidatorFactoryBean">
<property name=3D"valang">
<value><![CDATA[
{ phoneNumber : isPhoneNumber(?) =3D=3D TRUE : 'Invalid Phone Number. =
Must
be in format (###)###-####' }
{ email : isEmail(?) =3D=3D TRUE : 'Invalid E-mail Address. Must be =
in
format a@b.c' }
]]>
</value>
</property>
<!-- Here, we inject necessary properties to create the Function -->
<property name=3D"visitor">
<bean class=3D"org.springmodules.validation.RegExVisitor">
<property
name=3D"regExPatternString"><value>\(\d{3}\)\d{3}-\d{4}</value></property=
>
<property name=3D"functionName"><value>isPhoneNumber</value></property>
<property name=3D"visitor">
<bean class=3D"org.govgrnds.core.validation.valang.RegExVisitor">
<property name=3D"regExPatternString">
<value>"(\w+)@(\w+\.)(\w+)(\.\w+)*</value>
</property>
<property name=3D"functionName">
<value>isEmail</value>
</property>
</bean>
</property>
</bean>
</property>
</bean>
> Hi Steven,
> ValidatingSupport does support my needs quite well. I was moreso
> talking about the interceptors that are in the
> "org.springmodules.orm.support.validation" package. Also
> ValidatingSupport really be under the org.springmodules.orm package
> when it is not orm specfic. I guess I'm being a little picky :)
>
> I have attached my code, like I said its not perfect and took we about =
a
> whole 10 minutes to do. But it is working for us right now pretty nicel=
y.
>
> Also we are setting basic validation funcations for standard validation=
s
> like phonenumber, email, etc. Are there standard validation functions l=
ike
> these for Valang already?
>
> /*
> * Copyright 2004-2005 the original author or authors.
> *
> * Licensed under the Apache License, Version 2.0 (the "License");
> * you may not use this file except in compliance with the License.
> * You may obtain a copy of the License at
> *
> * http://www.apache.org/licenses/LICENSE-2.0
> *
> * Unless required by applicable law or agreed to in writing, software
> * distributed under the License is distributed on an "AS IS" BASIS,
> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> * See the License for the specific language governing permissions and
> * limitations under the License.
> */
> package org.springmodules.orm.support.validation;
>
> import org.aopalliance.intercept.MethodInterceptor;
> import org.aopalliance.intercept.MethodInvocation;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.springframework.beans.factory.InitializingBean;
> import org.springmodules.orm.support.validation.ValidatingSupport;
>
>
> /**
> * <p>
> * Interceptor Process different validations on method invocations,
> * uses springmodules
> * ValidatingSupport to validate any method from any bean and not just
> * ORM beans like in the HibernateIntercepter examples.
> * </p>
> *
> * <p>
> * Combination of springmodules and acegisecurity's
> * validationexamples. Took the pieces we needed and
> * glued them together
> * </p>
> *
> *
> *
> * @author Tyler Nelson
> * @see org.springmodules.orm.support.validation.ValidatingSupport
> *
> */
> public class ValidatingInterceptor extends ValidatingSupport implements
> MethodInterceptor, InitializingBean {
>
> protected final Log log =3D LogFactory.getLog(getClass());
>
> private Class[] argumentClasses;
>
> public void afterPropertiesSet() throws Exception {
>
> }
>
> /**
> * <p>This method binds and unbinds the validator maps
> * configured in the validators
> * property.
> *
> * @param methodInvocation the method invocation
> * @return the result of the method invocation
> */
> public Object invoke(MethodInvocation mi) throws Throwable {
>
> try {
> bindValidators();
>
> Object[] args =3D mi.getArguments();
>
> for (int i =3D 0; i < args.length; i++) {
> if (isValidatable(args[i])) {
> if (log.isDebugEnabled()) {
> log.debug("Validatingnterceptor calling "
> + "for: '" + args[i] + "'");
> }
> ValidatingSupport.validate(args[i],
> mi.getMethod().getName());
> }
> }
>
>
> return mi.proceed();
> }
> finally {
> unbindValidators();
> }
>
> }
>
> /**
> * @return Returns the argumentClasses.
> */
> public Class[] getArgumentClasses() {
> return argumentClasses;
> }
> /**
> * @param argumentClasses The argumentClasses to set.
> */
> public void setArgumentClasses(Class[] argumentClasses) {
> this.argumentClasses =3D argumentClasses;
> }
> /**
> * Determines if an attribute needs to be validated
> *
> * @param argument
> * @return
> */
> private boolean isValidatable(Object argument) {
> // if argumentClasses is null or empty advise all arguments
> if(argumentClasses =3D=3D null || argumentClasses.length < 1){
> return true;
> }
>
> if (argument =3D=3D null) {
> return false;
> }
>
> for (int i =3D 0; i < argumentClasses.length; i++) {
> if (argumentClasses[i].isAssignableFrom(argument.getClass()=
))
> {
> return true;
> }
> }
>
> return false;
> }
> }
>
>
>
>> Tyler,
>>
>> What kind of validation do you want to do? Obviously
>> ValidatingSupport can be reused in other scenario's than just
>> persistence. The event mechanism should be sufficiently powerful to
>> leverage it in other place. Could you please provide some code
>> example of the integration with Acegi?
>>
>> Thanks
>>
>> Steven Devijver
>> Senior consultant
>> @ Interface21
>> ste...@in...
>>
>>
>>
>> On 13 Sep 2005, at 17:16, ty...@me... wrote:
>>
>>> Hi,
>>> With all this talk about Validation made me want to speak up. I am
>>> currently using Valang validation which works great. However I
>>> wanted a
>>> way to trigger the validation on different layers of our
>>> application. I
>>> was thinking of injecting a map of Validator object into a method
>>> interceptor. Then I found ValidatingInterceptor and
>>> ValidatingSupport in
>>> SpringModules, which does this already. However it seems to be very
>>> ORM
>>> specific where it doesn=92t need to be.
>>>
>>> To make it more generic, I combined it with a ValidationInterceptor
>>> that
>>> Ben Alex had in the Acegi Domain project. Like the Acegi version I
>>> included a way to filter out objects on the interceptor level.
>>> However it
>>> seems a little redundant right now since ValidatingSupport figures
>>> out via
>>> the map keys. I see real value in adding something like this
>>> interceptor
>>> to Spring Modules. Does anyone have any interest in using it?
>>>
>>> Thanks,
>>> Tyler
>>>
>>>
>>>
>>> -------------------------------------------------------
>>> SF.Net email is Sponsored by the Better Software Conference & EXPO
>>> September 19-22, 2005 * San Francisco, CA * Development Lifecycle
>>> Practices
>>> Agile & Plan-Driven Development * Managing Projects & Teams *
>>> Testing & QA
>>> Security * Process Improvement & Measurement * http://www.sqe.com/
>>> bsce5sf
>>> _______________________________________________
>>> Springframework-developer mailing list
>>> Spr...@li...
>>> https://lists.sourceforge.net/lists/listinfo/springframework-develope=
r
>>>
>>>
>>
>>
>>
>> -------------------------------------------------------
>> SF.Net email is Sponsored by the Better Software Conference & EXPO
>> September 19-22, 2005 * San Francisco, CA * Development Lifecycle
>> Practices
>> Agile & Plan-Driven Development * Managing Projects & Teams * Testing =
&
>> QA
>> Security * Process Improvement & Measurement *
>> http://www.sqe.com/bsce5sf
>> _______________________________________________
>> Springframework-developer mailing list
>> Spr...@li...
>> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>>
>
>
>
> -------------------------------------------------------
> SF.Net email is Sponsored by the Better Software Conference & EXPO
> September 19-22, 2005 * San Francisco, CA * Development Lifecycle
> Practices
> Agile & Plan-Driven Development * Managing Projects & Teams * Testing &=
QA
> Security * Process Improvement & Measurement * http://www.sqe.com/bsce5=
sf
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
|