{"id":174,"date":"2022-01-17T11:35:12","date_gmt":"2022-01-17T11:35:12","guid":{"rendered":"https:\/\/kindsonthegenius.com\/elm\/?p=174"},"modified":"2022-01-17T11:46:34","modified_gmt":"2022-01-17T11:46:34","slug":"elm-parsing-user-input","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/","title":{"rendered":"Elm &#8211; Parsing User Input"},"content":{"rendered":"<p>In <a href=\"https:\/\/kindsonthegenius.com\/elm\/elm-updating-model-with-button-click\/\" target=\"_blank\" rel=\"noopener\">the previous lesson<\/a>, we covered how to grab user input from a TextBox using a button click event.<\/p>\n<ol>\n<li><a href=\"#t1\">Update Model With User Input<\/a><\/li>\n<li><a href=\"#t2\">Custom Function for Input Parsing<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Update Model With User Input<\/strong><\/h4>\n<p>Now what we would like to do is to use the text entered in the text field to update the a field in the model. I&#8217;ll call this field score.<\/p>\n<p>So let&#8217;s follow the steps<\/p>\n<p><strong>Step 1<\/strong> &#8211; Change the init method to include two more fields in the model like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">init<\/span> <span style=\"color: #000000; font-weight: bold;\">=<\/span>\r\n    {value <span style=\"color: #000000; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">877<\/span>,\r\n    firstName <span style=\"color: #000000; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>,\r\n    score <span style=\"color: #000000; font-weight: bold;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">99<\/span>}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; Import the<em><strong> toInt()<\/strong> <\/em>function from String in the import section. This is because, we expect a number to be input.<\/p>\n<p><strong>Step 3<\/strong> &#8211; We now want to update the score when the TextChanged message is called. So we&#8217;ll modify the case section of our update method by using the | operator to assign a new score to the model but keeping the model unchanged as we learnt earlier.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">case<\/span> msg <span style=\"color: #008800; font-weight: bold;\">of<\/span>\r\n    <span style=\"color: #333399; font-weight: bold;\">Add<\/span> <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n        {model <span style=\"color: #333333;\">|<\/span> value <span style=\"color: #000000; font-weight: bold;\">=<\/span>  <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>}\r\n    <span style=\"color: #333399; font-weight: bold;\">TextChanged<\/span> newText <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n        {model <span style=\"color: #333333;\">|<\/span> score <span style=\"color: #000000; font-weight: bold;\">=<\/span> toInt newText}\r\n<\/pre>\n<p>What is happening here is that we are converting the newText to using toInt function and assigning the result to the score.<\/p>\n<p>The problem here is that toInt() returns a a type &#8216;maybe&#8217; while score if of type number.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Custom Function to Handle Input<\/strong><\/h4>\n<p>Now the user input from the TextBox when converted using <strong><em>toInt()<\/em> <\/strong>return <strong><em>&#8216;maybe&#8217;<\/em><\/strong> and the maybe could be either:<\/p>\n<p><strong>just val<\/strong> &#8211; this is returned when the conversion is successfully and the number equivalent (val) of the argument is returned.<\/p>\n<p><strong>Nothing<\/strong> &#8211; this is returned when the toInt() fails. For example, you try to convert &#8220;abc&#8221; to number using toInt()<\/p>\n<p>Therefore in this custom function we will write, will simply return 0 when Nothing is returned by the toInt() function.<\/p>\n<p>Our custom function is given below. I call it parseInput()<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0066bb; font-weight: bold;\">parseInput<\/span> text <span style=\"color: #000000; font-weight: bold;\">=<\/span> \r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span> \r\n        theMaybe <span style=\"color: #000000; font-weight: bold;\">=<\/span> toInt text\r\n    <span style=\"color: #008800; font-weight: bold;\">in<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">case<\/span> theMaybe <span style=\"color: #008800; font-weight: bold;\">of<\/span> \r\n        <span style=\"color: #333399; font-weight: bold;\">Just<\/span> val <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span> \r\n            val\r\n        <span style=\"color: #333399; font-weight: bold;\">Nothing<\/span> <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span> \r\n            <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>    \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now we can use out custom function in the update method like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">TextChanged<\/span> newText <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n    {model <span style=\"color: #333333;\">|<\/span> score <span style=\"color: #000000; font-weight: bold;\">=<\/span> parseInput newText}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally we&#8217;ll modify the Add function to add the user input number to\u00a0 the model value. You can do this like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">Add<\/span> <span style=\"color: #000000; font-weight: bold;\">-&gt;<\/span>\r\n    {model <span style=\"color: #333333;\">|<\/span> value <span style=\"color: #000000; font-weight: bold;\">=<\/span>  model<span style=\"color: #333333;\">.<\/span>value <span style=\"color: #333333;\">+<\/span> model<span style=\"color: #333333;\">.<\/span>score}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now you can go ahead to recompile the program. You will notice that when you click the button, the\u00a0 value in the TextBox is continually added to the score and displayed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous lesson, we covered how to grab user input from a TextBox using a button click event. Update Model With User Input Custom &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-elm-programming-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Elm - Parsing User Input - Elm Programming<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Elm - Parsing User Input - Elm Programming\" \/>\n<meta property=\"og:description\" content=\"In the previous lesson, we covered how to grab user input from a TextBox using a button click event. Update Model With User Input Custom &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/\" \/>\n<meta property=\"og:site_name\" content=\"Elm Programming\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-17T11:35:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-17T11:46:34+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"headline\":\"Elm &#8211; Parsing User Input\",\"datePublished\":\"2022-01-17T11:35:12+00:00\",\"dateModified\":\"2022-01-17T11:46:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/\"},\"wordCount\":367,\"commentCount\":0,\"articleSection\":[\"Elm Programming Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/\",\"name\":\"Elm - Parsing User Input - Elm Programming\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\"},\"datePublished\":\"2022-01-17T11:35:12+00:00\",\"dateModified\":\"2022-01-17T11:46:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/elm-parsing-user-input\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Elm &#8211; Parsing User Input\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/\",\"name\":\"Elm Programming\",\"description\":\"Elm Programming Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/#\\\/schema\\\/person\\\/2a5153913a1c6885206701c60aef361e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"sameAs\":[\"https:\\\/\\\/kindsonthegenius.com\\\/elm\"],\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/elm\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Elm - Parsing User Input - Elm Programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/","og_locale":"en_US","og_type":"article","og_title":"Elm - Parsing User Input - Elm Programming","og_description":"In the previous lesson, we covered how to grab user input from a TextBox using a button click event. Update Model With User Input Custom &hellip;","og_url":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/","og_site_name":"Elm Programming","article_published_time":"2022-01-17T11:35:12+00:00","article_modified_time":"2022-01-17T11:46:34+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"headline":"Elm &#8211; Parsing User Input","datePublished":"2022-01-17T11:35:12+00:00","dateModified":"2022-01-17T11:46:34+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/"},"wordCount":367,"commentCount":0,"articleSection":["Elm Programming Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/","url":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/","name":"Elm - Parsing User Input - Elm Programming","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#website"},"datePublished":"2022-01-17T11:35:12+00:00","dateModified":"2022-01-17T11:46:34+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/elm\/elm-parsing-user-input\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/elm\/"},{"@type":"ListItem","position":2,"name":"Elm &#8211; Parsing User Input"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/elm\/#website","url":"https:\/\/kindsonthegenius.com\/elm\/","name":"Elm Programming","description":"Elm Programming Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/elm\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/elm\/#\/schema\/person\/2a5153913a1c6885206701c60aef361e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"sameAs":["https:\/\/kindsonthegenius.com\/elm"],"url":"https:\/\/kindsonthegenius.com\/elm\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":6,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":199,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/posts\/174\/revisions\/199"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/elm\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}