How to skip html5 validations when clicking on SKIP button - ruby-on-rails

I have button SUBMIT and SKIP on same page.When user click on submit all validation check and form submit that's good .
Issue is with SKIP button. How I skip validations when user click on SKIP button?

Write javascript/jquery code to remove the required attribute from the relevant fields on clicking the skip button.

In your view you want to create a reset type button, assuming you're using form_for or something that provides a local f var:
= f.button "Skip", type: :reset
Then in a javascript file that's loaded you want to attach to the click event for that button, I've written this in coffeescript (and accounting for turbolinks):
$(document).on "ready page:load", ->
$("button[type=reset]").click ->
form = $(#).closest("form")[0]
form.reset()
form.submit()
This will cause that button to both reset the form (ie. clear any values in it) and submit the form.
If JS is not enabled then the button will act like a standard HTML "reset" button (without submitting the form.

Related

Enable a Disabled Submit Button when both a Select field is selected, and Input field has a number input? Javascript

I am working on a project that has a form with a Select dropdown and an Input field with a disabled Submit button. I need the submit button to be disabled until both of these fields are filled and the the second input is a number. I am thinking of using event listener for both filed but do not know how to handle two changes in one function.
let select = document.querySelector("select#slect_items");
select.addEventListener('change', checkSelection);
let input = document.querySelector("input#number");
input.addEventListener("input", checkInput)
Next I am going to define both checkInput and Check selection.
I can get individual function working to dsiable the submit button but do not know how can I combine both eventlisteners together and enable submit button in a single function.
I saw one similar question asked using Jquery. However, I am a beginner and this project has to be coded in JavaScript.
Thanks for your time

Upload an image without displaying file_field and "apply" button. What's the correct way?

I am working in a rails 3.2 app,where the user has the avatar picture.
The user can chose his avatar in these way:
1) chose the file clicking the "Browse..." button in the edit user view (using file_field)
2) chose the file and click OK
3) then click on the "Apply Photo" button (on the right of the field containing the filename) to load the image to the server and update the view
I want improve the user experience, removing the field and the button in the view. This should be the way:
1) click a link 'Update avatar' (or directly click on the picture)
2) chose the file and click OK
No (visible) fields and no Apply button
What's the best way to do that ?
should I continue to use the file_field in the view but hide it and trigger the user#update action using some javascript code ? or is there some other better way to do that ?
Thank You
You can use one of js plugins to upload files. I.e. if you are using jQuery, you can try using plupload
I have encountered this issue with hiding the input element and putting a label instead.
For eliminating submit button, I have added onchange method.
<%= file_field_tag :file, class: "d-none", onchange: "this.form.submit()" %>
<%= label_tag :file, role: "button" do %>
<%= image_pack_tag('logo.svg') %>
<% end %>
PS: I'm using bootstrap class here, styles can be used instead for hiding the element.
The button near upload field is generated by browser and is standart widget for browser. You can use js plugin for that or realize iframe upload method (allow you async file upload even on IE). Simple tutorial for iframe - there Iframe method allow to you to customize upload link/field as you want =)

Asp.Net Mvc remote validation textbox focus issue

I have a single textbox on a form - basically to allow users to change the URL of their site in our mini CMS. We use remote validation to check that the URL is not already taken.
They enter their desired URL and hit the save button. If they do just that and the focus goes from the textbox straight to the submit button - the validation does not happen and the form doesn't submit properly. If they tap into an area of whitespace then the form does.
The issue with the form submitting is that if they tap whitespace we get the name of the submit button posted along with the desired URL - we use the (AcceptParameterAttribute) to allow us to route form submits to the right Action. This uses the submit buttons name attribute to do this. If they don't click whitespace to lose focus on the text box then only the desired URL is posted.
This is a really odd one and it is very annoying. Has anyone seen anything like this before? Is there a way to overcome the problem?
Try adding the following script to the page:
$(":input").live("blur", function () {
$(this.form).validate().element(this);
});
This should cause validation to occur when you click on the submit button. If not, try changing the first line to:
$(":submit").live("click", function () {

How can I update a hidden form field on AJAX submit in Rails 3?

Questions on Rails 3.0.7, and JQuery 1.5.1
Overview
I'm trying to do the following thing. I have a web page with a form on one side that lets me create a category, and a list of items on the other side, with a checkbox for each item. I would like to be able to check the check box, for each item, so that when I submit the form to create the category, the newly created category also creates a has_many through association with each item.
So the association part works almost, I can submit a list of checked checkboxes, and Rails creates the association on the backend if I send the list of checked items.
Problem:
Here's what doesn't work:
I am trying to submit the form via Ajax, so I wanted to bind an event handle to the rails.js 'ajax:beforeSend' event, so that it would scan through my list of checked checkboxes and at the checked ids to a hidden form field.
The problem is: I try to get a list of all the checked boxes, and add them to the hidden field when the user clicks the Submit button. Therefore, I figured I'd place the code to do so in the ajax:beforeSend event handler. What I "THINK" I'm noticing however, is that rails.js has somehow already processed the form fields, and constructed the HTTP query before this handler fires. I notice this through the following behavior:
Observed Behavior
1) I can reload a fresh page, click the button to submit the form, the alert boxes from my handler pop up saying that no boxes are checked, and the created category in my db has no associated items (CORRECT BEHAVIOR)
2) I then click 1 checkbox, the alert boxes pop up showing which boxes are checked (CORRECT). But then when I submit thee form, the category in the DB still has no associate items. And I can see through firebug and the server logs that and HTTP query was sent containing the old parameter list, not the newer one. (WRONG)
3) I then submit the same form again, changing nothing, and it shows me the correct number of items.
So I get the impression that the parameter construction is lagging. I hope this wasn't too long, an someone can help me figure out what is going on.
My Question:
What event do I need to bind to, so that the form gets submitted with the correct parameters? Or is there another way to go about it completely?
Thanks
Here's my code below.
Thanks,
$('#my_form')
/* Now we need to configure the checkboxes to create an association between
* the items, and the interaction that is being created. First we will bind a
* function to the click event, and if the box is then checked, we will add
* the item id to the list of interaction items. If it is unchecked, we will
* remove the item from the list of interaction items.
*/
.live('ajax:beforeSend', function(event){
// First get a list of all the checked Items
var checked_items = new Array();
$('#items input[type="checkbox"]:checked').each(function(){
checked_items.push(this.getAttribute('data-item-id'));
});
// Then add this list of items to the new_interaction form to be submitted
$('#interaction_new_items').val(checked_items);
alert(checked_items);
alert($('#interaction_new_items').val());
})
Bind to the click event on the submit button itself. The click event will be processed before your ajax beforeSend event.
$('#my_form .submit').click(function() {
// rest of your code here
});
A more flexible solution (say, in case you wanted to submit the form with something other than a click) would be to bind to the form's submit event.
$('#my_form').submit(function() {
// Tweak your form data here
})

How to submit without having a submit button on the form?

Can I have a submit in <% using (Ajax.BeginForm("ChangePassword", new AjaxOptions { OnComplete = "ChangePasswordComplete" })) without having a submit button on the form?
I yes, how? Let's say I want to submit the above when a user click on an input of type button simply?
You'd have to use Javascript to post back the form, jQuery has a submit method you can use.
$("#FormId").submit();
But really, an input of type button is the same as submit, only it submits automatically so you might as well use that.
Progressive Enhancement
Alternatively, you can use the jQuery Form plug-in to post a standard form (using Html.BeginForm or manually outputting the <form />) and that will work for people who don't have Javascript enabled on their browser.

Resources