Created
September 8, 2011 08:16
-
-
Save fmpwizard/1202908 to your computer and use it in GitHub Desktop.
Validation as you type in Lift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<form class="lift:InventoryCreate?form=post;multipart=true"> | |
<fieldset> | |
<legend><span>Main Information</span></legend> | |
<ol> | |
<li><label for="part_number">Part Number</label> | |
<input id="part_number" name="part_number" class="text" type="text" /><br> | |
<p id="part_number_error" class=""></p> | |
</li> | |
<li><label for="description">Description:</label> | |
<input id="description" name="description" size="80" class="text" type="text" /><br> | |
<p id="description_error" class=""></p> | |
</li> | |
<li><label for="category">Category:</label> | |
<input id="category" name="category" class="text" type="text" /> | |
</li> | |
<li><label for="sub_category">Sub Category:</label> | |
<input id="sub_category" name="sub_category" class="text" type="text" /> | |
</li> | |
</ol> | |
</fieldset> | |
</form> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InventoryCreate extends Logger { | |
object product extends RequestVar(Inventory.create) | |
.... //many other objects here | |
def render ={ | |
def process() ={ | |
def validateField(errorFieldId: String, field: String, text: String) ={ | |
if(text.length < 2){ | |
<p class="error" id={errorFieldId}>{field} is too short</p> | |
} else { | |
<p class="success" id={errorFieldId}>Valid</p> | |
} | |
} | |
def ajaxLiveText(value: String, func: String => JsCmd, attrs: (String, String)*): Elem = { | |
S.fmapFunc(S.SFuncHolder(func)) {funcName => | |
(attrs.foldLeft(<input id="part_number" | |
type="text" | |
value="" | |
onfocus="if (this.value == 'Enter part number') {this.value = '';}" | |
onblur="if (this.value == '') {this.value = 'Enter part number';}" | |
/>)(_ % _)) % | |
("onkeyup" -> makeAjaxCall( | |
JsRaw("'" + | |
funcName + | |
"=' + " + | |
"encodeURIComponent(this.value)") | |
) | |
) | |
} | |
} | |
} | |
"name=part_number" #> ajaxLiveText("", x => Replace( | |
"part_number_error", validateField( | |
"part_number_error", "Part Number", x))) andThen | |
"name=part_number" #> SHtml.onSubmit(product.is.part_number.set(_)) & | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment