1

In every tutorial I read that covers the struts2 validation or workflow interceptor, the examples given are very basic and assume every form field has a fixed name and is predefined in the .jsp file. E.g.:

<s:form method="post" validate="true">
      <s:textfield label="Name" name="name"/>
      <s:textfield label="Age" name="age"/>
      <s:textfield label="Favorite color" name="answer"/>
      <s:submit cssClass="btn btn-primary"/>
</s:form>

However, I currently have a form that is dynamically created by using javascript. When the page loads, ajax is used to fetch a list of countries (and metadata of those countries) from a database. A dropdown box is populated with the names of these countries. For each country, some input fields will be generated and added to the DOM, but these remain hidden until a country is selected. These inputs have id's such as:

france_extra_data_1
france_extra_data_2
france_extra_data_3

germany_extra_data_1
germany_extra_data_2

america_extra_data_1
america_extra_data_2
america_extra_data_3
america_extra_data_4

... and so on

When the user select a country, for example america, the inputs with id that have america in their name will be shown. the others remain hidden. The country america will NOT always have exactly 4 inputs associated with it. The reason why all inputs are generated on the spot and not just when the appropiate country is selected is because all inputs have a placeholder text and I do not want fetch this data from the database EVERY time, but just once when the page loads.

The problem is when I want to validate this form with struts2. Validators require knowledge of the name of the field, but I do not know beforehand how many inputs there will be (depends on the database) and I also do not which fields should be validated and which should be ignored (in the example above, only inputs with id's america).

Can the struts2 framework solve this problem?

I already read this question: dynamic field name for field validator in struts2, but I do not need a visitor field validator!

Community
  • 1
  • 1
user1884155
  • 3,616
  • 4
  • 55
  • 108
  • HOW do you know which validator should be applied (eg. the numeric control) if the fields are created randomly ? You know the names, but not the number, and that is ok... just use a List, and Visitor Validator. But which is the criterion to decide which kind of validation rules are to be applied to each one ? – Andrea Ligios Oct 18 '13 at 16:37
  • I don't really know the name either. I create like 20 input fields, but depending on what the user enters previously, only a subset of them are visible to the user (for example number 5, 6, 7 and 8) and need to be validated. Somehow, I need to tell the validator that only certain inputs need to be checked, and others need to be ignored... – user1884155 Oct 21 '13 at 06:39
  • The only thing clear is that the user can arbitrarily create a variable number of fields. Does he specify the type of the field while creating it (numeric, date, text,..) ? Please, add more details by editing your question. The relevant, minimal part of code interested and the detailed use-case. – Andrea Ligios Oct 21 '13 at 07:43
  • I edited the question to reflect what's actually going on – user1884155 Oct 21 '13 at 08:34
  • You need to submit only fields that you want to be submitted/validated. – Aleksandr M Oct 21 '13 at 09:02
  • Ok so I can remove the unnecessary parts of my form (for example the inputs with france and germany when only america is needed), but how can I write a validator if I do not know beforehand how many inputs there will be? I think I cannot use a list/visitor validator, because the text that the users will fill in are not part of any bean or something. – user1884155 Oct 21 '13 at 09:20
  • You can create your own validator or put this properties to some bean. – Aleksandr M Oct 21 '13 at 09:47

0 Answers0