Good morning, how could I validate a text_field in real time with js in rails? I have my validates in my model, and the validations and view of errors per field are made post submit, but how do I validate in the field itself without having to submit submit before. regards [updated] I tried the gem Judge, but it does not work, I leave the code:
#views/employees/new.html.erb
<div class = "panel panel-primary">
<div class = "panel-heading">
<h3> Registro de Empleado</h3>
</div>
<%= render 'form', employee: @employee %>
</div>
#views/employees/_form.html.erb
<%= form_for(employee, :builder => Judge::FormBuilder) do |f| %>
<div class="row row-no-top">
<div class="col-md-6">
<div class="field">
<div class="form-group <%= @employee.errors[:legajo].any? ? " has-error has-feedback" : "" %>">
<h1><%= f.label :legajo, "Legajo" %></h1>
<%= f.text_field :legajo, :validate => true, autofocus: true,requiered:true, :class=>"form-control input-sm " ,placeholder: "Legajo" %>
<%if @employee.errors[:legajo].any? %>
<span class="glyphicon form-control-feedback"><i class="fa fa-exclamation" aria-hidden="true"></i>
</span>
<p class="bg-warning"><%= @employee.errors[:legajo].to_s[/\w([^"]*)/] %></p>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
#models/employee
class Employee < ApplicationRecord
validates :legajo, :name, :last_name,:document_number, presence: { message: "Campo obligatorio" }
validates :legajo, uniqueness: { message: " El legajo ya existe" }, on: :create
validates :legajo, numericality: { message: " Se permite solo numeros enteros",only_integer: true }
validates :legajo, length: { is: 5, :message => "Debe tener 5 digitos" }
validates :legajo, uniqueness: { message: " El legajo ya existe" }, on: :update
end
#assets/javascript/application.js
//= require underscore
//= require json2
//= require judge
...
#config/routes
Rails.application.routes.draw do
mount Judge::Engine => '/judge'
...
end