How Struts2 validation framework works - java

I am using struts2 for my project.
Now i have a business information fill-up form for getting business details.
In form i have some basic items such as email, username , password , confirm password & Facebook twitter links(FB & tweeter links are optional but if user enters i want to check whether they are real)
for basic items i am using xml file validation as:
<validators>
<field name="companyname">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Company name is required.</message>
</field-validator>
</field>
<field name="fblink">
<field-validator type="url">
<message>Please enter a valid url</message>
</field-validator>
</field>
<field name="tweetlink">
<field-validator type="url">
<message>Please enter a valid url</message>
</field-validator>
</field>
</validators>
& if user enters values for Facebook or twitter url i am writing validation in Action class's Validate() method to check whether these are real(means HTTP_STATUS_CODE=200)
as :
public void validate()
{
if(getFblink()!=null && !getFblink().isEmpty())
{
if(URLValidation.validate(getFblink())== false)
{
addFieldError("fblink", getText("Enter valid facebook page link"));
}
}
if(getTweetlink()!=null && !getTweetlink().isEmpty())
{
if(URLValidation.validate(getTweetlink())== false)
{
addFieldError("tweetlink", getText("Enter valid tweeter link"));
}
}
}
Here URLValidation is my class & it contains static method validate which takes URL & using Httpclient libraries i am hitting & checking its status code.
Depending on its return value i.e boolean value i am setting error message.
Its working. But i want to know is my approach is correct?
Also i want to use shor-circcuit validation so how can i communicate in xml & validate() validation code??

You want to validate links and want to use your code (from validation xml). This will be possible by writing custom validators. Write a custom validator and use it through XML file for your fields.
For example you can see this tutorial for an idea.

Related

New Test Run using REST API - ALM 14

I am trying to update the test results using REST API for ALM Saas
http://targetserver:targetport/qcbin/rest/domains/ALMDomain/projects/ALMProject/runs - EndPoint
Below are the body(payload):
<Entity Type='run'>
<Fields>
<Field Name='name'><Value>testnamegoeshere</Value></Field>
<Field Name='test-instance'><Value>1</Value></Field>
<Field Name='cycle-id'><Value>cycleidgoeshere</Value></Field>
<Field Name='test-id'><Value>testidgoeshere</Value></Field>
<Field Name='subtype-id'><Value>hp.qc.run.MANUAL</Value></Field>
<Field Name='status'><Value>Failed</Value></Field>
<Field Name='owner'><Value>testownergoeshere</Value></Field>
</Fields>
</Entity>
Getting response is like this.
false
here is my question: I need to know if am passing correct parameter or not and please let me know, is API got changed or anything else??
URL you mentioned is for creating a new run. You have mentioned that you are going to update the test run. For that you need to append /{id} (id of run). Make sure you are making put call and not post for this operation.
Also probably you wont require to give cycle-id, test-id, test-instance details with update because those are now constants. If you are creating new run then it is mandatory.
Hope this helps you to solve.

How give submit on selecting drop down in moqui Framework?

I've implemented an application using Moqui Framework.I have form-single like this
<transition name="languagesUptade">
<service-call name="org.moqui.impl.UserServices.update#searchLanguage"
in-map="[locale:locale]"/>
<default-response url="."/></transition>
<form-single name="languageslist" map="languages" transition="languagesUptade">
<field name="locale" columnSize="col-sm-12"><default-field>
<drop-down allow-empty="false"><list-options list="localeStringList" key="${locale}" text="${locale} - ${name}"/></drop-down>
</default-field></field>
<field name="submitButton" columnSize="col-sm-12"><default-field title="Update"><submit/></default-field></field>
</form-single>
I am trying to remove field submit-button and i need get submitted on selecting drop down and it need to call the transition. How can i achieve that ?
Write some JavaScript to do it, and put it in a render-mode.text element. There is an example of this in the QuickSearch screen in HiveMind:
https://github.com/moqui/HiveMind/blob/master/screen/HiveMindRoot/dashboard/QuickSearch.xml

Struts validation error: "Got result of null when trying to get Boolean"

I have action class and do validation in XML. All validation is successful except this one:
<field name="dealBoardRequestDTO.contractType">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[((dealBoardRequestDTO.contractType!=null) and
(dealBoardRequestDTO.contractType.size()>0) and
(!dealBoardRequestDTO.contractType.contains(0))
)]]>
</param>
<message key="invalid.fieldvalue.dealBoardRequestDTO.contractType" />
</field-validator>
</field>
Got the following error in the console:
FieldExpressi W
com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn Got
result of null when trying to get Boolean.
In dealBoardRequestDTO class there is List<Integer> of contractType with getters and setters. If I do not use validation with the above code then all is ok and contractType is updated. Also in the action class there is a Map<Integer, String> for the field contractType.
Why is this error occurring?

Struts 2 OGNL - Comparing two string values in validation.xml

I am new to Struts2 and OGNL and am making a simple web application with a registration page. There are two fields, password and repassword (to re-enter the password) and using the validation framework i would like to validate that the two passwords match (I know that I can do it easily with JavaScript). Here is what I've got so far. All of the field-validators are working fine. This is my first non-field validator and I just cant get it to work.
<validator type="expression">
<param name="expression">${password}!=${repassword}</param>
<message>Passwords must match.</message>
</validator>
I tried both with
${password}!=${repassword}
and without
password!=repassword
the OGNL tags.
The expression validator is a Non-Field Level validator. Use fieldexpression validator which is a Field Level validator and validates using OGNL expression. And it must be equals (==) check.
<field name="password">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[password == repassword]]></param>
<message>Passwords must match.</message>
</field-validator>
</field>
The expression validator adds action errors. The fieldexpression validator adds field errors.
try
%{password == repassword}
The validator checks to boolean OGNL expression that both are equal.

how to validate dynamic map elements

i have a map and i want to validate in struts 2 validation framework by using expression validation how can i access the elements of the map dynamically?
if Map myMap; how can i validate the map with dynamic key? if mymap has static key like "Salary", i could validate like
<field
name="myMap['Salary']">
<field-validator
type="regex">
<param
name="expression">[0-9]+[.][0-9]+</param>
<message>${getText("errors.validation.number")}</message>
</field-validator>
</field>
thanks,
Helen
There are two ways you can use the myApp in declarative validation.
By using myMap.salary
By using myMap['salary']
You will need to use one of the above notations which is based on how you have your input form fields defined.
For example, if your input form looks something like below then you need to use the . operator as you are using the . operator while defining the name of input field.
<s:form action="sayHello">
<s:textfield name="myMap.salary" label="Salary">
<s:submit/>
</s:form>
If you use [] opertaor for defining the name of input field then use the [] operator to access the property in validator.
So, your validation code above is correct, you just need to define your input field at input form correctly with [] operator.
Hope this helps
Hibernate Validator Framework has a recursive validation feature.
And there is a plugin for Struts2 that uses this:
Full Hibernate Plugin: http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html
I don't think you can do this in the declarative validation. I'd suggest dropping down to the validation method for this, or doing the validation in javascript if you're using the javascript validation that is generated by the framework.

Categories

Resources