Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions

How to set a Textfield to Read Only?


Reply

8 Posts   7019 Views

Avatar
Tama

30 August 2010 at 1:46pm (Last edited: 30 August 2010 1:46pm), Community Member, 130 Posts

Howdy

I'm trying to set a text field in a form to read-only so it cannot be edited by the user. The HTML for this would be

readonly="readonly"

I still want a standard input form field displayed so I can populate it using Javascript.

The code I'm using is:

$TotalAmountField = new TextField('TotalAmount', 'Total To Pay (NZD)', '' );
$TotalAmountField -> performReadonlyTransformation();

$fields = new FieldSet(
new TextField('FirstName', 'Your First Name', '' ),
new TextField('LastName', 'Your Last Name', '' ),
$TotalAmountField
);

This doesn't seem to work and after quiet a bit of digging through documentation I'm still not sure what to do.

If anyone could give me the correct solution to this it'd be appreciated.

Tama

Avatar
Willr

30 August 2010 at 7:54pm Forum Moderator, 5511 Posts

For cases like this try the api docs rather than the standard documentation (http://api.silverstripe.org/2.4/forms/core/FormField.html#methodperformReadonlyTransformation)

The function returns a read only field so you should do...

$TotalAmountField = $TotalAmountField->performReadonlyTransformation();

Avatar
Tama

2 September 2010 at 12:27pm Community Member, 130 Posts

Thank you for that Will - much appreciated.

Avatar
Tama

6 September 2010 at 12:53pm Community Member, 130 Posts

Oh bugger, that didn't go as I expect it to. I'm trying to set a text field in a form to read-only so it cannot be edited by the user. The HTML for this would be

readonly="readonly"

I still want a standard input form field displayed so I can populate it using Javascript.

Using

$TotalAmountField = $TotalAmountField -> performReadonlyTransformation();

I don't get a standard input field. Just a span like this:

<span id="Form_PaymentForm_TotalAmount" class="readonly "><i>(none)</i></span>

Avatar
Devlin

16 September 2010 at 12:14am (Last edited: 16 September 2010 12:17am), Community Member, 215 Posts

If you are using readonly mode you also get an hidden input field:

<span id="Form_PaymentForm_TotalAmount" class="readonly "><i>(none)</i></span>
<input type="hidden" name="TotalAmount" value=""/>

I use this to populate via JS:

function changeField(form,id,value) {
   jQuery("#form_"+form+"_"+id).html(value);
   jQuery("input[name='"+id+"']").val(value);
}

If you want explicity use a readonly text field, you probably have to write your own class of the text field. Something like:

class TextField_Readonly extends ReadonlyField{
   /**
    * overloaded to display the correctly formated value for this datatype
    */
   function Field() {
      $valforInput = $this->value ? Convert::raw2att($this->value) : "";
      return "<input type=\"text\" name=\"".$this->name."\" value=\"".$valforInput."\" readonly=\"readonly\" />";
   }
   /**
    * This already is a readonly field.
    */
   function performReadonlyTransformation() {
      return clone $this;
   }
}

Avatar
Tama

16 September 2010 at 8:43am Community Member, 130 Posts

Hi Jay - thanks for that.

In the end I put them in as normal TextFields and used jQuery to set the "readonly" attribute:

jQuery('#Form_PaymentForm_TotalAmount').attr('readonly', true);

There's a whole bunch of jQuery happening against this form around custom validation so this approach works well.

Avatar
Martijn

16 September 2010 at 12:37pm Community Member, 271 Posts

Another way is to set the Field to Disabled.

$Name = new TextField('Name', 'Name');

$Name->setDisabled(true);

Avatar
merrick_sd

18 August 2012 at 2:26am Community Member, 95 Posts

maybe this should be a separate post. (anyway)

I have found that if i do

$currentratetrigger = new HiddenField('CurrentRateTrigger', '', 2);
$currentratetrigger = $currentratetrigger -> performReadonlyTransformation();

I see in the html rendered source code there is indeed a hidden field called CurrentRateTrigger
However when i come to save to my database it doesn't get saved.

where as if i do

$currentratetrigger = new HiddenField('CurrentRateTrigger', '', 2);

it will be saved

   $bookingeventorder = New BookingEventOrder();

   $fields = array(
   'UniqueOrderID',
   'CurrentPrice',
   'CurrentMuliRate',
   'CurrentRateTrigger',
   'BookingEventAmount',
      );
   
   $form->saveInto($bookingeventorder, $fields);
   $bookingeventorder->write();

thought i'd share that