Search code examples
scalalift

How to make a field READONLY in lift crudify


I have a field that I would like to be read-only and have a default value.

here what I have till now:

    object passwd extends MappedString(this, 20) 
    {
       override def defaultValue = "XXX" + Random.alphanumeric.take(12).mkString // some default value
//  readOnly = true // Or something similar??? 
    }

I tried overriding toHtml, asHtml, toForm, displayHtml but they all change the view or the label or something else.

I just want the defaultValue to be automatically generated (which works) but the users not to be able to edit that field when create/editing the entity.

Any advice would be very much appreciated


Solution

  • Got it,

    Just override _toForm and disable the input element:

    import net.liftweb.http.S
    
    override def _toForm =  
        S.fmapFunc({s: List[String] => this.setFromAny(s)}){name =>  
        Full(<input disabled='disabled' type='text' id={fieldId} maxlength={maxLen.toString}  
        name={name}  
        value={is match {case null => "" case s => s.toString}}/>)}