0

Is it possible that Rails doesnt save an entry if the users input is blank and that when the user clicks on the entry key it does not redirect to the index site but skipes to the next input? Here i have an simple form, so maybe someone can tell me how he would do it! Thanks in advance!

%= form_for(@patient) do |f| %>


<div class="field">
<%= f.label :vorname %><br />
<%= f.text_field :vorname %>
</div>
<div class="field">
<%= f.label :nachnahme %><br />
<%= f.text_field :nachnahme %>
</div>
<div class="field">
<%= f.label :alter %><br />
<%= f.date_select :alter %>
</div>
<div class="field">
<%= f.label :ort %><br />
<%= f.text_field :ort %>
 </div>
 <div class="field">
<%= f.label :strasse %><br />
<%= f.text_field :strasse %>
</div>
<div class="field">
<%= f.label :telefon %><br />
<%= f.text_field :telefon %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Em Sta
  • 1,676
  • 9
  • 29
  • 43
  • 1
    of course, it is possible, if you don't validate input on presence – Joe Half Face Jun 15 '13 at 13:32
  • im beginner in rails, what would you do exactly? – Em Sta Jun 15 '13 at 13:35
  • 1
    You want the focus to go to the next field when the user pressed Enter? You can use jQuery: http://stackoverflow.com/questions/4649604/jquery-select-next-text-field-on-enter-key-press. That's a web form interface issue rather than a Rails issue. – lurker Jun 15 '13 at 15:32

2 Answers2

0

If you want some database related columns (form is generated byform_for helper) to have value for sure, than in Patient model:

validates :vorname, :nachnahme, :presence=> true

Also, you can validate length, uniqueness of record (with Rails default validators) and write your own, if you would like to.

Joe Half Face
  • 2,303
  • 1
  • 17
  • 45
  • I think you misunderstood me, what i want is that, when the input is blank he simply jumps to the next input and when the user clicks create it shouldnt create the blank columns! – Em Sta Jun 15 '13 at 13:44
  • If I got you right, this is not possible. If your Patien has table, its instance or has in column some data, or column is empty, but record of each instance can't contain different number of columns. – Joe Half Face Jun 15 '13 at 13:51
0

if i understand ... you want to :

1 - prevent user to create his account if he clicks create button with mouse and there are blank informations ?

2 - if he clicks enter button of keybord and there are blank fields he jumps to the next input without trying to save form ?

for the first answer , you can use validation in your model like this :

# force user to fill all inputs in your form
validates :vorname, :nachnahme, :ort ...... ,  :presence=> true

for the second answer, you can use javascript (i use jquery here ) to prevent 'enter keyword' to submit data, and to jumps to the next input :

$(document).ready(function(){

   var id_of_element_have_focus;
   var valid;

   $("form").submit(function(){

      id_of_element_have_focus = $("*:focus").attr("id");

      valid = true;

      $(":text, :file, :checkbox, select, textarea").each(function() {

         if($(this).val() === ""){

           valid = false;


            $("#" + id_of_element_have_focus).next().focus();

         }

      });


      return valid;

   });

});
medBouzid
  • 7,484
  • 10
  • 56
  • 86