JSF - Bean level form validation for listbox - forms

I have a form designed in JSF. I need to check if a listbox item is selected. I am doing bean-level validation. My code is:
<webuijsf:listbox style="margin-left:10px;" binding="#user$webreports$frequentvisitorscategories.listBox}"
id="listbox" items="#frequentvisitorscategories.listboxDefaultOptions.options}"
selected="#{user$webreports$frequentvisitorscategories.selectedItemCategory}" rows="10" styleClass="listbox" multiple="true" width="190" required="true" validatorExpression="#{frequentvisitorscategories.category_validate}" />
----------------------------------------------------------------------
public void categories_validate(FacesContext context, UIComponent component, Object value)
{ String selectedValue = (String) value;
if(selectedValue.equals(null))
{
((UIInput)component).setValid(false);
FacesMessage message = new FacesMessage("Please select a category");
context.addMessage(component.getClientId(context), message);
}
}
When I don't select something it does not show the validation message. Does anyone have any opinion about this?
Thanks in advance

Related

Blazor event after #bind

In a Blazor page I have a collection of checkbox input elements bound to a boolean property with code
foreach(var item in items)
{
<input type="checkbox" class="checkbox" #bind="#item.IsReady" #onclick="((e)=>ItemIsReady(item))" />
}
I'm looking for a way to trigger the function ItemIsReady after the binding has completed. Currently the function triggers before the binding has completed.
Some form of #bind directive is necessary because the page loads the items, and ready items need to be appear checked after loading.
I can't use a function in the property setter.
Anyone know how to do this?
"I'm looking for a way to trigger the function ItemIsReady after the binding has completed. Currently the function triggers before the binding has completed."
I'm assuming you want to react to changes make to the checkboxes. Maybe I'm wrong.
If you want to catch update events then you either:
Use bind and catch the events on the property setter or
Wire up the input manually.
The demo page below shows how you can do that:
#page "/Test"
#foreach (var item in model)
{
<div><input type="checkbox" class="checkbox" checked="#item.IsReady" #onchange="((e)=>ItemIsReady(e, item))" /> <span>#item.IsReady</span></div>
}
#code {
List<ModelClass> model { get; set; } = new List<ModelClass> {
new ModelClass(),
new ModelClass() { IsReady=true },
new ModelClass()
};
class ModelClass
{
public bool IsReady { get; set; }
}
void ItemIsReady(ChangeEventArgs e, ModelClass item)
{
item.IsReady = (bool)e.Value;
}
}

checkboxlist with prechecked in struts2

I am trying to display checkboxlist with prechecked items.
The values are saved in a list in my database and for "editing" the user should be able to select new options as well as "uncheck" some of the earlier selected one.
Thats why I need to translate my List back to the checkboxlist...
Any idea how that could possibly work?
Thanks a lot!
I'll give you an example of how you can do it.
[1]edit.jsp page:
It is the page where you want to display your checked checkboxlist:
<s:checkboxlist name="type" list="typeList" />
here "type" is name of checkbox and "typeList" is a list which is loaded from actionclass in my case.
[2]loadEditData method in action class:
public class your_action_class_name extends ActionSupport {
private List<String> type;
private List<String> typeList;
public List<String> getType() {
return type;}
public void setType(List<String> type) {
this.type = type;}
public List<String> getTypeList() {
return typeList;
}
public void setTypeList(List<String> typeList) {
this.typeList = typeList;
}
public String loadEditData(){
tpyeList=\\add whole checkboxlist here;
type.add("value that you want to prechecked");
return SUCCESS;
}
}
[3]struts.xml:
<action name="edit" method="loadEditData" class="your_action_class_Name" >
<result name="success">/edit.jsp</result>
</action>
Now your flow is like follow:
1st call the edit Action that will implement loadEditData method and on returning success display edit.jsp page with checkboxlist which have prechecked value.
Is this answer helpful?

Search form that redirects to results page

Hello i have the following problem.
I have a search page lets call it search.xhtml and you can search for a bar-code. This value is unique so the result is always one or zero objects from the database
<p:panelGrid columns="1" style="margin:20px;">
<h:form>
<p:messages id="messages" globalOnly="true" showDetail="false" />
<p:message for="barcode" />
<p:inputText id="barcode" value="#{searchForm.barCode}"
required="true" requiredMessage="Value needed" />
<p:commandButton value="search"
action="#{searchForm.searchBarcode}" id="search"/>
</h:form>
</p:panelGrid>
This is the backingbean:
#ManagedBean
#ViewScoped
public class SearchForm extends BasePage {
private Long barCode;
#ManagedProperty("#{daoManager}")
public DaoManager daoManager;
public void setDaoManager(DaoManager daoManager) {
this.daoManager = daoManager;
}
public Long getBarCode() {
return barCode;
}
public void setBarCode(Long barCode) {
this.barCode = barCode;
}
public String searchBarcode() {
//request to dao to get the object
DataList<Data> data = daoManager.findbybarcode(barCode);
if (data.size() == 0) {
this.addMessage(FacesMessage.SEVERITY_ERROR,
"Not Found: " + barCode);
return null;
} else {
getFacesContext().getExternalContext().
getRequestMap().put("id", data.getId());
return "details";
}
}
So if i go to my details page which expect the parameter id this isnt send to the detail page.
backing bean details page:
#ManagedBean
#ViewScoped
public class DetailBean extends BasePage implements Serializable {
#PostConstruct
public void init() {
if (id != null) {
//Go on with the stuff
} else {
addMessage(FacesMessage.SEVERITY_ERROR,"Object not found");
}
}
}
What am i doing wrong? And is this wrong use of JSF? I know i can generate a list and the click on the result but thats not what i want. Also i can take the barcode from the first bean and pass it as a parameter but i want the details page only to accept the id from the objects. So is my thinking wrong? Or is there a solution to get it like this?
If I understand correctly, you wish to pass the ID of the barcode to the details page and yes this is possible.
getFacesContext().getExternalContext().getRequestMap().put("id", data.getId());
The following line is putting the ID parameter into the request that the client just sent you, but the navigation action to details will result in a different request. Try this instead:
return "details?faces-redirect=true&id=" + data.getId();
This will return an HTTP GET navigation action with the ID of the barcode passed as a request parameter in the request.

Processing a ListView inside a form

Please consider the following signup:
<form wicket:id="form">
<div wicket:id="fooList">
<input wicket:id="fooList.quxField" type="text" size="10"/>
</div>
<button wicket:id="submit"><wicket:message key="submitText"/></button>
</form>
And these two classes (I am assuming setters, getters etc.)
class FooClazz {
String quxField;
}
class BarClazz {
List<FooClazz> fooList;
}
And this form (models are wrapped in CompoundPropertyModel):
class BarForm extends Form<BarClazz> {
public BarForm(String id,final IModel<BarClazz> model){
super(id,model);
add(new ListView<FooClazz>("fooList"){
#Override
protected void populateItem(final ListItem<FooClazz> item){
item.add(new TextField<String>("fooList.quxField"));
}
}
}
}
Now the above code is generating a runtime exception for me:
2011-12-11 16:33:46 ERROR [org.apache.wicket.DefaultExceptionMapper] Unexpected error occurred org.apache.wicket.WicketRuntimeException: The expression 'quxField' is neither an index nor is it a method or field for the list class java.util.ArrayList
I can change the TextField to include a Model like this:
item.add(new TextField<String>("fooList.quxField", new Model<String>(model.getObject().getFooList().getQuxField())));
This resolves the error, but when I submit the form (with an Ajaxbutton) I never get to see the values entered into the form fields.
So how can I keep the TextField models connected to my form model? Am I overlooking the obvious?
(This is of course just a very shortened version of my original code ...)
Need to add: all my models are wrapped in CompoundPropertyModels.
Thanks in advance for any tips how to fix this.
I found it. I need to include a model for the TextField that has implementations for both getObject() and of course setObject(). So I really was missing the obvious.
#Override
protected void populateItem(final ListItem<Taste> item) {
final TextField<String> quxField = new TextField<String>("tastes.quxField", new Model<String>() {
#Override
public String getObject() {
return item.getModel().getObject().getquxField();
}
#Override
public void setObject(final String value) {
item.getModel().getObject().setquxField(value);
}
});
item.add(quxField);
}

gwt/extjs - form not submitting

i have form tag received from designer.
i need to submit the form from GWT given below.
FormPanel form = null;
Button submit = null;
function onModuleLoad(){
form = FormPanel.wrap(DOM.getElementById("MyForm"));
form.setEncoding(FormPanel.ENCODING_MULTIPART);
submit = Button.wrap(DOM.getElementById("OK"));
submit.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
// button clicked confirmed
form.submit();
}
});
formSubmitHandler = form.addSubmitHandler(new SubmitHandler(){
public void onSubmit(SubmitEvent event) {
}
});
}
but form was not submitted.
designer wrote the following lines.
form action="./a.cgi" method="post" name="MyForm" id="MyForm"
input type="button" value="OK"
form
Are you sure that the form's submit button has an id of "OK"? It looks like the button's value is "OK" but I don't see anything with "OK" as an id.
It seems weird though, since DOM.getElementById("OK") will return null if no element is found, and I'm not sure what happens when wrap() gets passed a null.
Check if getElementById("OK") is returning null.