Equivalent scrapbook

…scrap on Validation ror

custom validator example :

class PossessionSelectorPresenceValidator < ActiveModel::Validator
  def validate(record)
    possession_selector = record.possession_selector

    if possession_selector.blank?
      record.errors[:possession_selector] << "Please specify viewers of #{record.class.model_name.human}"
    else
      selector_class = possession_selector.constantize
      owner_plural_form = selector_class.model_name.underscore.pluralize
      unless record.send(owner_plural_form).any?
        record.errors[owner_plural_form] << "Please specify #{selector_class.model_name.human.pluralize}"
      end
    end
  end
end

validates_with PossessionSelectorPresenceValidator

date: 2013-01-11

costum validations

     validate :authentication_exist?


     def find_user
       true
     end

     def authentication_exist?
        self.errors[:username] << "We couldn't find this email address" unless find_user.present? 
     end

should not special chars

should not contain any non word chars (like: @#$ @'..)

validates_format_of :vanity_name, :without => /\W/  , :message => 'Can contain only chars numbers and underscore'

it should be the same functionality like (should be only word chars like a-zA-Z1-9_)
validates_format_of :vanity_name, :with => /\w/ , :message => 'Can contain only chars numbers and underscore'
but this one doesn't work

validatros on

You can discover if there are any valdiators on object

User.validators
User.validators_on :column_name

error message for validanion in views

<% if target.errors.any? %>  
<div id="errorExplanation">  
  <h2><%= pluralize(target.errors.count, "error") %> prohibited this record from being saved:</h2>  
  <ul>  
  <% target.errors.full_messages.each do |msg| %>  
    <li><%= msg %></li>  
  <% end %>  
  </ul>  
</div>  
<% end %>

you can also doo this in controller for not displaying certain errors

   @user.errors.messages.shift{:authentication} # don't display this msg to user
  #p @user.errors.messages