1

i m facing to a struts validator problem.

I has an object like this:

public class Reconstitution {
       private List<FormuleReconstitution> formuleList;
      ...
      ...some other attributes and methods...
}

and :

public class FormuleReconstitution{
    private Map<Long, ElementFormuleReconstitution> serieMap;
    ...
    ...some other attributes and methods...
}

and

public class ElementFormuleReconstitution {
    private Double coefficient;
}

i would like to add a validator on coefficient in MyAction-validation.xml but i don't know how to do this. I know how to add a simple validator but not when i have sublist with submap :(

my generated html code look like this :

<input type="text" class="texte" id="reconstitutionformuleList0listeElementFormuleReconstitution0coefficient" tabindex="0" value="" name="reconstitution.formuleList[0].listeElementFormuleReconstitution[0].coefficient"/>

how can i add a validator on the field Coefficient ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • *i don't know how to do this* is not a question. – Roman C Nov 17 '14 at 15:05
  • i added my question "how can i add a validator on the field Coefficient ?" ;) – Mançaux Pierre-Alexandre Nov 17 '14 at 15:26
  • Do you want to use JSR 303? – Roman C Nov 17 '14 at 15:33
  • first of all i simply just want to know how to create a validator on this type of field. Because now if i type 'aaa', struts look for a setCoefficient(String coefficient) but this method not exist and say nothing about this error and validation look ok. I want to say to struts to call setCoefficient(Double coefficient) with 'aaa' string but validator should say something like this 'its not a double'. And i want to do this using struts validator, no javascript etc...;) – Mançaux Pierre-Alexandre Nov 17 '14 at 15:48
  • you don't need a struts validator, if your field is of type double, and you pass a string to it then struts will try to convert a string to double and if it fails you will get a conversion error. – Roman C Nov 17 '14 at 15:59
  • I m agree with you but struts say nothing about this conversion error. I have another expression validator on my action, struts correctly entered in it in debug mode with eclipse, but when i look in debug view my value after typing 'aaa', i have the old value. And struts don't tell me anything about the conversion before entering in my validation method. – Mançaux Pierre-Alexandre Nov 17 '14 at 16:02
  • i can turn my question like this: how can i add a validator to check if the Double is in range of 0.0 to 1000.0, how can i do this with validator? ;) – Mançaux Pierre-Alexandre Nov 17 '14 at 16:09
  • Conversion is going before validation. You can check in debug mode errors happened during conversion. The action should contain those errors. – Roman C Nov 17 '14 at 18:03
  • Ok i try to check that and i give you a feedback thanks – Mançaux Pierre-Alexandre Nov 17 '14 at 20:47

1 Answers1

1

To validate an object inside a List, you need to use the Visitor Validator.

Then, to validate a double with min/max range, you need:

<field name="coefficient">
    <field-validator type="double">
        <param name="minInclusive">0.0</param>
        <param name="maxInclusive">1000.0</param>
        <message>
            The coefficient must be between ${minInclusive} and ${maxInclusive}
        </message>
    </field-validator>
</field>

Finally, I suggest you to read how the INPUT result works (for both Validation and Conversion errors)

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    thank you to give me a way, because i'm new in struts, my previous project use spring-mvc with JSF so its not same mechanic ;). And this project have many configuration problem and i can't change anything because its have too many impact :(. I see that interceptor-ref conversionError is in comment :(, when i add it, i have a message like i want but if i add this intercepetor it will change all the application and its not a good thing. I try to add interceptor directly to the action but i have 'Exception : Infinite recursion detected' so i try to find solution about this error ;) – Mançaux Pierre-Alexandre Nov 18 '14 at 09:32
  • No need for infinite recursion :) just use the `defaultStack`. If this is your first Struts2 project, I suggest you to start with small steps. Two actions, two JSP, simple form fields, simple validation, simple messages (success, errror or field). Then try adding Visitor. Have you read the INPUT result link ? P.S: feel free to upvote answers that helps you, if any – Andrea Ligios Nov 18 '14 at 09:37
  • yes i read the INPUT result link and i'm trying some stuff to understand interceptor and visitor validator ;). I tried to use defaultStack as interceptor into my action but all my fields are empty and a message is showed with all error message... i think this project sucks :D. to be continued... – Mançaux Pierre-Alexandre Nov 18 '14 at 09:47
  • Restart from scratch. Really. Maybe with a Maven archetype (if you already use Maven...) – Andrea Ligios Nov 18 '14 at 09:53
  • If i could...but you know its not always possible because its too expansive for the client;) and yes i use maven and its the only thing i could rewrite without any problem:) – Mançaux Pierre-Alexandre Nov 18 '14 at 22:13
  • Then post another question about the exceptions raised... When you'll fix that, you will see that this question is already answered ;) – Andrea Ligios Nov 18 '14 at 22:31