react-hook-form only validate on submit - react-hook-form

How can I prevent react-hook-form from continuing to validate after it's failed validation. Take the following example.
I press submit with no value in the input. Required error is triggered.
I then paste a value into the input field that do not pass the pattern validation, at this point I want it to only validate again after submit it pressed but it validates immediately.
Imagine I needed to post the value to the backend for validation I wouldnt want it to post every time I added a new value or pasted a value in the input.
Here's my sandbox. https://codesandbox.io/s/clever-shape-r37j9y?file=/src/App.tsx:74-89

You can change default validation mode by using mode and reValidateMode. please check https://react-hook-form.com/api/useform for more details. In your codesandbox, use useformas below. tested and working fine.
...
const { handleSubmit, control } = useForm({
mode: "onSubmit",
reValidateMode: "onSubmit",
});
...

Related

BigDecimalField validation bug

BigDecimalField has a nice feature: it allows only digits, "+" and "-" signs, and a decimal separator defined by the current locale ("." or ","). It works fine until someone tries to enter a combination that is not a number or cannot be directly converted to the java BigDecimal type (e.g "123...45.6+++7-89--").
For sach combinations, the validation passes without any warning, and the background business object gets a null value.
Field is in the dialog and every time before opening the dialog there is a call to Binder.readBean() method (in order to set the appropriate data for editing), on "save" button in dialog I call Binder.writeBean(). Problem is that the next time I open the dialog, the problematic BigDecimalField still contains an invalid number entered,
actually Binder.readBean() stops working for that field.
There is a similar bug with IntegerField which I noticed: IntegerField validation bug.
How can I validate BigDecimalField and avoid this bug? How do i get the BigDecimalField to work again and not keep showing invalid data? Is there a elegant way to catch the NumberFormatException that probably occurs somewhere inside the vaadin api, and proces it i.e. warn the user that he has to enter a valid decimal number. Is there a way to do it via Binder?
I am using Vaadin 23.3.0
Checking format error is not yet by default on. You need to enable it by setting enforceFieldValidation=true in feature flags in src/main/resources/vaadin-featureflags.properties file.
com.vaadin.experimental.enforceFieldValidation=true
See more at: https://github.com/vaadin/platform/issues/3066

web2py pass parameters to controler and download a file

I have the following problem
On my page user can select date from and date to. This dates are to be send to controller on button click, which creates an excel file and user receives that file?
Any suggestions ??
Thank you
def excel_file():
form = SQLFORM.factory(Field('start_date', 'date'),
Field('end_date', 'date'))
if form.process(session=None).accepted:
excel_file = create_excel_file(form.vars.start_date, form.vars.end_date)
return response.stream(excel_file, filename='name_of_file.xlsx',
attachment=True)
return dict(form=form)
Note, the above sets session=None in the call to .process() in order to disable use of the _formkey hidden field (otherwise, you would only be able to submit the form once and would have to manually reload the page for a second submission). This means there is no CSRF protection, but that shouldn't be a problem here, as the form submission is just being used to request data rather than make any changes. If you need CSRF protection, you will have to implement it manually.
Also, note that excel_file can be a file-like object (such as StringIO), an open file object, or a string representing a full file path.
Alternatively, in the browser, you could add an event handler via Javascript to capture the button click and instead of allowing the form to post, call window.open() with a web2py URL that will create and serve the file (you would have to pass the values of the start and end dates from the form via the query string of the URL). You could optionally blank out the form fields after the submission.

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.

What happens inbetween clicking the button Submit and the method create in Rails?

I ask this because I have a form with a radio button set to nil :
= f.radio_button :estimate_type, nil
I have debugger right at the beginning of my method call :
def create
debugger
When I hit the debugger, I check out my params, and they say the value is on not nil.
Enter Insanity wolf. Somehow this is getting converted on click. And I've scoured the entire app looking for possibly a leaky javascript file, or anything closely resembling the word 'on'. I've checked all my bases. Defaults in schema.rb, jquery click events, model validations, you name it. Nothing with the word "on" anywhere.
So the real question is, is there a way I can throw a debugger in a place in which if I were to click submit, the debugger would appear before the model validation, and then hopefully where the params are still what they are in the form. And then I can follow it down the trail and see where it goes wrong.
It doesn't have anything to do with your JavaScript. This is something that I've experienced before as well, but I'm not sure why it converts nil to 'on'. I do know that passing in :nil as a symbol returns a null string, as well as just simply passing in false.
A better approach to trying to solve your problem may be to put the debugger in the validation callback itself.
Nothing to do with rails - you could verify this by using your browser's network inspector to see that the browser is actually sending the parameter value "on".
By trying to set the value to nil (which doesn't really make sense - parameter values are always strings) you're suppressing the value attribute entirely from the generated HTML.
The standard says that in this case the default value for the input shall be "on" and so that is what your browser submits.

How to give user live feedback for Rails form

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.

Resources