form_with client side validation - ruby-on-rails

Im using the client side validation gem. And have ran the all the instructions as stated to get validation working, but validation doesnot work on my form.
My form:
<%= form_with(model: profile, local: true, method: :patch, multipart: true, validate: true) do |form| %>
I have put validate: true as stated on document.
Rather than put validation in my model:
validates :name, :email, presence: true, length: { maximum: 10 }, if: :can_validate?
def can_validate
true
end
The doc says you can force on the form which I have done instead:
And in my text field I have applied the validation:
<%= form.text_field :name, validate: { presence: true }, id: :profile_name, class:"input is-large" %>
But the validation does not work at all, no error message.

I created a repo to reproduce your issue myself:
https://github.com/wmavis/so_rails_client_side_validations
It looks like client_side_validations works for me if I use form_for but does not work for me if I use form_with:
A search for "client_side_validations form_with" turned up this link:
https://github.com/DavyJonesLocker/client_side_validations/issues/696
It seems like the easiest solution for you is to use form_for. If you need to use form_with, it seems like the developer may be working on a solution or have one coded on another branch.
Let me know if it works or if you need more help.

Related

Validate at least one checkbox is ticked in Rails 5

I am trying to do a server side validation in Rails 5 and I am having issues. I'm still very much a noob but this has me stumped.
I want to check to see if any of the 4 checkbox options have been ticked on saving.
In my form I have:
<%= f.input :property_type, label: 'List as', as: :check_boxes, collection: %w(Student Graduate Professional Family), checked: property.property_type %>
And I have tried
validates :property_type, acceptance: { message: 'must be selected' } and validates :property_type, presence: true neither have worked.
I've also tried to create a custom validation but I could not get that to work either.
Can anyone please help?
Custom validation should work in your case. could you please check with below snippet?
validates : property_type_is_selected
private
def property_type_is_selected
if self.property_type.blank?
self.errors.add(:property_type, 'must be selected.')
end
end

Do form validation that does not use database

I have a form and I have made some inputs required. After submitting the form that value will be sent to an API. I know that the validations are put into model file but since I do not have a database, how can I use the rails validation?
Right now I am validating the code inside a controller using if else.
if !params[:groups][:name].blank? && !params[:groups][:make].blank? && !params[:groups][:model].blank? && !params[:groups][:firmware].blank?
This does the work but it is not very elegant.
Take a look at ActiveModel, it lets you do "model things" without the database. There were some limitations that made me not use it in the end (I think related to associations) but for simple stuff it's great (and it's a part of how ActiveRecord works.
Example code from docs
class Person
include ActiveModel::Model
attr_accessor :name, :age
validates :name, :age, presence: true
end
this is easy. On the form input fields that you NEED, add required: true For example:
<%= form.for #something do |f| %>
<%= f.text_field :title, placeholder: 'Title', required: true %>
<% end %>
The user gets an error if the required fields are not filled out correctlty.
Is this what you mean?
Justin
EDIT
I guess I would look at using the gem
client_side_validations
Let us know how you go

Client side validation with judge not working

I am trying to use client side validation with Judge gem but validation not working.
In my gem file I used
# Forms
gem 'simple_form', '3.0.2'
#Client side validations
gem 'judge'
I have simlpe_form gem and I know i need to use plugin for simple form with judge but i am trying to use in normal form_for but its not working with that either.
My model looks like
class User < ActiveRecord::Base
paginates_per 10
validates :name , presence: true, length: { maximum: 200 }
In my view
- title 'User Registration'
.vcentered
.signup-box
- if #user.authorizations.any?
%h1 We just need a little more information...
- else
%h1 We just need a little bit of information...
= form_for #user,:builder => Judge::FormBuilder, url: user_registration_path, html: { class: 'new-user form-default' } do |f|
- if #user.name.blank?
= f.text_field :name, class: class_with_errors(#user, :name),
title: title_with_errors(#user, :name),
placeholder: 'Full Name',
:validate => true
I have followed all configuration stps as given on github page of gem.
Please suggest solution of the problem or how I can find the cause of problem.
in client side validation when i load the page browser is saying (at top middle, I forget what it is called)
The page at localhost: 3000 says: PAge can't be blank. Any idea why it is working like this ?
rails generate judge:install path/to/your/js/dir
and then in your route.rb
mount Judge::Engine => '/judge'

simple_form doesnt prevent sending request without required fields

i have this form
= simple_form_for #category.fields.build, url: category_fields_path(#category) do |f|
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type"
= f.input :description
= f.submit "Add field"
and this field model
class Field < ActiveRecord::Base
FIELD_TYPES = %w(integer float date string text)
validates :description, presence: true
validates :kind, presence: true
belongs_to :category
end
when i leave 'description' field empty, no request is send and i get notice 'Please fill out this field'. which is what i want. on the other hand, when description is filled in but kind is not, a request is still send to the 'create' action! No field gets created, but 'description' needs to be filled in again. there should be no request in such situation. any idea how to fix this?
Though, I don't have exact answer to Your problem, but You should start with checking HTML output. Simple from relies on HTML5 to provide front-end validation. All inputs should have required attribute, to have validation enabled. Maybe there is a bug, and in this particular case simple_form does not output required attribute.
Another thing to take in account as it is HTML5, consult browser support: http://caniuse.com/#feat=form-validation . Theoretically it's possible that You are testing on browser that has limited support for form validations.
If You discover that simple_from did not output required for Your kind fuel, try forcing it:
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type", required: true
I got my answer at Simple Form github's issue topichere. to sum up, problem was prompt, validation is not (yet?) working with it correctly, solution is to replace it, eg like this:
= f.input :kind, collection: Field::FIELD_TYPES, include_blank: "Choose field type", label: false

How to simply validate a checkbox in rails

How do you simply validate that a checkbox is checked in rails?
The checkbox is for a end user agreement. And it is located in a modal window.
Lets say i have the checkbox:
<%= check_box_tag '' %>
Where and how should i validate this?
I have seen most posts about checkbox validation in rails here, but none of them suit my needs.
Adding
validates :terms_of_service, :acceptance => true
to your model should do it. Look here for more details and options.
However, if accepting the terms is not part of a form for your model, you should use client-side validations, i.e. JavaScript, like this (in jQuery):
function validateCheckbox()
{
if( $('#checkbox').attr('checked')){
alert("you have to accept the terms first");
}
}
You can add a script file to your view like this:
<%= javascript_include_tag "my_javascipt_file" %>
and trigger the function on click:
<%= submit_tag "Submit", :onclick: "validateCheckbox();" %>
EDIT: you can assign an id to your checkbox like this: check_box_tag :checkbox. The HTML will look like this: <input id="checkbox" See these examples for more options.
I was able to skip the jQuery portion and get it validation to work with this questions help. My method is below, I'm on Rails 5.1.2 & Ruby 2.4.2.
Put this code in your slim, erb or haml; note syntax may differ slightly in each.
The line below was specifically for slim.
f.check_box :terms_of_service, required: true
I used a portion of kostja's code suggestion in the model.
validates :terms_of_service, :acceptance => true
Adding on to what has been said already, if you want to add a custom error message, you can add the following to your form:
f.input :terms_of_service, as: :boolean
and then add the following to your model:
validates :terms_of_service, acceptance: { message: "must be accepted"}
Error messages will start with the field name by default followed by your custom message (e.g. Terms of service [CUSTOM MESSAGE]). Something I also found useful was to include a link to the terms of service in the label so users can easily access it to see what they are agreeing to:
f.input :terms_of_service, as: :boolean, label: "I agree to the #{link_to "terms of service", [TERMS AND CONDITIONS PATH]}".html_safe

Resources