Allow empty field and validate if not empty in Vaadin - java

I have an input field in my form that when it is empty and form is submitted, I want to allow the form to submit. And the field is not empty, I want it to be validated. This is the code I have so far, but the field gets a validation error when I submit the form and field is empty:
binder.forField(field)
.withValidator(field-> field.matches(REGEX),
FORMAT_ERROR_MSG)
.bind("field");

The straightforward way is to make your validator accept the value if it's empty or if it matches your regular expression.
binder.forField(field)
.withValidator(value -> value.isEmpty() || value.matches(REGEX),
FORMAT_ERROR_MSG)
.bind("field");

Related

Conditional Required field in Oracle ADF 12c

I have a form that requires multiple user inputs. One of them is a drop down list from which the User has to choose. Now I have to set the next input field as required depending on the options chosen in the first field.
For ex: There are 4 input options to be chosen for the first input field - A,B,C,D
I want the next input field to become mandatory when the user chooses option A and D.
How do I build the expression for the required field in this case?
I have tried doing this but that doesn't work
https://stackoverflow.com/a/48089828/15161963
1). Assume that, the name of first field() is 'FirstField' and next input field id 'SecondField' .
2). Then on 'SecondField'component's Required property , write EL as below .
Required = #{bindings.FirstField.attributeValue eq 'A' || bindings.FirstField.attributeValue eq 'B'}
3). And ensure that , 'SecondField's partialTrigger property points to 'FirstField' component .
I think you can binding your dopdownlist to a bean.
binding=#{yourBean.firstDropdowList}
Create method isDisableInputField
Boolean isRequireYourInputField(){
Boolean dropdownValue=firstDropdowList.getValue();
if("A".equals(dropdownValue) ||"D".equals(dropdownValue)) {
return true:
}
return false
}
In your af dropdown tag. Set
require=#{yourBean.isRequireYourInputField()}

Set a Default Value for a dropdown in ADF

In a jsff file a component has been designed and the values are being fetched from a Static VO. There are only two values in the static Vo. I need the first option to be set as default. But there is a empty value that is being set. I have written a condition to disable it. And when it is disabled the value must be set to the first one.
I have tried the List UI Hints and tried to enable the Include No selection item: Labeled Item first of list, I tried creating a new vo which only has one value and rendered it in Jsff(But it will make the code more complex for the future development) I have tried creating the switcher. But none of them worked as they should.
Can anyone suggest me a alternative where the code complexity does not increase and by default there is a first value selected. And disable if there is condition for disable tag in jsff is true.
PS: Once the field is disabled the first value must be the default value to be set by default.
There don't seem to be any default way to do this with Oracle ADF without adding code to your view Object. Here's how to automatically select First value in your ADF LOV (detail from here https://cedricleruth.com/autoselect-first-value-in-adf-lov-inside-a-table/) :
Generate the RowImpl Java class of your View Object and Static VO
In this RowImpl.java add the following function to return the first value if no value is already selected
public String defaultNextStatus() {
String value = (String) getAttributeInternal(AttributesEnum.NextStatus.index());
if (value == null) {
XxcrWorkflowUvVORowImpl nextStatut = (XxcrWorkflowUvVORowImpl) getWfkVA().first();
//Wkfva is the VO of the LOV
if (nextStatut != null) {
return nextStatut.getTxtValeur();
}
}
return value;
}
In the Detail panel of the attribute add the following Default Value Expression: adf.object.defaultNextStatus()
In the Detail panel of the attribute set the refresh Expression Value to false to avoid picking the first value again in case of ppr/refresh

How to handle `null` strings coming from a RESTful API in JSONObject?

Here's a service return, that gives us user's profile info:
{
email: 'someone#example.com',
pictureUrl: 'http://example.com/profile-pictures/somebody.png',
phone: null,
name: null
}
Now we get this JSON in our android app, and turn it into JSONObject model:
JSONObject profileInfo = new JSONObject(profileInfoJson);
And we bind UI views to data:
email.setText(profileInfo.getString("email"));
phone.setText(profileInfo.getString("phone"));
name.setText(profileInfo.getString("name"));
Then in our TextView or EditView we have null string, instead of having nothing.
It's possible that we check null values using if-then statements, but that's too much for a real-world application with so many fields.
Is there a way to configure JSONObject to gracefully handle null strings?
Update: I used optString with a fallback, as suggested, but it has no effect:
firstName.setText(profileInfo.optString("firstName", ""));
And the result is the same EditText has null in it.
Use optString, if no suitable value is found then the second parameter will be returned instead of exception or null
phone.setText(profileInfo.optString("phone","nophone"));
name.setText(profileInfo.optString("name","noname"));
Returns the value mapped by name if it exists, coercing(try to cast) it if
necessary, or fallback(return second parameter)if no such mapping exists.

Using trim() but still didn't get expected output

Ok,i am developing spring MVC based web application, application shows data is list, and i also facilitate filter options to enhanced search functionality, I also remove extra space by using trim(), but what happening now, when user input data in text field and enter the corresponding result will be displayed into the list, but if space added after input, the result will be "NOT FOUND" even i handle the space in javascript too
Java Code which fetches data from database
if (searchParamDTO.getRegNO().trim() != null && !searchParamDTO.getRegNO().trim().equals("") && !searchParamDTO.getRegNO().trim().equals("null")) {
query += " AND UR.REG_UNIQUE_ID = :REG_UNIQUE_ID ";
param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO());
}
JavaScript Code: fetches the value in behalf of id
function setSearchParameters() {
regNo = $('#regNo').val().trim();}
i also attached two screenshot with spaces and without spaces
Without space
With space
As #Greg H said you're trimming the string when checking if it's blank, but then adding the raw string to the query which will include any trailing spaces.
Then, this line param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO()); should be replaced by param.addValue("REG_UNIQUE_ID", searchParamDTO.getRegNO().trim());

Can JsonPath be used to validate multiple paths?

I need to verify that JSON contains values in two fields.
Can I verify that some JSON contains two values, for example ...
$.field1.field2 = test && $.field3[*].field4 = test2
...using JsonPath?
I can successfully validate one field but I don't know how to validate multiple fields
You can use a JsonPath Filter for each condition and you can combine filters with and.
For example:
Filter combinedFilter = Filter.filter(
Criteria.where("$.field1.field2").is("test").and("$.field3[*].field4").is("test2")
);
JsonPath.parse(json).read("$", combinedFilter);
More details in the docs.

Categories