Zend Framework - Add new input element using javascript - javascript

I'm working on a project using Zend Framework.
I'm creating a form on which users can add a set of elements by pressing a + sign.
Zend framework uses subforms and decorators to get array of values from a form.
These will show when the page is displayed
How does the new fields created with Javascript integrate in that model?

The best demo of dynamically adding fields on the client to a Zend_Form with which I am familiar comes from Jeremy Kendall:
http://jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/
The upshot of the technique is to add/call a preValidation() method on the form to check the post for fields missing in the form. If it finds any such fields, then they are added to the form object. By the time isValid() and getValues() are called, all the Zend_Form_Element objects have already been attached to the form, so processing runs as normal.

One suggestion would be to define all input fields that you want to provide using zend form.
But when the form is displayed you could hide certain fields and make them visible by clicking on +.
I think this is the most simple approach because for adding decorators and stuff you would need to change php files on client side and this is not possible.
Another suggestion, you could define several forms. Clicking on + redirectes the user to another form with an added field.

Related

How to link HTML5 input to two forms

I have an input that's used by JS to control submitting two forms with different actions, but only one of them will be submitted and it should include this input.
I can do it with a hidden input using JS to change their values when the original one changes but I'd like to know if there is an HTML5 solution.
Here is my JS code to do it:
$(function(){
$('#originalOne').change(function(){
// check if I should disable a form
$('.hiddenOnes').val($(this).val());
})
});
I think I might be understanding what you are asking, correct me if I'm wrong:
"Is there a way to update the values in one form based on the values in another using HTML5 only (without using JavaScript)?"
Unfortunately, the answer there is NO. You will need to attach event listeners and handle changes using JavaScript. I had previously suggested using a <fieldset> to group the inputs that you wanted to disable independently, but this method does not work for multiple form actions.

In Salesforce open the edit screen after the object was created with Javascript button

In Salesforce, we have a button that creates events with the fields already filled out, but after the item is created I want it to reload the screen in the "Edit" view in case they want to change anything. Any ideas?
Do you need to validate fields values? Maybe it will be easier to do with field validation rules? It can be useful in cases when your data is populated not only from user screens, but also can be created in code (classes/triggers) or via API.
If you know that you exactly should to use JavaScript, you can add standard event attributes to command buttons on your Visualforce page and write JS functions for implementation of your validation logic.
I ended up creating a visualforce page with a controller and a custom button to accomplish what I needed.

web2py form read values in js

I'm building a webpage in web2py.
The effect that I want to achieve is a form that one can submit in the usual way, but where the html page containing the form also features an html canvas which is managed by js code that is able to dynamically see the contents of the form. This enables a sort of preview to be seen live as the user changes the contents of the form and updated continuously prior to the user submitting the form.
It seems there are two challenges to doing this.
1.) web2py likes you to define forms in the controller which generates to form code automatically - I don't see a way to add extra elements in the middle of the form by editing the html page.
2.) I don't see any way to get js code to dynamically read the values entered into the form.
Any suggestions of how to do this would be much appreciated. I'm new to web2py but have spent quite a while trying to figure out how to get this working
Thanks!
1.) web2py likes you to define forms in the controller which generates to form code automatically - I don't see a way to add extra elements in the middle of the form by editing the html page.
Regarding the above, letting web2py generate the form HTML is an optional convenience. If you need custom markup, you can do that as you normally would. There are also ways to add form elements to web2py's server-side DOM representation of the form.
See:
http://web2py.com/books/default/chapter/29/07/forms-and-validators#More-about-manipulation-of-FORMs
http://web2py.com/books/default/chapter/29/07/forms-and-validators#Adding-extra-form-elements-to-SQLFORM
http://web2py.com/books/default/chapter/29/07/forms-and-validators#SQLFORM-in-HTML
http://web2py.com/books/default/chapter/29/07/forms-and-validators#Custom-forms

Suggestion needed for NON JavaScript version of an input autocomplete

I'm building an App that is heavy on jQuery. Most of it I can handle without the use of JS and still have a functioning site, however there is one bit that is eluding me. (note, I'm using ASP.NET MVC but that shouldn't matter in this instance)
I have an input field that is making great use of jQuery-UI AutoComplete. The behavior is very simple. The user is asked to input their City, but is given an AutoComplete list of valid cities. If the city is invalid, the server side validation fires and tells them to try again.
If they do select a valid city, the jQuery method updates a hidden field that contains the CityID of the selected city. This is working phenomenally well, and I really like the performance.
Here's where the problem enters. If JS is not available in the browser, the ID field is not updated, and hence the DB is not updated. I am not using the AutoComplete input on the server side at all, just the ID field. What would be a good solution to circumvent this issue?
Default to a select element containing the cities as options and id's as values, and change it to the autocomplete field with the script on page load.
If for some reason sje397's answer doesn't work for you (it's an elegant solution, unless the city auto-select is based on some other field on-screen, such as a zip code or state), simply POST both fields. When evaluating the POSTed data, if the CITY text box has data, and the hidden field does not, then evaluate the entered city using the same validation method used by the jquery callback. If the hidden field has data, you assume that javascript is enabled and use your current logic.
Several options:
1 - Serve HTML initially that shows the "hidden" input, and doesn't include the "autocomplete" one. When JS loads, have a function edit the DOM to your current situation.
2 - Have the form default to send the "autocomplete" data to the server. Use javascript to edit the "send" function to have it switch to the "hidden" input.
Get the page to by default to send the input of the user over the intertubes to your server, if javascript is enabled, change it so it only sends the ID over instead (using javascript obviously).

Javascript submitting all the forms on a page

I'm using jsf 1.2. When a particular jsp has more than one form with a specified id, for example when using something like below, jsf gives the form a seemingly random id.
<ui:repeat>
<h:form id="repeatingform">
...
I would like submit all forms using javascript. Is there a way to do this without knowing the ids of the forms?
Submitting more than one form at once it not really possible. The problem is that each form requires its own separate request - submitting a form is basically similar to clicking a link, and you can't open all links on a page at once (you can by opening them in new tabs/windows, but that's a different matter)
If you really do want to keep each form its separate form element, you can use Aquatic's example,
var forms = document.getElementsByTagName("FORM");
for (var i=0; i<forms.length; i++)
forms[i].submit();
but replace the code which runs submit() with code which submits the form using XMLHttpRequest. You can have multiple XMLHttpRequests running in the background.
Hmm, It won't work like that real easy. If you would use something like document.form1.submit(); it posts that specific form and all values in it.
So it's no use looping through all the forms and submitting every single one.
That would be the same as clicking on the submit button of each single form, resulting in each form being posted separately.
The solution is to collect the values of each field in each form in a single collector form, and post the collector form.
You can read (with code examples!) more about it here: http://www.codetoad.com/forum/15_24387.asp
You can access all forms on the page in the next way
var forms = document.getElementsByTagName("FORM");
for (var i=0; i<forms.length; i++)
forms[i].submit();
I think there are several parts of this problem.
1. Are you trying to submit multiple forms in JSF? There is a way you can seggregate components in different forms and then submit a chosen group of them if req. The outer tag may be and inside these you may have as many as you may like. (Please know t:subform is a tomahawk library; but it works well with JSF). Also Subforms can be submitted in a chosed group using t:commandButton.
Notice the last paragraph on subform deocumentation that all forms with comma seperated id's may be submitted. so for example
some input controls here
more controls here
whole lotta controls here
something like this will allow you to submit selective forms and validating components only for forms you wish to validate. If that is what you are really trying to do here.
But if your intent is really to submit these forms by a single piece of java script I have a small advise, javascript supports lookup of form tags dynamically in a dom tree as shown in above example by aquatic. you could traverse those ui elements also by implementing a PhaseListener which gets invoked before "RENDER_RESPONSE" phase and there traverse the ComponentUITree to get the names or list of all the UI components and then provide them to javascript by scriplet variables or $ variables that JSP 2 supports.
I could give more detailed answers or examples if you could exactly lay out your problem, as to WHY you want a common java script and do you intend to submit multiple forms parallely or you intend to submit them one at a time ( as and when requiered) but by a common piece of java script.
Parallel submission of HTML form component is really not possible until and unless u use javascript to accumulate component values from all forms and then do whole lot of manual maipulating.
Give more details on your actual intent.

Categories

Resources