1

pls anyone help me to do validation in array of object...weightage is my field name..i just want to validate(presence and numericality) the weightage.but i could not get any validaation errors..i dont know at whivh place i did the mistake..if anyone knows the solution pls let me know

This is my view:

<tbody>             
            <% if @company_column_master != nil then                            
                @company_column_master.each.with_index(0) do |assess,index| %>
                  <tr> 
                    <td><%= index+1 %> 
                        <%= hidden_field_tag :company_column_master_id ,assess.attributes["company_column_master_id"],name: 'cd_column_id[]' %>
                        <%= hidden_field_tag :data_element_id ,assess.attributes["data_element_id"],name: 'data_element_id[]' %>
                        <%= hidden_field_tag :display_order ,assess.attributes["display_order"],name: 'display_order[]' %>
                    </td>       
                    <td><%= text_field_tag "display_name", assess.attributes["display_name"], name: 'display_name[]', size:50 ,:autocomplete => :off%></td>
                    <td><%= text_field_tag "weightage", assess.attributes["weightage"] ,name: 'weightage[]' ,:autocomplete => :off%></td>                                           #getting values from db using array
                    <td class="text-center">
                        <%= check_box_tag('status'+index.to_s,'1',if (assess.attributes["status"] == 1) then true end, class: 'js-switch') %>
                    </td>
                  </tr>
                <%end%>
                <%= hidden_field_tag :phase_id ,@company_column_master[0].attributes["cd_phase_id"]%>
                <%= hidden_field_tag :phase_detail_id ,@company_column_master[0].attributes["cd_phase_detail_id"]%>         
            <%end%>
        </tbody>

This is my model part:

class CompanyColumnMaster < ActiveRecord::Base
  validates :weightage, :presence => {:message => 'cannot be Blank!'}, :format => {:with => /\A[0-9]+\z/, message: "may only contain numbers."}, 
  :length => {:maximum => 50, :message => 'Exceeds Maximum number of numbers.'}

  validate :check_weightage

 def check_weightage
   if weightage!=nil && weightage < 0
     errors.add(:weightage, "should be greater than or equal to zero")
   end
 end
end
2
  • You are messing with the validations.May be should have at these Guides Commented Nov 20, 2014 at 9:52
  • Sorry i do not understand what you are saying.. Commented Nov 20, 2014 at 10:18

1 Answer 1

2

You are doing 2 thing at the same time - you use Rails' validates and custom validate. Choose one.

In order to use custom validation you have to write the method (what you've done almost Ok):

validate :check_weightage

def check_weightage
   errors.add(:weightage, "should be greater than or equal to zero") unless (weightage && weightage >= 0 && weightage.is_a? Integer)
end
Sign up to request clarification or add additional context in comments.

12 Comments

yeah i have tried this too..but really am not getting solution. or Should i use [] in model too?? like def check_weightage errors.add(:weightage[], "should be greater than or equal to zero") unless (weightage[] && weightage[] >= 0 && weightage.is_a? Integer) because am displaying the weightage values using array right?? so that am asking end
what I've wrote should work, but you have to remove validates line
i have removed that line and wrote your code..but it shows unexpected constant" error
i have got the error in my model "Unexpected tConstant"in line 22
and which line is that? does it intercepts with the method I've written?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.