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   7363 Views

Avatar
Tama

Community Member, 132 Posts

30 August 2010 at 1:46pm

Edited: 30/08/2010 1:46pm

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

Forum Moderator, 5513 Posts

30 August 2010 at 7:54pm

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

Community Member, 132 Posts

2 September 2010 at 12:27pm

Thank you for that Will - much appreciated.

Avatar
Tama

Community Member, 132 Posts

6 September 2010 at 12:53pm

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

Community Member, 217 Posts

16 September 2010 at 12:14am

Edited: 16/09/2010 12:17am

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

Community Member, 132 Posts

16 September 2010 at 8:43am

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

Community Member, 271 Posts

16 September 2010 at 12:37pm

Another way is to set the Field to Disabled.

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

$Name->setDisabled(true);

Avatar
merrick_sd

Community Member, 97 Posts

18 August 2012 at 2:26am

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