[Ur] Integers as select box option values

Istvan Chung istvan at vivatropolis.org
Thu May 22 19:10:13 EDT 2014


On 05/22/2014 01:55 PM, David Snider wrote:
> I’m putting an integer value in the value attribute of the option tag by using the “show” in the rendering function.
> 
> <select><option value=“1”>Option 1</option></select>
> 
> However when I go to read the value back in the form handler it comes back as a string. Does Ur/Web have a way to convert strings back into integers?
> 

The [read] polymorphic function, with signature

val read : t ::: Type -> read t -> string -> option t

can do what you want: if x is an int, then [read x] will evaluate to
None if x cannot be converted to an int.


The [readError] function, on the other hand, with signature

val readError : t ::: Type -> read t -> string -> t

always returns an int, but [readError x] will throw an error if x cannot
be converted to an int.


[readError] should only be used if you are certain that the input will
be correct, so since you are getting your input from a web form, you
should use [read], not [readError], in this case.

-- 
Istvan Chung




More information about the Ur mailing list