I am using jquery validate plugin to validate form fields . I am also using jqtree . on click of every child node a section of form is visible to user, which is supposed to be filled with values.For every child there is a form content to be filled. Entire tree content is declared within one form only. I have a button in the form which on click generates json file. I am calling the function below to validate form
$("myform").validate();
....
if($("#my-form).valid())
generate the json file
but this is not validating the entire form. suppose I am on childNode1 it validates only form section defined for childNode1. As far as I have understood jquery validate plugin should validate entire form when correct form id is specified. But can anyone tell me what has gone wrong in my approach?
The .validate() method does not "validate the form". It only initializes the plugin on the form. .valid() will programmatically trigger a validation test.
Your code:
$("myform").validate();
....
if($("#my-form).valid())
generate the json file
$("myform") - Is that supposed to be an id, class, or name? As you've written it, it's looking for a <myform></myform> element.
$("#myform") // id="myform"
$(".myform") // class="myform"
$("[name='myform']") // name="myform"
Is your form element called myform or my-form? If it's the same <form> element, then the two jQuery selectors would be the same.
$("#my-form) is missing the closing quotation mark.
If the id of the <form> element is "myform", then your code should be...
$("#myform").validate(); // <- initialize the plugin
....
if ($("#myform").valid()) { // <- test the form's validity
// generate the json file
....
}
OP Title: jquery validate plugin, validating form fields of only current screen
Your question does not seem to have anything to do with the title. There is only one form described in your OP, and since this is JavaScript, only the page that's loaded in the browser is relevant. Not sure what you mean by "current screen".
but this is not validating the entire form. suppose I am on childNode1 it validates only form section defined for childNode1. As far as I have understood jquery validate plugin should validate entire form when correct form id is specified.
By default, the plugin will not validate any form fields that are hidden. You would manipulate the ignore option to over-ride this behavior. Setting ignore to [] will tell the plugin to ignore nothing and validate all fields including the hidden ones.
Related
When I try to submit a form, either via an input[type=submit] or by calling form.submit(), Parsley validates the form and cancels the submission if invalid. Is there any way I can skip that validation since I'm manually calling validate on sections of my form?
Specifically what I'm trying to achieve is submitting partial versions of the form, so I validate a group and only that portion is sent to the server (even if the rest of the form is still not valid).
I you want to cancel Parsley default validation on submit event, you'll have to remove the submit.Parsley binded event on your form.
Doing a $('#yourform').off('submit.Parsley'); should solve your issue.
Best
Edit: For Parsley2, since events names have changed, it should be $('#yourform').off('form:validate');
if you want to skip single element just use :
data-parsley-excluded
Form fields that won't be validated by Parsley. For example, if you
want to add disabled and hidden fields to the existing list, use:
data-parsley-excluded="input[type=button], input[type=submit],
input[type=reset], input[type=hidden], [disabled], :hidden"
but if you want to validate a specific groupd then use:
data-parsley-group
Assign a group to a field for specific group validation. eg:
data-parsley-group="signup". This way, you could only validate a
portion of a form and not all the fields.
source :
http://parsleyjs.org/doc/index.html#psly-usage-form
When running the submit in JS you can do:
$('#yourform').parsley().destroy();
So with jQuery in code, it could look like this:
var $myForm = $('#yourform');
$("#submit-button").on('click',function(e){
e.preventDefault();
$myForm.parsley().destroy();
$myForm.submit();
});
As I answered here, adding formnovalidate to the button seems to work
https://stackoverflow.com/a/74746624/1148163
The jQuery validation engine plugin has the ability to do ajax validation; which works gret except for one small catch...
It sends off the field ID instead of the field name to be validated.
Why is this an issue?
I have a simple item that to create it only requires one textbox to be filled out; so we have this as a modal on every page for managing said item.
We use the jQuery validation engine plugin to validate that the entered value is unique.
Now this also means that the modal shows up on the edit page. Which obviously has the title in a field as well for you to edit.
And we want this field to be validated as well but because the validation engine sends across the field ID instead of the field name we must give the two fields different ID's
e.g. createtitle and edittitle and then on the backend have
if($fieldId == 'createtitle' || $fieldId == 'edittitle'){$fieldId = $fieldId}
Which really is an ugly approach; is there any way to get it to use the name; or another attribute instead?
Maybe this plugin could help you. It uses class names of your element to validate.
I've been trying to submit a form through mootools (1.4.5) on FF 14. The form does not contain an input named submit (which is often the problem). What I want is onchange in a select to submit the form. After half an hour the code below was my first attempt that got it working. objSelect is a select object that is contained within the form that I need to submit.
$(''+objSelect.getParent('form').get('id')).submit();
.
What the reason that the code below doesn't work as well?
// Without the explicit cast to string (''+); doesn't work
$(objSelect.getParent('form').get('id')).submit();
nor
// Most obvious way; doesn't work
objSelect.getParent('form').submit();
you can't have child nodes with reserved names that get exported via old DOM level API - see here how forms are being represented: http://www.quirksmode.org/js/forms.html#access
So basically - at an element level, if you have:
form
input[name=foo]
input[name=bar]
form.foo and form.bar will reference these element on the form element object.
the problem in your case is:
form (with a .submit and .reset method)
button[name=submit]
now form.submit stops referencing the submit method and starts referencing the input element.
you still submit this form by doing either:
rename the element that is wrong or remove from the form.
cheat by calling submit from a clean form.
new Element("form").submit.call(this.getParent("form"));
basically you create a new form element with a clean submit method. that returns an object and you call the method with the other "dirty" form as context.
this is just the same as doing
Array.prototype.slice.call(arguments)
My question seems to be the same as javascript validation for dynamic element , however the answers presented there do not appear to be working for me. I am using the query.validate plugin, and it works fine for most of my form. However, I have a button to add a row to a table containing several input elements. The code I am using to add the form input elements is the following:
var table=document.getElementById('flightlegs');
var rowCount=table.rows.length-1;
var row=table.insertRow(rowCount);
...
var cell4=row.insertCell(3);
var element4=document.createElement("input");
element4.type="text";
element4.size="3";
element4.setAttribute("required", "required");
element4.maxlength="3";
element4.name="legto";
element4.id="legto"+rowCount;
cell4.appendChild(element4);
$('#legto'+rowCount).rules('add', {required:true});
...(more of the same for other cells)...
Ok, so this may not be the best way (i'm new at javascript), but it does add the input fields as desired. However, when hitting submit, those new fields are not included in the validation. What am I missing here?
jquery.validate "selects only the first element for each name and only those with rules specified" (take a look at the source code of the plugin, the function called elements defined around line# 450).
In your case, each newly added form element has the name legto, which would cause jquery.validate to select only the first form element with that name and discard the rest wrt form validation.
Here's how you can fix this
element4.name="legto"+rowCount;
I have a page with 2 forms and a hidden field that is outside of both of the forms.
How can the hidden field be submitted with either of the forms?
I thought about doing something like this with jQuery:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
// do something to move or copy the
// hidden field to the submitting form
});
});
</script>
Will this work? Any other ideas?
EDIT
The hidden field stores a serialized object graph that doesn't change. Both server methods require the object. The serialized object string can get pretty hefty, so I don't want this thing sent down from the server 2 times.
You can simply inject a copy of the element into each form right before submission.
This way, you can have the option of having different information for each hidden form field without affecting the other.
Something like this:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$("#hidden_element").clone().appendTo(this);
});
});
</script>
If you want to use the exact same element for both forms without creating a fresh copy, just don't use clone()
See documentation for clone() and for appendTo()
EDIT:
If you don't want to send the hidden element with every request the form sends. Consider storing it in the database for that user for that time. You can submit its content once, and once only for every page reload, and then just send the database id of the hidden element with each form post.
On page load, something like this:
$.post("page.php", { reallyBigObject : $("#hiddenfield").val() }, function(insertedID){
$("#hiddenfield").val(insertedID);
});
Then, in the server side code:
//insert $_POST["reallyBigObject"] into databse
//get the just inserted id (in php it's done with mysql_insert_id())
//echo, or print the just inserted id, and exit
This way your js gets the callback.
Now, you can submit the form as you would, but this time, you're only sending the id (integer number) to the server.
You can then simply delete the object from your server (run a cron to do it after X amount of time, or send another request to delete it.
Honestly, though, unless you object is HUGE(!!), I think storing it by submitting it only once is a lot more complex to execute than to simply send two requests to the server.
Let me know if you have any other questions...
With HTML5, you can include a "form" attribute with an input element. The value of the form attribute is the id of the form(s) the field belongs to. To include the field in more than one form, include all form ids in a space-delimited list. Unfortunately, the form attribute is not supported in IE at all (as of IE 9). FF and Chrome support start in version 4 and 10 respectively.
Append the field to both forms at page load:
$(function() {
$('#form1, #form2').append($('input[name=fieldName]'));
});
Assuming you are doing a non ajax submit you could just append the field into the form being submitted. However if you need this info in both forms is it not better to store this value server side in a session store. This way any non js clients will also work!
$(function() {
$('form').submit(function() {
$('input.yourhiddenSubmit').appendTo(this);
});
});
The only way to pass the variable to the next form is to have that variable in the data that is passed when the form is submitted (either GET or POST), unless you want to use AJAX. Assuming you want to have the hidden variable outside of the actual form for a "good reason", I would recommend using jQuery to include the hidden input field into the form just before submission, like so:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$(this).append("<input type='hidden' name='hiddenField' value='"+$("#hiddenField").val()+"' />");
return true;
});
});
</script>
Replace all the instances of "hiddenField" with the ID of your hidden field outside the form. This will create a new input inside of the form just before it is submitted with the value of the hidden field that is elsewhere on the page.
Beyond that, you'd have to be a bit more specific about what your exact problem was (and why the field isn't being included in the form) for me to help.
Note that the above code should work in theory, but I didn't have a chance to actually test it out myself.