onvalidation

59 views
Skip to first unread message

Michael Herman

unread,
Apr 15, 2013, 2:34:48 PM4/15/13
to web...@googlegroups.com
I'm slightly confused on how to make a calculation and then update a SQL field after a form is validated.


Essentially, the end_date field is dependent on the data inputed into the other two fields, start_date and length. So once the form is validated, a calculation is triggered then the data is inserted into the database for the end_date field.

Does that make sense? 

I'm also using the SQLFORM.grid default view for adding records.


Thanks!

Niphlod

unread,
Apr 15, 2013, 2:52:14 PM4/15/13
to web...@googlegroups.com
either with onvalidate or with a computed field .... read it on the book ^_^
http://web2py.com/books/default/chapter/29/06#Computed-fields

Michael Herman

unread,
Apr 15, 2013, 3:33:34 PM4/15/13
to web...@googlegroups.com
Basic example:

db.define_table('contact',
Field('contract_start','date'),
Field('contract_length','string'),
Field('contract_renewal','date', writable=True, readable=True,
compute=lambda r: (r['contract_start']+r['contract_length']))

db.contact.contract_start.requires = IS_DATE(format=T('%m-%d-%Y'),
error_message='Must be MM-DD-YYYY!')
db.contact.contract_renewal.requires = IS_DATE(format=T('%m-%d-%Y'),
error_message='Must be MM-DD-YYYY!')
db.contact.contract_length.requires = IS_IN_SET(['6 months', '1 year',
'2 year's, '3 years'])


Thanks. As far as the DAL goes, I am using SQLite, and I know I can do
something like this to add a specific time period to a date by using
the following statement -

INSERT date('now','+10 day');

Is there a DAL equivalent? I didn't see anything in the book. Seems
like this would be too low of a function to be part of the DAL.
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/web2py/f-14OLOhKpM/unsubscribe?hl=en.
> To unsubscribe from this group and all its topics, send an email to
> web2py+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Michael Herman

unread,
Apr 16, 2013, 2:34:45 AM4/16/13
to web...@googlegroups.com
The logic makes sense in Rails. Something isn't quite clicking with DAL. 

class Event < ActiveRecord::Base
  # Sets the end_date to be +duration+ months after the start_date
  def duration=(duration)
    self.end_date = self.start_date + duration.to_i.months
  end
end

<%= form_for(@event) do |f| %>
  <%= f.date_select :start_date %>
  <%= f.select :duration, [["6 months", 6], ["1 year", 12], ["2 years", 24]] %>
  <%= f.submit %>
<% end %>

Niphlod

unread,
Apr 16, 2013, 3:22:36 AM4/16/13
to web...@googlegroups.com
IS_IN_SET({6 : "6 months", 12 : "1 year", 24: "2 years"}) for your contract_length and then

contract_start + datetime.timedelta(days=contract_length*30)  
Reply all
Reply to author
Forward
0 new messages