Tuesday, May 26, 2009

Insert At Cursor -- Javascript


function insertAtCursor(myField, myValue) {

//IE support

if (document.selection) {

myField.focus();

sel = document.selection.createRange();

sel.text = myValue;

}

//MOZILLA/NETSCAPE support

else if (myField.selectionStart || myField.selectionStart == '0') {

var startPos = myField.selectionStart;

var endPos = myField.selectionEnd;

myField.value = myField.value.substring(0, startPos)

+ myValue

+ myField.value.substring(endPos, myField.value.length);

} else {

myField.value += myValue;

}

}
// calling the function

insertAtCursor(document.formName.fieldName, 'this value');

Friday, May 15, 2009

Different ways of Validating Date of Birth in Model

def validate
if (date_of_birth.month==2 && date_of_birth.month.day > 29)
errors.add(:birthdate, "Invalida date")
end
end

OR

validates_each :date_of_birth do |record, attr, value|
record.errors.add attr, "is not a valid date. You must be at least 18 years old to sign in." if value.blank?
end

OR

def validate
if date_of_birth.month.blank? || date_of_birth.day.blank? || date_of_birth.year.blank?
errors.add_to_base "day, month and year cannot be blank."
end
end

Validates_presence_of : customise field name

validates_presence_of :id_country, :message => "Country can't be blank"

class Adresse < ActiveRecord::Base

HUMANIZED_COLLUMNS = {:id_country => "Country"}

def self.human_attribute_name(attribute)
HUMANIZED_COLLUMNS[attribute.to_sym] || super
end
end