web2py - A single database value across multiple html input tags -
is possible have single database value (say, post/zip code) split across 2 html input tags (the first 4 digits in first, , last 3 in second).
i using helper method of displaying form, far i'm aware, use 'raw' html achieve same thing (and if case, not mind @ changing view file have raw html). there way can use raw html, have 2 input tags postcode, , somehow combine values in controller?
so model file:
db.define_table( 'user_address', field('street_address', 'string', requires=is_not_empty()), field('city', 'string', requires=is_not_empty()), field('country', 'string', requires=is_not_empty()), field('postcode', 'string', requires=is_length(7)), )
this controller:
def new(): form = sqlform(db.user_address) if form.accepts(request,session): response.flash = "form accepted" return dict(form = form)
and view:
<section> <h2>register</h2> {{=form.custom.begin}} {{=form.custom.widget.street_address}} <br> {{=form.custom.widget.city}} <br> {{=form.custom.widget.post_code}} <br> {{=form.custom.widget.country}} <br><br> {{=form.custom.submit}} {{=form.custom.end}} </section>
if want use built-in form processing functionality, breaking field 2 inputs complicated. simpler approach might include 2 additional fields in database table , making combined "postcode" field computed field:
db.define_table('user_address', field('street_address', 'string', requires=is_not_empty()), field('city', 'string', requires=is_not_empty()), field('country', 'string', requires=is_not_empty()), field('postcode1', label='postal code', requires=is_length(4)), field('postcode2', requires=is_length(3)), field('postcode', compute=lambda r: r.postcode1 + r.postcode2, readable=false, writable=false)
you still need use custom form code can keep postcode1 , postcode2 inputs inline , suppress label postcode2.
the default table definition above show postcode1 , postcode2 fields , hide postcode field (i.e., in forms , grid). if displaying record (e.g., using grid feature), can instead set readable
, writable
attributes of postcode1 , postcode2 false
, postcode attributes true
.
one other option define custom widget "postcode" field. widget should include 2 visible input elements want display in form hidden input element name "postcode". need javascript (which included in widget code or loaded separately) automatically concatenate values in 2 visible inputs , put result in hidden input. widget code have take existing value in postcode field , split 2 visible widgets (this necessary update forms, display existing values in of form inputs).
Comments
Post a Comment