English 中文(简体)
How to validate a select tag in struts 2 framework
原标题:

I am a newbie into programming and i am currently employed as a junior programmer. I am currently having some problems validating the select tags in one of my forms. What i actually trying to do is to ensure that one item is selected before the user submits the form .

In the form i have;

<s:select list="assessmentTypes" headerKey="0" headerValue=" -- Select One --"
          listKey="id" value="name" listValue="name" key="course.assessmenttype"
          name="assessmenttype.id"/>

I have some knowledge of validation but not to sure how to do it for the select list.

I tried using a normal validation with strings but i don t think it is required in this case. for example;

<field name="course.assessmenttype">
    <field-validator type="requiredstring">
        <message>Please Select a value</message>
    </field-validator>
</field>

all help would be appreciated, Thanks in advance.

问题回答

One option is to use an int validator with a min value set. Since you want any value greater than 0.

     <field name="course.assessmenttype">
          <field-validator type="int">
              <param name="min">1</param>
              <message>Please Select a value to continue</message>
          </field-validator>
      </field>

Set headerKey="0" to headerKey=""

<field name="course.assessmenttype">
          <field-validator type="requiredstring">
              <param name="trim">true</param>
              <message>Please Select a value to continue</message>
          </field-validator>
</field>
<field name="course.assessmenttype">  
           <field-validator type="regex">  
       <param name="expression">assessmentTypes</param>  
       <message>Select a value to continue</message>  
     </field-validator>  
      </field> 
instead of assessmentTypes, you can directly mention the dropdown list values. Make sure that your validator XML file is in the format <ActionClassname>-validation.xml,which should be in the same package as Action class 




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签