How to link HTML5 input to two forms - javascript

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.

Related

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.

Ext JS Search button which is attached to form

So basically I wanted to make a thing like this. A search button which is disabled. than when somebody enters something in one of the fields of the form I want button to enable itself and if somebody cleans every input up than I want to disable that button back. So my Idea was something like this. Extend the button provided by Ext JS. and somehow when this button is drawn or initialized, attach event of value changed to its upper form's input fields. so basically when somebody changes value of any input, that method attached as a listener will be called and than with help of some logic I can achieve the thing I want. but there is one problem. I don't really see how to do that. I mean I have some guesses but all of them have led me to the dead-end. So can you suggest what can I look at or from where to start ?
You can use formBind to enable/disable button depending on the validity state of the form. Validity of the form fields are checked against the validators set for form fields (like allowBlank: false). See the Example usage here
Another way to validate is to use VTypes
But, for the specific requirement to enable button if user enter values in any one of the fields, you might require custom validators. Sample fiddle here: https://fiddle.sencha.com/#fiddle/5qe

Zend Framework - Add new input element using 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.

Dynamic form submission using javascript - how to elegant code?

Here is the problem:
My page displays a set of items. Each item has a check box associated to it (which is part of a form). User might check any of these check boxes and press 'Delete' button. The same page also has a 'Upload' button which would upload an excel sheet of items.
At the moment, my form action sumbits to say : "xyzAction" and I have two different handlers (analogous to Struts Action) - one for deletion of stores and other for uploading stores.
I am told that the best way to do this is to rely on javascript by doing one of these:
1)) Switching form action on press of upload and delete buttons - there by invoke different actions.
2) Use a hidden variable "act" to set it to delete / upload and submit to a single form. The server side action would take care of identifying the act and forwarding to the corresponding action.
Approach (1) - seems very inelegant to me. Playing with form action seems unnecessary.
Approach (2) - would obviously not work if your javascript is turned off and is not very elegant either.
There must be a third way to doing this?, which would make me happy?
It sounds like you may need two different forms, for the two different actions.
You need to get the HTML correct first.
You have two different actions, so you should have two forms - a good rule of thumb is that each form should only have one submit button. This is the best practice for HTML and will ensure that the page works without JS or any other trickery.
Once you have the page working like this, use JS to manipulate the DOM to produce the UI that you need. This is using JS to add behvour to the UI and is best practice for unobtrusive JS.
(If you really want to conflate your actions in a single form, changing the action of the form with JS is the best course of action. But consider what would happen if a user checks a check box and then changes their mind and uploads a file leaving the checkbox checked. You should take care that this shouldn't delete anything.)

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