0
votes

In my database I have changed the type of a field from VARCHAR to DECIMAL. Therefore I have change the type in the java model class from String to BigDecimal.

In my previous XML validation file I have a requiredString fieldValidator. If I leave it, everytime I enter the value (a big decimal) in the form field, the requiredString fieldValidator fires. (maybe a problem with the param interceptor that should convert String to BigDecimal).

Anyway for the same field, I have another field validator which is a REGEX.

So I thought that if I dont use the requieredString, just the regex validator it would work, but not. If i dont fill the field up, no error fires.

So.. to sum up :

my action has a field from the Beneficiary class. In the Beneficiairy class I have a BigDecimal field. my xml validation file is like that for this field :

<field name="benef.nirBenef">
    <field-validator type="regex">
        <param name="expression"><![CDATA[^(1|2)\d{14}$]]></param>
        <message key="error.securityNumber"/>
    </field-validator>

when the field is empty, the validation says it is all right.

why ?

when if I use the requiredstring validator, the validation says it is wrong even though the value is good

Thank you

1
well, first I can use required validator instead of requieredStringmlwacosmos
second, I have to create a class that extends DefaultTypeConverter to be sure that Param interceptor does his jobmlwacosmos
but still it looks like my regex validator does not work anymore... so this is the question nowmlwacosmos
the question is : why regex validator does not work when the field is a BigDecimal, knowing that I used class convertermlwacosmos

1 Answers

0
votes

I found it...it is the same thing for any objects..

*/ Create a file saying what field(s) is/are complex Name of the file : name_of_the_action-conversion.properties

for example my action class is BenefInfos.java, so my properties file is : BenefInfos-conversion.properties

benef.nirBenef=model.BigDecimalConverter
benef.nirAssure=model.BigDecimalConverter

the value is the name of my converter class BigDecimalConverter in model package

*/ Create BigDecimalConverter.java

public class BigDecimalConverter extends DefaultTypeConverter {

@Override
public Object convertValue(Map Context, Object values, Class classe) {
    BigDecimal field = null;

    if(classe == BigDecimal.class) {
        String value = ((String[]) values)[0];
        field = new BigDecimal(value);
    }

    return field;
} 

}

*/Now if you want to VALIDATE, you have to create your own validator. To do so :

*/ add validators.xml to declare your own validator :

<validators>
    <validator name="socialsecurityformat" 
        class="validator.SocialSecurityValidator"
    />
</validators>

*/Create the validator class (here it is SocialSecurityValidator)

public class SocialSecurityValidator extends FieldValidatorSupport{
  public void validate(Object o) throws ValidationException {
    String fieldName = this.getFieldName();

    if(this.getFieldValue(fieldName, o) != null) {
        String value = ((BigDecimal)this.getFieldValue(fieldName, o)).toString();
        Pattern p = Pattern.compile("^(1|2|7)\\d{14}$");
        Matcher m = p.matcher(value);

        if(!m.matches()) {
            addFieldError(fieldName, o);
        }
    }
  }

}

*/ and finally in the validation xml file you use your own validator.

<field name="benef.nirBenef">
    <field-validator type="required">
        <message key="error.requiredField"/>
    </field-validator>
    <field-validator type="socialsecurityformat">
        <message key="error.securityNumber"/>
    </field-validator>
</field>

Hope this is helpful