How to give user live feedback for Rails form - ruby-on-rails

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 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.

Related

When to perform additional processing on the form in Rails?

I'm writing a form where some additional processing is required before sending the values. The image below depicts the simplified example.
where test_date and remind_date are saved on the server as a string of format YYYY/MM/DD.
However, the text field shown above allows users to enter an integer. The value from this field needs to be processed (basically, test_date - form_value).
I'm currently doing this conversion before sending the value to the server, within a saveForm() function that is triggered when the Submit button is pressed.
I feel like this might not be the ideal way, and wanted to get your opinions. Would it be better to handle this within the controller, by allowing a new param?
For a simple field like this you can just create another instance variable in the controller.
For more complex calculations or additional fields you could use a form object, eg: using the https://github.com/andypike/rectify gem or Reform.

Is there any way to keep validate field optional in rails?

I have User model which includes 7 fields. for all these fields validation is written.i have two form where i am displaying fields depend on condition. in one form i have name password and city and other form i have role,phone and name.
When i try to submit the first form i got the error which says phone and role field are required resulting into failure of form.
Is there any way by which i can submit both form without getting the validation errors ??
Note : i want my logic to be in model only.. Please help me with this problem.
You could use a conditional validation to achieve what you want:
See here: http://guides.rubyonrails.org/active_record_validations.html#conditional-validation
However, this can quickly get hard to manage. Depending on the condition you're switching on, it'd probably be a cleaner design to use a 'Form Object' which will give you more control and let you do validations without the messy conditional logic.
See section #3 of this blog post for more detail:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Using this pattern, you would check for your condition in the controller then determine which form object to send to the view.

How do I copy the values of one form element to another?

This is a follow up to this questions:
How can I manipulate a form / inputs to be ignored when a form is submitted
I have the info displayed, the form, the show()s and hide()s, etc. Most everything seems to be progressing fairly well. Where I could use some input - no pun ontended - is on how to take the new values in the form and copy those to the display only div as part of the ajaxSuccess.
I can copy type=text and textarea but what about checkboxes, multi-selects and radios.
In short, once the new values in the form are submitted, I need to update the page (not as a form) with those new values. Perhaps I need to write some for each form element type? Even so, I'm not quite sure where to start.
Pardon me if my working isn't clear. If that's the case just ask what you'd like me to clarify.
The way that will make most sense is to have the script that is processing your form return the form contents in the ajaxSuccess data. Here's an example:
The javascript:
$.ajax({
url: 'ajax/process.php',
success: function(data) {
// Load returned data into page
$('#result').html(data);
}
});
The php:
var $input1 = $_POST['input1'];
var $input1 = $_POST['input1'];
// DO SOMETHING WITH VARS AND IF SUCCESSFUL RETURN
if($success){
echo "Thanks! Here is your info: ".$input1." and ".$input2;
}
Once the data has been returned you can manipulate it anyway you like. For example if you needed to separate out the different values from the form you could post a comma separated string back from the PHP and use the javascript .split() method to create an array from that string.
Hope that's helpful :)
I'd say that your best bet is to take a different approach altogether, and reload your content from the database after it's been updated - in this case via an AJAX call. The reason being that your posted content from the form may not match what ends up being saved to the database, as some of your fieldtypes may process and alter the data they're passed (for example, stripping tags, formatting text, etc). This way the refreshed content exactly matches what is displayed when the same content comes from the Channel Entries call.

How do i filter and validate form fields in symfony 1.4?

Im trying to integrate a content filtering API. My plan was to use pre/post validators but I've lost may way somehow.
What i need to do is send the values to the content filtering service. If the response comes back that the content has been filtered it will also return a modified value for the field (basic profanity filtering... matches are replace with asterisks). Thats all well and good i can throw validation errors no problem - simple stuff.
However i dont want just throw errors. What needs to happen is that validation errors are thrown as normal, but the values are modified in the form for re-display.
Basically if someone posts something naughty i want them to get a validation error saying their post has been modified, they can re-submit the now "clean" post, or they can go about editing it to make it clean without the word replacements.
But do clean on a validator either throws an error OR returns cleaned values, not both. How can i go about implementing both? This will be used on many different forms with many different field names, so modifying methods on the form or a form base class isnt really an option - it needs to happen in the validation sub-framework somehow.
You can adjust this plugin for your needs http://www.symfony-project.org/plugins/WebPurifyPlugin

how to return url in form file in strut

hai all,
i have one problem regarding to return the form file value is validation fail. i have one form that have a few field to be fullfil by the user and one attachment field.if one of the field is blank the system will give the error when submit the form..my problem is, all the field will have the value that we entered before this but for form file it disappear.
Let's see if I understand your issue: You have a form with several input fields, and among them a <input type=file> for uploading some file content. In case of validation error, you return to that page (with struts2 result = INPUT, I guess) and the content of the previously filled fields appears, except for the FILE field.
This makes sense, if you understand how the other fields are "refilled" in Struts2 in this scenario (just from the action, which has typically mapped the parameters to properties). The server doesnt know the (clientside) fullpath of the file, it is not sent along the http request (it would be a privacy issue), it's the content of the file what is uploaded (and perhaps the filename without path). You can't escape that.
Anyway, I think this a case in which validation should signal an error early (in javascript, in the client side). Think about it: the user is uploading a file (potentially large) together with other info, and AFTER uploading it the server checks the fields and instructs the user to refill the fields and retry (including the upload). This can be unacceptable. My advise is, then, to include client-side validation (and if it pass, and the server validation fails, then the user must resign to refill the FILE field). If the file is large, one could split the input form in two pages, a first one to fill the several fields, and afterwards another to upload the file.

Resources