GXT - Pissed off with ComobBox in Grid (different Display Label and Value) - gwt

I am trying to add a CobmoBox in EditorGrid
I have a class Vehicle with fields
Integer vehicleId;
String plateNo;
Integer vehicleType; //1=Car,2=Truck
I want the combo box to show vehicle's type in text form i.e if vehicleType is 1, "Car" would be displayed. And when the user select any other option - like "Truck" the corresponding integer value should be populated into the bean.
This is pretty standard stuff with plain old JSP and HTML.
However I couldn't find a simple way the do this in Ext GWT.

If you are using a GXT ComboBox, the easiest way to is create a model representing your vehicle object if you haven't already. This is basically a class that extends GXT's BaseModelData class.
Once you have your model, you create a combo box using that type:
ComboBox<VehicleModel> box = new ComboBox<VehicleModel>();
The last step is to tell the combo box which fields to use for values and for display, which is done with 2 method calls:
box.setDisplayField("field name for display");
box.setValueField("field name for value");
When you load up a store of vehicle models, GXT will take care of the rest. You will, however, need to convert the model back to the vehicle object itself to be persisted.

Related

Thymeleaf form with multiple objects of the same class

Simple problem but can't find a solution: I have a Thymeleaf form used to add a new object, say of a Book class. It works perfectly well and I only need that particular form for adding new objects, not editing the existing ones. The question is: how can I put several objects of the Book class in the same single form? So, purely for convenience, instead of filling form for a single book and clicking Send you can fill form for several books at once and only then click Send, have them all inserted into the database (in whatever order) and also have the option to fill the form partially (e.g. the form has room for 5 books but it will also accept 1, 2, 3 or 4 and you can leave the rest blank).
Edit: I've tried passing a list of object to the Thymeleaf template with the form bound to the whole list and iteration inside, but Thymeleaf throws BingingResultError upon rendering it.
You need to use a wrapper object to realize what you want.
Something like:
public class BooksCreationDto {
private List<Book> books;
// default and parameterized constructor
public void addBook(Book book) {
this.books.add(book);
}
// getter and setter
}
Then you need to pass this object as a model attribute in your controller:
BooksCreationDto booksForm = new BooksCreationDto();
model.addAttribute("form", booksForm);
bind fields using index property
th:field="*{books[__${itemStat.index}__].title}"
and get back the result with
#ModelAttribute BooksCreationDto form
in your controller.
For a complete and detailled explaination visit: https://www.baeldung.com/thymeleaf-list

Dynamically generated form in PyQt

Let's say I've the following class:
class Person(object):
def __init__(self, attributes):
# Attributes is just a dictionary (string -> value)
self.attributes = attributes
def set_attribute(self, attribute, value):
self.attributes[attribute] = value
# Some other code which may cause the value of other attributes to change,
# or other attributes to be added or deleted too
Now I'd like to create a PyQt dialog with a simple form, but which is dynamically generated (to account for the variable number of attributes). I did this by creating a QFormLayout in a QDialog and adding a row for every attribute the Person object has:
self.formLayout = QtWidgets.QFormLayout()
for attribute in self.person.attributes.keys():
label = QtWidgets.QLabel(self)
label.setText(attribute)
lineEdit = QtWidgets.QLineEdit(self)
lineEdit.textEdited['QString'].connect(self.onAttributeChanged)
# or use a checkbox instead of a line edit if the attribute is a boolean,
# combo box if it's an enum or similar, etc.
self.formLayout.addRow(label, lineEdit)
Where onAttributeChanged() calls self.person.set_attribute with the appropriate attribute and the new value inputted by the user.
Now that takes care of updating the model with the user's input values, but the problem I'm facing is how to update the view when set_attribute modifies the person's other attributes (since set_attribute may add or delete other attributes, or change the other attributes' names or values). So far, I've considered to following:
Rebuild the entire form whenever information changes. There's probably something better than this.
Use the Qt signal/slot model, like I did by connecting lineEdit to onAttributeChanged. From what I understand, this means using pyqtSignal to create signals in my Person class representing the attribute names, plus emitting the appropriate signals whenever the names are modified. However, I'd like to keep my Person class intact, and free from any Qt .emit() calls. (Not to say this still doesn't handle the case of attributes being added/removed.)
Ditch the form altogether and use a table view with a QStandardItemModel instead. Not ideal since I really wanted a form and not a table (for the sake of better usability).
Is there some elegant way of handling this kind of dynamic form in PyQt?

Configure symfony2 automatic form label generation

I'd like to know if there is a way to configure, disable or otherwise override the way Symfony handles automatically generating form labels for entity properties.
We also use the Sonata Admin Bundle and Symfony native and Sonata do not always produce the same label for complex form fields based on a given entity property, specifically when adding related entity properties e.g. something.somethingRelated. Sometimes it seems there is an extra space added to the label string (where the "dot" is) so we end up with two different labels being generated for the exact same property in two different forms.
Sonata has a configuration that allows some control over how the label is generated; e.g underscores, native, do nothing, etc.
We are trying to get our translations under control but still use the automatic generation of form labels rather than having to add a label (key) to each form field.
Right now I think my best alternative is to just turn it off in Symfony and let the actual property name be the "string" that is generated without any processing on it. So, thisIsTheProperty would have an automatically generated label string "thisIsTheProperty", not "This Is The Property" or whatever.

return multiple columns from stored procedure in MVC

Was previously using this line in my controler to return an id and text column from, a stored procedfure in asp.net MVC
user = new SelectList(ctx.Database.SqlQuery<LkUpGenderModel>("EXEC dbo.uspGetLkUpGender").ToList(), "GenderID", "Gender");
However i now want to return more text values, I have extended the model to have these extra fields but I'm getting an error.
Is there a way to get this working:
user = new SelectList(ctx.Database.SqlQuery<LkUpGenderModel>("EXEC dbo.uspGetLkUpGender").ToList(), "GenderID", "Gender", "GenderShort", "GenderCombined");
currently it flags the SelectList( saying the call is ambiguous
The previous code simply returns instances of LkUpGenderModel and then builds a SelectList from that using the GenderID property of that class as the value and the Gender property as the display text.
Your new code doesn't request more properties, it simply passes additional parameters to the SelectList constructor: namely, dataGroupField which you're setting to the GenderShort property, and selectedValue, which you're setting to the GenderCombined property. See: https://msdn.microsoft.com/en-us/library/dn725507(v=vs.118).aspx.
If you have additional columns being returned from the stored procedure that you want filled in on the class, then you should add additional properties to the class to handle those. However, since all you're doing with the data is creating a SelectList from it, you can't send any additional data to the view other than the value and display text, making returning additional data pointless in this instance.

How can I get the Zend_Form object via the Zend_Form_Element child

I've built a Zend_Form_Decorator_Input class which extends Zend_Form_Decorator_Abstract, so that I could customize my form inputs -- works great. I ran into a problem in the decorate class, in trying to get the form name of the element, so as to built a unique id for each field (in case there are multiple forms with identical field names).
There is no method like this: Zend_Form_Element::getForm(); It seems Zend_Form_Decorator_Abstract doesn't have this ability either. Any ideas?
I don't think changing the id from the decorator is the right approach. At the time the decorator is called the element already has been rendered. Thus changing the id would have no effect to the source code. Additionally, as you already have pointed out, the relation between a form and its elements is unidirectional, i.e. (to my best knowledge) there is no direct way to access the form from the element.
So far the bad news.
The good news is, that there actually is a pretty easy solution to your problem: The Zend_Form option elementsBelongTo. It prevents that the same ID is assigned to two form elements that have the same name but belong to different forms:
$form1 = new Zend_Form(array('elementsBelongTo' => 'form1'));
$form1->addElement('Text', 'text1');
$form2 = new Zend_Form(array('elementsBelongTo' => 'form2'));
$form2->addElement('Text', 'text1');
Although both forms have a text field named 'text1', they have different ids: 'form1-text1' and 'form2-text1'. However, there is a major drawback to this: This also changes the name elements in such a way that they are in the format formname[elementname]. Therefore $this->getRequest()->getParam('formname') will return an associative array containing the form elements.