giovaZ
giovaZ

Reputation: 1470

Rails : Check if field is blank on submit

In a rails application i've a search field that i must control (on the submit action) if it's blank. This one is not connected to a table for register some data:

 <%= form_tag products_path, :method => 'get' do %>
  <%= text_field_tag :search, params[:search]%>
  <%= submit_tag "Ricerca" %>   
 <% end %>

i've tried define an action in my controller for check the value of the parameters that i pass:

if !(params[:search].present?)
  redirect_to root_path, error: 'Insert a research key'
else
  @count = Product.search(params[:search]).count

  if @count == 0
    redirect_to root_path, error: 'No data found for your search'
  else
    @products = Product.search(params[:search])
  end
end

Any idea for the validation of my field throught Rails?

Upvotes: 1

Views: 8168

Answers (3)

Akshay Borade
Akshay Borade

Reputation: 2472

Use ActiveModel for a tableless model with validations.

the model:

class ExampleSearch
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :input

  validates_presence_of :input
  validates_length_of :input, :maximum => 500

end

and your form:

<%= form_for ExampleSearch.new(), :url=>posts_path, :method=>:get, :validate=>true do |f| %>
  <p>
    <%= f.label :input %><br />
    <%= f.text_field :input, required: true %>
  </p>
  <p><%= f.submit "Search" %></p>
<% end %>

For good user experience use gem 'client_side_validations'

Info on ActiveModel:

http://railscasts.com/episodes/219-active-model

Upvotes: 3

jcuenod
jcuenod

Reputation: 58375

You can use HTML5 validations for the client side (you should still do a server side check):

<%= form_tag products_path, :method => 'get' do %>
  <%= text_field_tag :search, params[:search], required: true %>
  <%= submit_tag "Ricerca" %>   
<% end %>

:required => true will require that there be something in the search field.

Upvotes: 3

Clark
Clark

Reputation: 616

You can validate both server side, and client side. You're going to want server side always, as the url can be accessed without using the form, and you need a way to handle that. Client side will make the user experience better, as they don't need to reload the page for feedback.

For server side it's as easy as if params[:search].blank? this will check for both = nil and = "".

For client side there are 2 main ways. Javascript and HTML 5. With HTML 5 you can add :required => true to your form elements, and that's all you need. Using javascript, or in this case JQuery it could work something like this

$('form').submit(function() {  //When a form is submitted...
  $('input').each(function() { //Check each input...
    if ($(this).val() == "") { //To see if it is empty...
      alert("Missing field");//Say that it is
      return false;            //Don't submit the form
    }
  });
  return;                      //If we made it this far, all is well, submit the form
});

Upvotes: 2

Related Questions