Getting the value of Primefaces' p:editor when onchange is called - jsf-2

I have a component.
I want to be notified whenever its value is changed. Then I need to check the new value to decide if it's empty (hence, I'll disable the submit button) or not.
The former can be done using the onchange attribute.
My problem is with the latter: accessing p:editor's value from within javascript!
Please help.

I found the solution.
Here is the p:editor with the onchange attribute:
<p:editor onchange="handleTextChange()" id="responseEditor"/>
and here is the Javascript callback function:
function handleTextChange()
{
editorText = document.getElementById("appDetailsForm:responseEditor_input").value;
alert(editorText);
}
p.s.1 appDetailsForm is the id of the enclosing form. Primefaces prepends it to the component's primfaces Id when it generate the html.
p.s.2 appDetailsForm:responseEditor_input is the id of the textarea inside the <p:editor> component that is created when the html is generated.

Related

Asp.Net MVC Model Binding with JQWidgets

When you want to create a DateTime picker control with JQWidgets, you must define a div element and then call a function like this using Javascript:
$("#MyDivElementId").jqxDateTimeInput().
The problem is: I'm not able to figure out how I can use Model Binding of Asp.Net MVC with this syntax. I mean, the Model Binding feature will try to match key-value pair received from input controls in the form element and obviously, div element are not input control.
I found somebody who already resolved this problem using hidden field set with values of matching div JQWidgets element before submitting form but I don't like this solution; it's not natural and I must write to much code for a thing that should be simpler in my view.
Does anybody have more elegant solution?
If you set the "name" attribute of the DIV tag, the value from the DateTimeInput's Input tag would be submitted.
First of all when you submit id is not submited and i just opened that plugin demo. when you add code $("#MyDivElementId").jqxDateTimeInput(). it will create textarea with name MyDivElementId and when you submit then you will have the same value on server side. Other issue can be with date format since they would be probably different on client side and server side.
try to add input parameter for controller "DateTime MyDivElementId" and check if its null or not.

set value of field using rjs

I need to set the value of a field in my form using rjs.
That field in question appears twice in my form and I want to set the value of the field found inside a particular div only.
The code below does not seem to work.
page[:mydiv_id][:book_bk_title].value = "myvalue"
My question is how do i set the value of a field by also specifying the div containing it. using rjs?
If you are using Jquery:
var div_id = "mydiv";
$("#"+div_id+" :input[ID^='input_id']").val('Field Value');

Dynamically added option in SelectOneMenu control on a JSF form submits as null

I have a standard JSF h:form which contains an h:SelectOneMenu control. As long as I am selecting an item from the list which is populated when the page is rendered it works perfectly. I don't think it is important, but to put it in context, the value from the select is used to build a query which returns a list of matching records.
I've implemented the JQuery autocomplete box on the control and it still works just fine as long as I'm selecting one of the original values.
The problem comes when I enter a value not in the select control when the page is rendered. Using JQuery, I've set it up so that when a value not on the list entered, the value is added to the select as a new option.
I can verify that the option is added to the underlying select control, and selected through the javascript. However when the setter is invoked in the backing bean immediately after that, the value passed in to the setter is null, and the function to run the query is never reached. The following error is returned in the AJAX response, but I have yet to be able to find a place where the value is validated. It isn't a required field either.
Validation Error: Value is not valid
Here is my front end code:
<h:selectOneMenu id="make" styleClass="combobox" value="#{listBean.make}"
effect="fade" label="#{listBean.makeLabel}" >
<f:selectItems value="#{listBean.makeList}" />
</h:selectOneMenu>
And the setter in the bean:
public void setMake(String make) {
this.make = make;
}
I'm guessing I just need to find a way to include the new option in the makeList List on the backing bean, but I'm not sure how to do that. Any help or suggestions would be appreciated.
Java EE 6, GlassFish 3.1, Eclipse 3.7 - problem observed on both FireFox and Chrome
You need to provide the dynamically added item through <f:selectItems>, not through JavaScript. If the item is not present in <f:selectItems>, then you will get exactly this validation error. This is done so as part of safeguard against tampered/hacked requests in an attempt to get illegal/unprovided values into the server side.
Easier is to use a JSF component library. PrimeFaces for example has a <p:autoComplete> for the exact purpose.

asp.net mvc disabled text box updated by javascript does not post new value

I am using a strongly typed model for my view. I have a disabled text box whose value I update using javascript. The textbox is rendered using this
<%: Html.TextBoxFor(model => model.TotalAmount, new { disabled = "disabled"})%>
This renders a textbox with NAME and ID as 'TotalAmount'. TotalAmount is also a property on my model that binds to this view.
The javascript to update its value in the view is like this within its function:
document.getElementById('TotalAmount').value = {assigning new value here};
The function does get called and I can see the value in the disabled textbox when I change some value in another editable textbox. However, when I post this form to my action method as below :
[HttpPost]
public ActionResult Process (ProcessVM FormPostVM)
{
}
the disabled textbox property [TotalAmount] still has the old value but the editable textbox which I modified contains the new value I entered. Why does the disabled textbox not contain the javascript updated value?
I tried using
ModelState.Remove("TotalAmount");
in the action method above, but as I already figured it didn't work.
Any clues, tips?
Thanks for your time....
HTML input elements such as textboxes that have the disabled="disabled" attribute will never send their values to the server when the form is submitted. If you want to send the value to the server while still disabling the user from changing it you could make the textbox readonly:
<%= Html.TextBoxFor(model => model.TotalAmount, new { #readonly = "readonly" }) %>
Disabled inputs are never sent in a form submit, try using readonly attribute instead or hidden inputs
Disabled fields don't get posted. Try having a hidden form field that will send the value to the server, and set both TotalAmount and the hidden form field. On the server, use the value for the hidden field instead.
On a side note, since this looks like the order total, this is something I would recalcuate on the server rather than opening up the possibility of someone hacking the html and getting a discount on their product.
EDIT:
To the other's points, I'd forgotten about the readonly attribute. That will work too.
If you change it to use readonly rather than disabled, then this should give you the same functionality, but post the value.
Browsers don't post values back in disabled input controls, as you've discovered. Probably the easiest way to work around this is to hook onto form submission, and re-enable the input as the form is being submitted; the user won't have a chance to edit the value, and it should get posted with the rest of the request.
i think the last issue described it : please check it out :
Retrieving the value of a asp:TextBox

Multiple controls in MVC view that modify same model value

I have a field in my model - call it 'distance' - and I want to write a view that contains both a checkbox and a textbox.
If the checkbox is checked, the textbox becomes disabled and 'distance' gets the value 0 when the form is submitted.
If the checkbox is not checked, 'distance' gets whatever value is in the textbox when the form is submitted.
Anybody have any guidance how to do this? It's the logic of assigning a model value based on the state of both the controls that confusing me...
The UI behaviour can be accomplished with Javascript via an onclick event handler on the checkbox. The only value you need to submit is the input field whose value will be set to 0 or whatever is set manually.
With jQuery you would have:
$('#checkbox_id').click(function() { $('inputfield_id').val('0');
The Model behavior depends on what the checkbox represents. If it's an actual property on the Model, then you will need to check the value server-side as well as (optionally) using aleemb's javascript method.
If a check in that field simply represents a distance of zero, then aleemb's method will suffice.
Aleemb's answer worked like a charm, with a couple of minor modifications:
$("#Distance_checkbox").click(function() {
if ($(this).is(':checked')) {
$("#Distance_textbox").val(0);
$("#Distance_textbox").attr("disabled", true);
}
else {
$("#Distance_textbox").val("");
$("#Distance_textbox").attr("disabled", false);
}
});

Resources