开发者

How to give user live feedback for Rails form

开发者 https://www.devze.com 2023-01-20 07:29 出处:网络
I need to make a Rails form that responds back to the user live as they input fields. Specifically, the user will need to input four decimal fields representing data from some tests our company runs.

I need to make a Rails form that responds back to the user live as they input fields.

Specifically, the user will need to input four decimal fields representing data from some tests our company runs. The problem is that the testers often input incorrectly (leave out a digit, or hit a '4' instead of a '1', etc).

This is easy to check, as in real life, the inputs shouldn't vary by more than 5. I would just like to send a live message back as they input saying something like "Please double check there开发者_如何学C is no mistake in your input" if the fields vary by certain conditions.

I looked at many live validations tutorials, including the one in Advanced Rails Recipes, but a) i don't need a server request for this, and b) this is not technically a validation since I only want to warn the user of the input, not prevent them from making it.


You can do this with some javascript by attaching onchange to the input fields that you want to warn users for as they're filling out the form. This way, as they move to the next field in the form, your event handler gets called, checks the value of the form field they just inputed information for, and decides whether or not to alert the user with a warning message. You wouldn't have to hit the server at all.

An example using jQuery:

$('#your_form_field').change(function () {
  var isValid = false; // Validate the input's value based on some criteria
  if (!isValid) {
    alert("Please check your work.");
  }
});


AJAX with RJS RailsCast could help. You could write RJS that is returned once from server and then executed N times on events on the client.

0

精彩评论

暂无评论...
验证码 换一张
取 消