vaadin form cannot hide null fields - java

I have some vaadin forms which are reloaded after an event. The event causes some of the fields to become null. This means the form displays the label for the field and "null". I would like both the label and "null" value to disappear after the event is loaded. I would also like to do this without loading a different form to replace the form with the null values.
I have the following constructor for the form:
public IInfoForm(Info info) {
List<Object> orderedProperties =Arrays.asList(InfoContainer.DISPLAYED_FIELDS);
setItemDataSource(new BeanItem(idInfo), orderedProperties);
The info container retrieves the data from a webservice and DISPLAYED_FIELDS lists the fields to display.
I have tried looking for properties to set on the form fields but to no avail.

You can set for any field a special value that stands for the null value. Example:
TextField textfield = new TextField();
textfield.setNullRepresentation("abc");
So instead of null "abc" is represented in the text field.
Create the fields by yourself and add them or add a FieldFactory to the form.
Form form = new Form();
form.setFormFieldFactory(new FormFieldFactory() {
public Field createField(Item item, Object propertyId,
Component uiContext) {
Field field = DefaultFieldFactory.get().createField(item,
propertyId, uiContext);
if (field instanceof TextField) {
((TextField) field).setNullRepresentation("abc");
}
return field;
}
});

I would try to do so that I remove the fields with null value from the orderedProperties list and call
setItemDataSource(new BeanItem(idInfo), orderedProperties);
with that list.

Related

SmartGWT: Programmatically set edit value in list grid to null for numeric field

I need to set edit value in list grid from existing value to null in numeric field. Edits come from external component and must be reflected on the grid. Filed is not required so it can have null value.
I was trying the following:
1) throws an exception
Integer nullValue = null;
listGrid.setEditValue(rowNum, fieldName, nullValue);
2) look like this is working the same as clearEditValue(rowNum, "fieldName")
HashMap map = new HashMap<>();
map.put("fieldName", null);
listGrid.setEditValues(rowNum, map);
I'm using SmartGWT 6.0p
I'm able to set a cell of a Integer field to null in a ListGrid with the following code
public void setCellValue(int rowNum, String nameOfField, ListGrid listgrid) {
ListGridRecord row = listgrid.getRecord(rowNum);
Integer intNull = null;
row.setAttribute(nameOfField, intNull);
listgrid.updateData(row);
}
After setCellValue was called, the corresponding cell correctly changed to blank.
Found solution. Regardless of filed type I can cast null to String
In this case SmartGWT is not throwing any exception and edit value is set to null
listGrid.setEditValue(rowNum, fieldName (String)null);

Jackson include null set by user?

I am serializing a POJO using jackosn, and I want that all the values for which the user sets some value irrespective whether it's null or not must be included in the serialization.
So currently:
POJO:
public class YourItem {
public String key;
public String item;
}
Currently when user does:
YourItem item = new YourItem();
item.setKey("abc");
The serialization gives:
{
"key" : "abc"
}
as I configured ObjectMapper as objectMapper.setInclude(Include.NON_NULL)
However now if the user specifically calls the setter and sets the value as null, then I want that item in my serialized string.
So if user does
YourItem item = new YourItem();
item.setKey("abc");
item.setItem(null);
I want in serialzation both key and item values are present like:
{
"key" : "abc",
"item" : null
}
How do I differentiate between the user set null and the default null.
Is there a configuration in ObjectMapper ??
Some people consider using null to be bad practice (The book Clean Code, for instance)
Disregarding that, you cannot differentiate between the default initialization null and a user-set null by language-design
You need some sort of state that tracks if a field has been accessed by a setter. If it hasn't been accessed by a setter and is null, ignore it.
One way to do this is Jackson Filters, which allows you to define various conditions for serializing a field during runtime (your condition being that your setter-access-state indicates that the field was set by a user)
http://www.baeldung.com/jackson-serialize-field-custom-criteria

Accessing text in the GWT TextBox from ChangeEvent

I'm dynamically generating a form based on data received from an RPC call into a FormFieldData object which has details about the field to be rendered such as, Field Name, expected length and type of input, if the field is a required field or not and valid input Regex in some cases etc.
I'd like to be able to perform validation on the field depending on above attributes.
Here's an example:
private void renderTextField(FormFieldData field){
FormGroup formGroup = new FormGroup();
FormLabel formLabel = new FormLabel();
if(field.isRequired()){
formLabel.setText(field.getName()+"*");
}else{
formLabel.setText(field.getName());
}
formGroup.add(formLabel);
TextBox textBox = new TextBox();
textBox.addChangeHandler(new ChangeHandler(){
#Overrride
public void onChange(ChangeEvent event){
//TODO - find a way to get the text entered in TextBox
// and perform validation on it
//and set the TextBox Style to "Validation-error"
}
});
formGroup.add(textBox);
form.add(formGroup);
}
There're similar methods to render dropdowns, Numeric fields, radio button fields etc. which would need similar validation.
The problem is I can't access the text from the TextBox inside the onChange method without declaring it final, which I can't do because I might be rendering multiple text fields. I don't know much about ChangeEvent and if there's a way to get the text from the that.
I'd really appreciate any pointers to a way to do this in real time as the data is entered into the form, other than having to iterate through the fields and their corresponding FormFieldData object when the form is submitted.
First off, you can make the variable final, no problem.
If you don't want to do that for whatever reason, you can get the TextBox from the event like this:
textBox.addValueChangeHandler(new ValueChangeHandler(){
#Overrride
public void onValueChange(ChangeEvent event){
TextBox box = (TextBox) event.getSource();
// Do whatever you need to here
}
});
You are probably also looking for ValueChangeHandler instead of ChangeHandler.

Can I submit a field value as an array with extjs?

On my form I have a textarea which calls for a list of words. e.g. word1,word2,word3 etc. The user can put as many words as they wish. My application gets this entry back as a single string, "word1,word2,word3" and to convert it to an array I have to do myString.split(",").
I was wondering if it is possible to set up the form so that extjs knows it should convert this to an array when it submits the data? Something like:
var myField = {
xtype : 'textarea',
fieldLabel : 'Words',
name : 'words',
type: 'array'
}
edit: I'd also be happy with having some kind of onSubmit function that sets the value of the field to an array client side before it is sent
Assuming you use extjs 4 and higher I would suggest the following:
Override Ext.form.field.TextArea field and implement it's getModelData( ) function to be something like this:
getModelData: function() {
var me = this,
data = null;
if (!me.disabled && !me.isFileUpload()) {
data = {};
data[me.getName()] = me.getValue().split(",");
}
return data;
}
This will allow Ext to properly interpret your field's model value as a string array. Then the model of your form will contain proper array for your field.
You may call getFieldValues() for your basic form to return corresponding json to send it to server, or you may use Ext MVC functions to work with form's model.

Casting result.event into an ArrayCollection of Person (Class) in Flex

Im kinda new to the Flex environment and I was wondering how to do this scenario:
My initial code goes like this:
public function displayAllNames(event:ResultEvent):void {
var result:ArrayCollection = new ArrayCollection();
result = event.result as ArrayCollection;
if (result.length != 0){
listBox.dataProvider = result;
}
}
event.result is an ArrayCollection of Person class that has the name attribute
listBox is the id of the List Component in Flex where the names are going to be displayed
When I tried to run the code, the listbox did show something. But instead of the names, it displayed object Object all throughout. It seems like I still have to do something with event.result first so that it would be an ArrayCollection of Person class.
You need to set the labelField of your ComboBox, try this
Assuming Person has a visible name property,
public class Person(){
public var name:String = "User 1";
}
You can tell your ListBox to use that property to populate its label field by using the labelField property
if (result.length != 0){
listBox.labelField = "name";
listBox.dataProvider = result;
}
By default I believe is set to "label", if that property is not found it will use Object.toString() that's where [Object object] is coming from.
due to this its also possible to override, or simply include, the toString method in your data provider class and not change the list's labelField
overide pubic function toString():String{
return "this will become your label field";
}

Categories

Resources