I am trying to do validation on a Map using struts 2.
When I click on the submit button, the error message will always appear even though I have inserted a value into the textbox.
Is there anyway for it to work?
Any help will be much appreciated.
The following is my file
In my new.jsp
<s:textfield name="myObject.myMap['name']" key="name" ></s:textfield>
In the validaton.xml
<validators>
<field name="myObject.myMap['name']">
<field-validator type="requiredstring">
<message key="errors.required"/>
</field-validator>
</field>
</validators>
Related
I want to use a custom datetime attribute to track, when an item entered a given state for the first time (serverdefault:clock).
To avoid later manipulation, this custom field at the same time should be set to readonly for everyone.
It seems however that in all combinations I come up with the "readonly" takes precedence and immediat es ly blocks the "serverdefault" that seems to be executed with the rights of the user iniziating tha transition.
For e.g. "created date" however the behavior seems possible for the system itself ...
Is there a way to achieve the same behavior for custom fields?
You can define your field like this:
<FIELD name="Custom Date" refname="My.CustomDate" type="DateTime">
<WHENNOTCHANGED field="System.State">
<READONLY />
</WHENNOTCHANGED>
</FIELD>
and then add this XML to the TRANSITION node for the given state:
<TRANSITION from="Resolved" to="Closed">
<FIELDS>
<FIELD refname="My.CustomDate">
<SERVERDEFAULT from="clock" />
</FIELD>
</FIELDS>
</TRANSITION>
Is it possible to assign DEFAULT rule for DateTime Field while adding a Work Item in TFS? But not using CLOCK value. Giving some Default Date.
Yes, you can do this by hardcoding the value in the Work Item Type Definition. In the appropriate TRANSITION element under the FIELDS\FIELD element for that field, instead of using ServerDefault and clock which you have probably seen like this:
<FIELD refname="Microsoft.VSTS.Common.ActivatedDate">
<SERVERDEFAULT from="clock" />
</FIELD>
try doing this:
<FIELD refname="Microsoft.VSTS.Common.ActivatedDate">
<COPY from"value" value="01-Feb-2014" />
</FIELD>
It is possible, try this:
<FIELD name="Custom Date" refname="Custom.Date" type="DateTime">
<DEFAULT from="value" value="2100-01-01" />
</FIELD>
The date that will be displayed is a day before of whatever date you're setting.
Is possible to make this conditional expression validation?:
(invitation.id==null and (newText==null or newText.isEmpty()))
I've tried several ways, several times, but don't achieve it.
This version is working, but on server-side, and ignore if invitation.id is null or empty... any ideas???:
<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">!(invitation.id eq null and (newText eq null or newText.empty))</param>
<message>${getText("validation.required")}</message>
</field-validator>
</field>
http://struts.apache.org/release/2.2.x/docs/fieldexpression-validator.html
what is wrong with the expression?? Thanks!
Your problem is that you are trying to validate another field (invitation.id) into the newText Field Validator (i don't think it's possible, but I'm not sure).
However, you could split it into two validators, raising the message corrispondent to the failure case, that is more correct imho;
<field name="invitation.id">
<field-validator type="required">
<message>${getText("validation.invitation.id.required")}</message>
</field-validator>
</field>
<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[
newText != null && !newText.trim().empty())
]]>
</param>
<message>${getText("validation.newText.required")}</message>
</field-validator>
</field>
, if you need to trim it, otherwise it could become simply
<field name="invitation.id">
<field-validator type="required">
<message>${getText("validation.invitation.id.required")}</message>
</field-validator>
</field>
<field name="newText">
<field-validator type="requiredString">
<message>${getText("validation.newText.required")}</message>
</field-validator>
</field>
Note that required is for every non-text fields, while requiredString is for text fields only.
Expression Validator is very powerful, but it should be used for more complex purposes:
for example, if you want to validate a Date against another one dynamically read (through a Getter) from your Action; lets say you've previously chosen a User, and you need to validate a date from the page against the user Start and End Validity interval; but you want to pass the validation too if the date is not inserted, because it is already handled by the required validator (so you won't raise two messages):
<field name="inputDate">
<field-validator type="required">
<message><![CDATA[ Input Date is mandatory ]]></message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[
inputDate==null ||
(inputDate >= chosenUser.startValidity
&&
inputDate <= chosenUser.EndValidity
)
]]>
</param>
<message>
<![CDATA[Input Date must be included in the User Validity interval
(from ${chosenUser.startValidity} to ${chosenUser.endValidity} )
]]>
</message>
</field-validator>
</field>
where chosenUser is an User object from your Action (public User getChosenUser())
and startValidity and endValidity are properties of the User object (public Date getEndValidity()).
And as you can see, the dynamic read can be performed in messages too... this is how powerful expression validator is ;)
First of all expression is not a valid parameter. If you really want to validate on client side, try to use java-script or jquery for validation. But you have to validate inputs on server - side also because sometimes user could disable the javascript .So in that case you can use struts2 - validation.
for detailed explanation refer http://viralpatel.net/blogs/struts2-validation-framework-tutorial-example/
Ok, finally I used client-side validation width jquery emulating struts2.
I don't like this for compatibility reasons, please if there is a way to do it in a standard way, I'll apreciate any help.
Removing struts validation:
<!--<field name="newText">
<field-validator type="fieldexpression">
<param name="expression">!((invitation.id==null or invitation.id.empty) and (newText==null or newText.empty))</param>
<message>${getText("validation.required")}</message>
</field-validator>
</field>-->
Adding js/jquery code:
function mySubmit() {
if ((invitationId==null || invitationId<0) && $("#text").val().trim()=='') {
var trStrutsFieldError='<tr errorfor="text">'+
'<td colspan="2" align="center" valign="top"><span class="errorMessage">'+strValidationRequired+'</span></td>'+
'</tr>';
$(trStrutsFieldError).insertBefore($("#text").parent().parent());
return false;
}
return true;
}
$(document).ready(function(){
$("form").submit(function(event) {
$.each($("form").find("span.errorMessage"), function() { /* <tr errorfor="title"><td colspan="2" align="center"><span classname="errorMessage" class="errorMessage">Campo requerido */
return false;
});
if ($(mySubmit).exists()) {
if (!mySubmit())
return false;
}
return true;
});
});
I have lots of number field let say num1, num2 ...
Now I am looking for way I can apply a set of validation on all field rather than writing same validations multiple times.
Here is set of two validation for requiredString and Number check
<validators>
<field name="num1">
<field-validator type="requiredstring">
<message>Field cannot be left blank</message>
</field-validator>
<field-validator type="regex" short-circuit="true">
<param name="expression"><![CDATA[^[0-9]+$]]></param>
<message>Not a valid Number</message>
</field-validator>
</field>
</validators>
Tia
i trying to give validation to the dojo datetimepicker using struts 2 validation xml but no success if someone has gone through the same scenario do help me out
<s:label value="Joining Date"/>
<sx:datetimepicker name="joiningDate" displayFormat="dd/mm/yyyy"/>
and in the validation xml i write
<field name="joiningDate">
<field-validator type="required">
<message>Select Date.</message>
</field-validator>
</field>
problem is when i click submit the page submit even if the joinindate field is empty means validation field in not applied
Just add required Attribute to the tag.
i,e
<sx:datetimepicker name="joiningDate" displayFormat="dd/mm/yyyy" required="true"/>
it should work...!
Cheers
Karthikeyan
You can also use:
<field-validator type="date">
<param name="min">01/01/1990</param>
<param name="max">01/11/2012</param>
<message>Birthday must be within ${min} and ${max}</message>
</field-validator>