Dont fill request scoped bean - forms

I have a request scoped bean which is filled with various properties from a form. These properties are then used to update another view scoped bean. Now I want to give the user the possibility to the reset the form in such a way that all form fields are holding the values they had when the page was loaded the first time. These values are defined through the bean itself:
#ManagedBean
#RequestScoped
public class ItemSearchBean {
private Rarity minRarity = Rarity.None;
private Rarity maxRarity = Rarity.None;
...
}
Notice though that the form submiting button actually invokes a ajax request, therefor no full page reload goes on.
The submitting button:
<p:commandButton value="Search"
actionListener="#{itemSearchBean.refreshTable}"
update="itemTable,notify"/>
I already tried to use a simple reset button, but it only reseted the form to the last submitted values:
<p:commandButton type="reset" value="Reset"/>
One has to somehow ask the server for a fresh new bean (or prevent it to fill the bean), but I have no clue how to do this.

You should be able to do that with a plain HTML link to the same view:
Reset
or let JSF create the link for you:
<h:link value="Reset" />
This way you'll have a new GET request that will create a new UIViewRoot, just like if you were accessing the same view in a new browser tab.
If you want a button instead, you can use an h:button
<h:button value="Reset" />
This button will rely on a Javascript that will reload the page on click.

you could also do a two phase setup. a little more work, but allows all sorts of Undo workflows if you take it further.
basically, have two beans that encapsulate your form with the usual accessor/mutator pairs. then in your controller, expose only one (either x, or y) and that is your "form bean". in y, you store the pristine copy that is retrieved from the repository, that way you can do field level or bean level reversion without having to reload the page or do a full data remarshall from the repo.
so, simply:
2 beans to encapsulate the form (you might even be able to re-use your entity if it maps onto the form).
fill both from the repo during data marshall
expose one to the user in the facelets
keep the second as a backup for resetting/reverting either fields or the entire form
there are other ways to skin the "undo" cat, and this is only another.

Related

Stop "?" from being added to URL

I currently have an Angular 2 app where the user can submit a form to create a new item. When the submit button is clicked, it calls a function that sends the data to the server and navigates to a new page when the server confirms that the data has been saved successfully.
My problem comes because the form submission appends the form parameters to the URL. So for example if I had an input named title and submission took me to the route mytitle which is the input for the title field, Angular (or whatever injects the GET parameters) would try to navigate to mysite.com/mytitle?title=mytitle instead of just mysite.com/mytitle. Even adding [ngModelOptions]="{standalone: true}" to all of my inputs still leaves a question mark with no parameters after it.
This is a problem because it causes Angular to reload the app because the given route does not match any routes in my route definitions. Is there a way to disable the GET parameters being injected into the URL entirely? POST doesn't work either because I have nowhere to post to, and my next URL uses data from the form itself.
I found an answer to my question, the "Submit" button defaulted to being of type "submit", so changing it to type "button" removed the GET parameters injection behavior.
Check setValue in your form control. Example: Set value this way this.form.controls['val'].setValue='' rather than assigning an empty value
(i.e. this.form.controls['val'] == 0).
If your problem still exists, kindly add button type submit to the button and manually give the click function access to it.

Send new value to Google Tag Manager through dataLayer from fancybox (ajax)

I'm using GTM and I need sending several values for each web/product.
In a specific web I've a lightbox to get some data from user and need to send a "conversionValue" with a different value when the user finish the task.
When I try to do this by using dataLayer.push it just add a new object to the original dataLayer (as it is in a fancybox loaded by ajax, not iframe, it gets the "parent page" dataLayer), so I get a dataLayer with many objects and only the last one has the correct "conversionValue".
The problem is, if I try to use this variable from GTM, it gets the first object to look for the conversionValue...
Should I clear the dataLayer before pushing new objects? Am I doing it wrong? Is there any other way to do this?
After reading about Google Tag Manager, it doesn't matter if you add new objects to dataLayer because their variables can't be readed from GTM the same way as you read them for the first time.
You have to read new values by defining events.

Field and Form Validation with ember.js

I've integrated Boronine's excellent field validation code for ember.js from jsfiddle. While that is wonderful, I still need to perform form level validation, to ensure that when the user submits the form, everything is okay.
What's the best way to do that? Is there a way that I can mark a field as having been validated, so that the form handler can simply walk the fields to see what has been validated or not?
MP.SignUpFormView = Em.View.extend({
submitLogin:function (event) {
// walk through object fields to perform validation here, but how?!
}
});
Edit:
For clarity, I am using Handlebars and binding, not trying to walk DOM objects or the like.
The pattern you're trying to use makes sense in applications that follow a document-scripting pattern, which Ember does not. You can force this work, but you'll find each next step in the application will get harder and harder.
In Ember display is backed by data objects so form fields in an Ember application are bound to a property on some object and as changes are made, the values are updated immediately. You don't even really need a <form> except maybe for styling.
When a user wants to take some action on this object (like persisting it to a server) the application's current state will answer the question "what happens when a user wants to take this action right now?" A user clicking a button here doesn't mean "now serialize the data in the form and do something" it means "I'm done changing the properties of this object and would like to do something else in the application now."
Your handlebars template would look something like this:
{{view Ember.Textfield valueBinding="name"}}
{{view Ember.Textfield valueBinding="age"}}
<button {{action save content}}>Save</button>
And a possible state in your application where this can be handled
Ember.Route.extend({
save: function(router, event){
if (event.context.validate()){
router.transitionTo('someNewState')
}
}
})

How to submit a form in RichFaces?

I know it maybe sounds a basic question but I'm having a hard time figuring this out.
First of all I have this form:
<h:form>
<h:inputText value="#{movies.name}"/>
<a4j:commandButton id="mybutton" value="Modify" immediate="true" action="#{movies.testModify}"/>
</h:form>
I want to catch the value from the input text from within my testModify() method from movies bean.
My problem is that testModify doesn't get called at all. The odd behaviour that I noticed is that when I remove h:form tag the method does get called but I still don't know how to get the value from my input text.
From what I've read, a4j:commandButton needs h:form for it to work properly.
Any help will be highly appreciated!
Use an h:commandButton instead of an a4j:commandButton. The first is the standard JSF button that will submit your form, the latter performs an ajax request.
First of all, as stated by Markos Fragkakis, use a basic <h:commandButton> instead of an ajaxified <a4j:commandButton>, as in your case, you have no interest of using an Ajax action here.
Second, remove the immediate="true" attribute on this button. Using this attribute means that the default ActionListener should be executed immediately (i.e. during Apply Request Values phase of the request processing lifecycle), rather than waiting until the Invoke Application phase.
If your action on Java bean is still not called, maybe something wrong happend before the Invoke Application JSF lifecycle phase. Adding a <h:messages> will display the possible issues:
<h:form>
<h:messages/>
<h:inputText value="#{movies.name}"/>
<h:commandButton id="mybutton" value="Modify" action="#{movies.testModify}"/>
</h:form>

ASP.NET MVC passing data between forms

Im pretty new to ASP.NET MVC, trying to figure out my way around.
Currently i return a ViewModel which has a IEnumeable Events as its only property. This ViewModel is StronglyTyped to a UserControl which dislays the IEnumable Events in tabular form.
One of the properties of the Event Model is an XElement, which contains some XML loaded from the DB.
Now i've added a link to the end of the tablular data to be able to view the XML in a separate page. How do i pass this data to another page for viewing?
I would post a request back to the server with some sort of Id for the Event-object and have the receiving end send back the XML related to that Id.
if you're looping through the Event objects in your IEnumerable, you can do something like:
<%= Html.ActionLink("GetXml", "Events", new { id = currentEvent.Id }) %>;
Now create an Action on your EventsController (given that you have one) like so:
public ActionResult GetXml(int id)
and retrieve the XML to pass back to the View
There are basically two ways of bringing data from one page to another using ASP.NET MVC (or any other language/framework which follows the HTTP protocol):
Sessions: Use a session to store the data you need, and load it back up at the next page.
Post the needed data back to the server. This way, the server can hold it and display it on the next page. Posted data usually comes from input or textarea elements. If you use input type="hidden" you can give it a value which represents your data. This way, you can post it back and forth till you arrive where you want.
Besides what Arve is advising, you could also consider TempData.
If you use the Get-Post-Redirect/Forward concept for you app, you could do something like:
GET - Initial Request comes in, Server responds with View and model data. User selects an item which leads to ...
POST - User selects one of the items from #1, triggering a post. That particular item can be fetched from repository, placed in TempData and then...
REDIRECT/FORWARD - The redirect collects the information out of TenpData and uses it as the model for the new View.
here is an example http://www.eworldui.net/blog/post/2008/05/08/ASPNET-MVC-Using-Post2c-Redirect2c-Get-Pattern.aspx