How to pass selected value from selectOneRadio tag to fileUpload tag in PrimeFaces - file-upload

I'm using PrimeFaces 6.0. I used selectOneRadio tag and populates the list from database, and then I would like to use the selected value to identify the type of document selected and pass this to fileUploadListener so that the right document type is attached to file being uploaded. How do I pass that selected value?
<p:selectOneRadio id="docType" value="#{fileUploadBean.docType}" layout="grid" columns="1">
<f:selectItems var="document" value="#{fileUploadBean.docDescriptionList}" itemLabel="#{docType}" itemValue="#{docType}" />
</p:selectOneRadio>
<p:fileUpload id="docFile" value="#{fileUploadBean.file}" mode="advanced" allowTypes="/(\.|\/)(pdf)$/"
fileUploadListener="#{fileUploadBean.uploadFile}" multiple="true" update="messages">
</p:fileUpload>
<p:growl id="messages" showDetail="true" />

You'll need to add variable for allowed types and then update it when radio button is changed:
<p:selectOneRadio id="docType"
value="#{fileUploadBean.docType}"
layout="grid"
columns="1">
<p:ajax listener="#{fileUploadBean.updateAllowTypes}" update="#form" />
<f:selectItems var="document"
value="#{fileUpoadBean.docDescriptionList}"
itemLabel="#{docType}"
itemValue="#{docType}" />
</p:selectOneRadio>
<p:fileUpload id="docFile"
value="#{fileUploadBean.file}"
mode="advanced"
allowTypes="#{fileUploadBean.allowTypes}"
fileUploadListener="#{fileUploadBean.uploadFile}"
multiple="true"
update="messages">
</p:fileUpload>
And in Java bean:
private String allowTypes; //getters and setters
...
public void updateAllowTypes(){
allowTypes = ... //Specify allowed types
}

Related

Blazor checkbox binding is not working - server-side

TLDR;
It is like the string/textbox binding works just fine on input controls, but the checkbox binding backed by Boolean properties does not work. I know the binding for checkbox values needs to used a 'checked' attribute instead of a 'value' attribute, but Blazor is supposed to handle that across different control types.
I'm doing some Blazor work (server-side app) with RC1 and cannot seem to get Boolean values binding to an input checkbox control. I believe that the syntax being used is correct (see below). As a simple test, I created a new project and simply replaced the index.razor page with the sample code below. When you run it, notice:
The "Test Value" for the textbox input control initializes just fine.
The checkbox's initial value is true, but the checkbox is not checked.
Change the textbox input control's text and then change control
focus. Notice a message gets printed in the Debug window in the Output
tab of Visual Studio (Expected behavior)
Change the checkbox input control's value (checking or uncheck) and then change control focus. Notice that there no message appears in the Debug window in the Output tab of Visual Studio (Not expected behavior).
#page "/"
<div class="form-group">
<label for="last-name">Textbox Binding Test</label>
<input #bind="TestString" type="text" class="form-control" id="last-name" placeholder="Enter Last Name" />
</div>
<div class="form-group">
<label for="send-email-updates">Checkbox Binding Test</label>
<input type="checkbox" bind="#TestBool" id="send-email-updates" />
</div>
#code {
private bool _testBool = true;
protected bool TestBool
{
get { return _testBool; }
set
{
_testBool = value;
System.Diagnostics.Debug.WriteLine($"Value of {nameof(TestBool)} = {value}");
}
}
private string _testString = "Test Value";
protected string TestString
{
get { return _testString; }
set
{
_testString = value;
System.Diagnostics.Debug.WriteLine($"Value of {nameof(TestString)} = {value}");
}
}
}
This behavior was observed regardless of making the properties public, used auto-properties (no private variables), or removed the control name/id attribute values. This seems to happen regardless of whether I use the #code directive on the page or separate out a viewmodel that inherits from ComponentBase.
The bottom line is that I'm able to get text-based values when a user submits the form, but all the Boolean properties seem to remain as they were when first initialized.
When you look at both controls:
<input #bind="TestString" type="text" class="form-control" id="last-name" placeholder="Enter Last Name" />
<input bind="#TestBool" type="checkbox" id="send-email-updates" />
It is clear you are mixing bind and #bind notations, probably from older Blazor editions.
This one works in rc1:
<input type="checkbox" #bind="TestBool" id="send-email-updates" />
but in general I would argue for using the <EditForm> and related tags:
<EditForm Model="this">
<InputCheckbox #bind-Value="TestBool" />
</EditForm>

How can I configure a live template that generates a builder method in IntelliJ IDEA?

I oftentimes need to create builder methods in my code. These methods are similar to getters, but they return this and they use with instead of get.
To be faster with that task I'd like to create a live-template in IDEA.
This how far I got:
(in ~/.IntelliJIdea14/config/templates/user.xml this looks like this:)
<template name="builderMethod" value="public $CLASS_NAME$ with$VAR_GET$(final $TYPE$ $PARAM_NAME$) {
this.$VAR$ = $PARAM_NAME$;
return this;
}" description="create a builder method" toReformat="true" toShortenFQNames="true">
<variable name="CLASS_NAME" expression="className()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="PARAM_NAME" expression="VAR" defaultValue="" alwaysStopAt="true" />
<variable name="TYPE" expression="typeOfVariable("this." + VAR)" defaultValue="" alwaysStopAt="true" />
<variable name="VAR_GET" expression="capitalize(VAR)" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JAVA_EXPRESSION" value="false" />
<option name="JAVA_DECLARATION" value="true" />
</context>
</template>
This nearly works, except for typeOfVariable("this." + VAR) which does not. I just guessed how to call this method, because I could not find any documentation on the syntax used in the expressions except for this page which does not even mention a script language name or something that would make googling for easier.
How do I fix the call to typeOfVariable?
Bonus question: How can I get complete() for VAR to only show fields?
Similar question but not does not even have a start: Live Template for Fluent-API Builder in IntelliJ
Replace typeOfVariable("this." + VAR) with typeOfVariable(VAR).
Edit:
Another way to generate builder methods is to use an appropriate setter template (instead of live template).
https://www.jetbrains.com/help/idea/2016.1/generate-setter-dialog.html
There is already a built-in setter template named "Builder" which generates setters such as:
public Foo setBar(int bar) {
this.bar = bar;
return this;
}
You can create your own template (e.g by copying it) and change it so that the method prefix is with.
And to make the generated method parameter final go to settings:
Editor | Code Style | Java
Select the Code Generation tab
Tick Make generated parameters final
IntelliJ IDEA add final to auto-generated setters

selection xtype listener unable to access multifield and multifield unable to fire the dialog.form.findfield

I have been working on disabling the content and ignoring data that is populated in a selection field that forms part of multifield that comes with CQ5 dialog
below is the code that is not working
<medicineType
jcr:primaryType="cq:Widget"
fieldLabel="medicine Type : "
name="./medicinetype"
blankText="medicine Type"
type="radio"
xtype="selection">
<options jcr:primaryType="cq:WidgetCollection">
<inactive
jcr:primaryType="nt:unstructured"
text="medicine Lead"
value="medicinelead" />
<active
jcr:primaryType="nt:unstructured"
text="doctor Lead"
value="doctorlead" />
</options>
<listeners jcr:primaryType="nt:unstructured"
selectionchanged="function(box,value){
if(value == 'medicinelead'){
var ans = box.findParentByType('dialog').form.findField('./medicinedoctorset');
ans.setDisabled(true);
ans.getEl().up('.x-form-item').setDisplayed(false);
}
"/>
</medicineType>
<medicinedoctorset
fieldLabel="doctor cards"
jcr:primaryType="cq:Widget"
name="./medicinedoctorset"
xtype="multifield">
<fieldConfig
jcr:primaryType="cq:Widget"
type="select"
options="/apps/doctor-api/components/medicineview/doctorcards.json"
optionsRoot="doctorcards"
optionsTextField="label"
optionsValueField="id"
xtype="selection" />
</medicinedoctorset>
Not sure what exactly you are trying to do in the code, but it seems to me that you are trying to disable a particular field medicinedoctorset. Try adding id to the field you want to disable, get and disable the field as CQ.Ext.getCmp("idOfTheFieldToBeDisabled").setDisabled(true); Hope this helps.

How to send parameter to fileUploadListener in PrimeFaces fileUpload

When I create a model I would like to save images for a model. I am using PrimeFaces fileUpload component. When I save pictures I want to know to which model particular image refers to. That's why I need to send id of a model to backing bean.
Is there any possibility to send id of model to fileUploadListener?
<h:form enctype="multipart/form-data">
<p:panelGrid columns="2">
<h:outputLabel for="hotelName" value="#{msg.hotelName}"/>
<p:inputText value="#{apartmentNew.name}" id="hotelName"/>
<h:outputLabel for="hotelDescription" value="#{msg.hotelDescription}"/>
<p:inputText value="#{apartmentNew.description}" id="hotelDescription"/>
<h:outputLabel for="hotelImages" value="#{msg.hotelImages}"/>
<h:form enctype="multipart/form-data">
<p:fileUpload id="hotelImages"
fileUploadListener="#{apartments.handleImageUpload}"
mode="advanced"
sizeLimit="10000000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/">
</p:fileUpload>
</h:form>
</p:panelGrid>
<p:commandButton id="saveApartmentButton" value="#{msg.save}" action="save"/>
<p:commandButton id="cancelCreationApartmentButton" value="#{msg.cancel}"
action="cancel"/>
</h:form>
Not via request parameters. You can do so via component attributes.
E.g.
<p:fileUpload ...>
<f:attribute name="foo" value="bar" />
</p:fileUpload>
with
String foo = (String) event.getComponent().getAttributes().get("foo"); // bar
I needed to pass a key parameter along with the uploaded file. I found that fileUploadListener executes during the APPLY_REQUEST_VALUES phase, so I could not use an EL expression in the f:attribute tag. I also tried to find the value using event.getComponent().findComponent("id"), but although the component was present, the value was null. I think a #ViewScoped bean would fix the missing value, but I am stubbornly attempting to keep my beans at #RequestScoped until I have absolutely no other option. Ultimately, I had to use FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id") which I got from http://forum.primefaces.org/viewtopic.php?f=3&t=6432
Error in types:
String foo = event.getComponent().getAttributes().get("foo");
Instead, do it this way:
Object foo = event.getComponent().getAttributes().get("foo");
Integer foo = (Integer) event.getComponent().getAttributes().get("foo");
You can use:
<div onclick="#{myBean.myMethod(myParam)}" >
<p:fileUpload id="fileUpload" fileUploadListener="#{myBean.onFileUplod}" mode="advanced" dragDropSupport="true"
update=":messages" process="#this" >
</p:fileUpload>
</div>
Method in myBean:
public void myMethod(String myParam) {
selectedMyParam = myParam;
}
Then you can use selectedMyParam in onFileUpload method.

Disabling radio button in jsp based on map value struts2

I'm trying to use a boolean value returned from a map in my bean to either disable/enable a radio button in a jsp page.
Class snippet:
public class Options{
private String optionId;
private Map<String,Boolean> negativeMap;
public setNegativeMap(Map<String,Boolean> negativeMap){
.......
}
JSP snippet:
<input id="radioClick<s:property value=optionId"/> type="radio" disabled="%{negativeMap[optionId]}" />
Am I on the right track with this? Is there something I'm missing?
Please, I think your JSP snippet was wrong near "value=optionId"/>" :
<input id="radioClick<s:property value=optionId"/> type="radio" disabled="%{negativeMap[optionId]}" />
Your JSP snippet should be like this :
<input id="radioClick<s:property value=optionId/>" type="radio" disabled="%{negativeMap[optionId]}" />