使用简单格式编辑字段时,验证不起作用

时间:2019-12-05 22:23:47

标签: ruby-on-rails ruby-on-rails-5 simple-form

我创建了一个包含一些字段的文件表单,在我的模型中,我使用了validades函数来验证输入数据,当我注册时它可以工作,但是当我要编辑数据时,例如发送一个空字段,验证是行不通的,我是Rails的新手,无法弄清楚出了什么问题。有人可以给点提示吗?

我的模型中的一些验证:

validates :name,
            presence: { message: 'El nombre no puede estar vacio' },
            format: { with: VALID_NAME_REGEX,
                      message: 'El nombre solo puede tener caracteres alfabeticos sin espacios' },
            length: { in: 4..20,
                      too_short: 'El nombre debe tener al menos 4 caracteres',
                      too_long: 'El nombre debe tener maximo 20 caracteres' },
            allow_blank: true

  validates :last_name,
            presence: { message: 'El apellido no puede estar vacio' },
            format: { with: VALID_NAME_REGEX,
                      message: 'El apellido solo puede tener caracteres alfabeticos sin espacios' },
            length: { in: 4..20,
                      too_short: 'El apellido debe tener al menos 4 caracteres',
                      too_long: 'El apellido debe tener maximo 20 caracteres' },
            allow_blank: true

  validates :identity_card,
            presence: { message: 'El numero de cédula no puede estar vacio' },
            format: { with: VALID_IDENTITY_CARD_REGEX,
                      message: 'Número de cédula inválido' },
            allow_blank: true

我的文件带有html和格式:

<div class="container">
  <div class="row">
    <div class="col-4 offset-4">
      <%= simple_form_for([:comensal, @comensal]) do |f| %>
<%= f.error_notification %>
<% if @comensal.errors.any? %>
<%= puts @comensal.errors.inspect %>
<div class="alert alert-danger">
  <div class="error_explanation">
    <ul>
      <% @comensal.errors.each do |attr,msg| %>
      <li><%= "#{msg}" if @comensal.errors[attr].first == msg %> </li>
      <% end %>
    </ul>
  </div>
</div>
<% end %>

<%if !flash.empty?%>
<div class="row">
  <div class="col-lg-12">
    <% flash.each do |key, value| %>
    <%if key == "notice"%>
    <div class="alert alert-success fade in">
      <%else%>
      <div class="alert alert-<%=key%> fade in">
        <%end%>
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <strong><%=value%></strong>
      </div>
      <% end %>
    </div>
  </div>
  <%end%>

  <%= f.input :name, label: 'Nombre', input_html: { class: 'form-control' }, label_html: { class: 'em_separator', id: 'mt_null' } %>
  <%= f.input :last_name, label: 'Apellido', input_html: { class: 'form-control' }, label_html: { class: 'em_separator' } %>

  <%= f.fields_for :user do |form_user| %>
  <%= form_user.input :phone, label: 'Teléfono', input_html: { class: 'form-control' }, label_html: { class: 'em_separator' } %>
  <% end %>
  <%= f.input :identity_card, label: 'Cédula de identidad', input_html: { class: 'form-control' }, label_html: { class: 'em_separator' } %>
  <div class="form-check-inline">
    <%= f.input :gender, label: 'Género', as: :radio_buttons, collection: [['Masculino', 'M'], ['Femenino', 'F']], input_html: { class: 'form-check-input' }, label_html: { class: 'em_separator', id: 'radio_styles' } %>
  </div>

  <span class="em_separator">Ubicación</span>
  <div class="places_container">
    <select id="estados"> 
    <option value='' >Estado</option>
    </select>

    <select id="municipios">
      <option value='' >Municipio</option>
    </select>

    <select name="place_id" id="restaurant_place_id">
      <option value='' >Parroquia</option>
    </select>
  </div>

  <div class="row">
    <div class="col-6 offset-3">
      <%= f.button :submit, value: 'Actualizar', class: 'btn-warning text-dark' %>
    </div>
  </div>

  <% end %>
    </div>
  </div>
</div>

0 个答案:

没有答案