Dahdahm
Dahdahm

Reputation: 513

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

Upvotes: 2

Views: 219

Answers (2)

Dahdahm
Dahdahm

Reputation: 513

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}}/>)}

Upvotes: 2

SHildebrandt
SHildebrandt

Reputation: 316

The trait MappedField defines the methods writePermission_? and readPermission_?:

/**
 * Given the current execution state, can the field be written?
 */
def writePermission_? = false
/**
 * Given the current execution state, can the field be read?
 */
def readPermission_? = false

So you could just override these with

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

Is that what you're looking for?

Upvotes: 1

Related Questions