Problema con validates_format_of

Hola a todos de la lista…

en mi proyecto quiero que antes de guardar valido el formato de un campo
fecha, esto es lo que tengo

validates_format_of :fecdurcom_at, :message => “^ El Formato del campo
Vigente hasta es Inválido”, :with =>
/\A(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))--)$)\Z/i

yo no se mucho de regular expresion pero eso lo consegui de una pagina:

pero no me funciona no me deja grabar me da error

algun guru de perl que me pueda ayudar?

muchas gracias

On Wed, Aug 20, 2008 at 1:15 AM, Manuel P.
[email protected] wrote:

en mi proyecto quiero que antes de guardar valido el formato de un campo
fecha, esto es lo que tengo

validates_format_of :fecdurcom_at, :message => “^ El Formato del campo
Vigente hasta es Inválido”, :with =>
/\A(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))--)$)\Z/i

Eso es tremendo pollo.

Como es el flujo de entrada de fechas en tu app? En que momento se
pasan de String a Date?

Por otro lado comentarte que por convenio un campo *_at en Rails es un
datetime, las fechas se guardan en campos *_on.

2008/8/20 Xavier N. [email protected]:

Eso es tremendo pollo.

Como es el flujo de entrada de fechas en tu app? En que momento se
pasan de String a Date?

Por otro lado comentarte que por convenio un campo *_at en Rails es un
datetime, las fechas se guardan en campos *_on.

Sí, tremendo pollo, cuando se puede utilizar a Ruby para ello:
http://apidock.com/ruby/Date/strptime/class

class MiClase
validate :validates_fecdurcom_date_format
private
def validates_fecdurcom_date_format
errors.add(:fecdurcom, “el mensaje”) unless parse_date(fecdurcom)
end

la magia viene a continuación

def parse_date(date, formats = [‘%d-%m-%Y’, ‘%d-%m-%y’])
formats.each do |format|
begin
Date.strptime(date, format)
return true
rescue ArgumentError
# nada aquí
end
end
return false
end
end

Eso debería funcionar con fechas como las que debería validar tu RegEx.

Suerte

gracias daniel por responder pero no me funciona me da el siguiente
error

TypeError in ComisariosController#update

can’t dup NilClass

RAILS_ROOT: /home/mwp/Proyectos MWP/sistema_rnc
Application Trace http://localhost:3000/comisarios/4# | Framework
Tracehttp://localhost:3000/comisarios/4#| Full
Trace http://localhost:3000/comisarios/4#

/usr/lib/ruby/1.8/date/format.rb:583:in dup' /usr/lib/ruby/1.8/date/format.rb:583:in _strptime’
/usr/lib/ruby/1.8/date.rb:954:in strptime' app/models/comisario.rb:44:in parse_date’
app/models/comisario.rb:42:in each' app/models/comisario.rb:42:in parse_date’
app/models/comisario.rb:38:in validar_formatdate' app/controllers/comisarios_controller.rb:64:in update’
app/controllers/comisarios_controller.rb:63:in update' /usr/bin/mongrel_rails:128:in run’
/usr/bin/mongrel_rails:244

/usr/lib/ruby/1.8/date/format.rb:583:in dup' /usr/lib/ruby/1.8/date/format.rb:583:in _strptime’
/usr/lib/ruby/1.8/date.rb:954:in strptime' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:1000:in send’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:1000:in
run_validations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:998:in each’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:998:in
run_validations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:962:in valid_without_callbacks?’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/callbacks.rb:273:in
valid?' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:933:in save_without_transactions’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:80:in
transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:100:in transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:120:in rollback_active_record_state!’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2034:in update_attributes’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/mime_responds.rb:106:in
call' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/mime_responds.rb:106:in respond_to’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in perform_action_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in
call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in perform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in measure’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in perform_action_without_caching’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in
perform_action' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in cache’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in
cache' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in perform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in process_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in process’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in
process' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in handle_request’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in
dispatch' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in dispatch_cgi’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in
dispatch' /usr/lib/ruby/1.8/mongrel/rails.rb:78:in process’
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in synchronize' /usr/lib/ruby/1.8/mongrel/rails.rb:76:in process’
/usr/lib/ruby/1.8/mongrel.rb:618:in process_client' /usr/lib/ruby/1.8/mongrel.rb:617:in each’
/usr/lib/ruby/1.8/mongrel.rb:617:in process_client' /usr/lib/ruby/1.8/mongrel.rb:736:in run’
/usr/lib/ruby/1.8/mongrel.rb:736:in initialize' /usr/lib/ruby/1.8/mongrel.rb:736:in new’
/usr/lib/ruby/1.8/mongrel.rb:736:in run' /usr/lib/ruby/1.8/mongrel.rb:720:in initialize’
/usr/lib/ruby/1.8/mongrel.rb:720:in new' /usr/lib/ruby/1.8/mongrel.rb:720:in run’
/usr/lib/ruby/1.8/mongrel/configurator.rb:271:in run' /usr/lib/ruby/1.8/mongrel/configurator.rb:270:in each’
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in run' /usr/lib/ruby/1.8/mongrel/command.rb:211:in run’

/usr/lib/ruby/1.8/date/format.rb:583:in dup' /usr/lib/ruby/1.8/date/format.rb:583:in _strptime’
/usr/lib/ruby/1.8/date.rb:954:in strptime' app/models/comisario.rb:44:in parse_date’
app/models/comisario.rb:42:in each' app/models/comisario.rb:42:in parse_date’
app/models/comisario.rb:38:in validar_formatdate' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:1000:in send’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:1000:in
run_validations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:998:in each’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:998:in
run_validations' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:962:in valid_without_callbacks?’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/callbacks.rb:273:in
valid?' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/validations.rb:933:in save_without_transactions’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:80:in
transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:100:in transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:120:in rollback_active_record_state!’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/transactions.rb:108:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2034:in update_attributes’
app/controllers/comisarios_controller.rb:64:in update' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/mime_responds.rb:106:in call’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/mime_responds.rb:106:in
respond_to' app/controllers/comisarios_controller.rb:63:in update’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158:in perform_action_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:697:in
call_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:689:in perform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in measure’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:68:in
perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/rescue.rb:199:in perform_action_without_caching’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:678:in
perform_action' /usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in cache’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/query_cache.rb:8:in
cache' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/caching.rb:677:in perform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:524:in process_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/filters.rb:685:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/session_management.rb:123:in process’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388:in
process' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171:in handle_request’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115:in
dispatch' /usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:126:in dispatch_cgi’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:9:in
dispatch' /usr/lib/ruby/1.8/mongrel/rails.rb:78:in process’
/usr/lib/ruby/1.8/mongrel/rails.rb:76:in synchronize' /usr/lib/ruby/1.8/mongrel/rails.rb:76:in process’
/usr/lib/ruby/1.8/mongrel.rb:618:in process_client' /usr/lib/ruby/1.8/mongrel.rb:617:in each’
/usr/lib/ruby/1.8/mongrel.rb:617:in process_client' /usr/lib/ruby/1.8/mongrel.rb:736:in run’
/usr/lib/ruby/1.8/mongrel.rb:736:in initialize' /usr/lib/ruby/1.8/mongrel.rb:736:in new’
/usr/lib/ruby/1.8/mongrel.rb:736:in run' /usr/lib/ruby/1.8/mongrel.rb:720:in initialize’
/usr/lib/ruby/1.8/mongrel.rb:720:in new' /usr/lib/ruby/1.8/mongrel.rb:720:in run’
/usr/lib/ruby/1.8/mongrel/configurator.rb:271:in run' /usr/lib/ruby/1.8/mongrel/configurator.rb:270:in each’
/usr/lib/ruby/1.8/mongrel/configurator.rb:270:in run' /usr/bin/mongrel_rails:128:in run’
/usr/lib/ruby/1.8/mongrel/command.rb:211:in `run’
/usr/bin/mongrel_rails:244

Request

Parameters:

{“commit”=>“Guardar”,
“_method”=>“put”,
“authenticity_token”=>“c1f5b1a382889f913bb979cf3589464f7222f47c”,
“id”=>“4”,
“comisario”=>{“cpc”=>“12847”,
“cedcom”=>“12852047”,
“nomcom”=>“Indira Carolina”,
“apecom”=>“Yanez Rodriguez”,
“proceso_id”=>“2”,
“fecdurcom_on”=>“15/08/2020”}}

El 20 de agosto de 2008 20:29, Daniel R. Troitiño
<[email protected]

escribió:

On Wed, Aug 20, 2008 at 16:40, Manuel P.
[email protected] wrote:

gracias daniel por responder pero no me funciona me da el siguiente error

TypeError in ComisariosController#update

can’t dup NilClass

RAILS_ROOT: /home/mwp/Proyectos MWP/sistema_rnc

Eso es que le estás pasando un valor nulo al parse_date. Deberías
comprobarlo antes de enviarlo:

def validates_fecdurcom_date_format
unless fecdurcom.nil?
errors.add(:fecdurcom, “el mensaje”) unless parse_date(fecdurcom)
end
end

Suerte.