Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » How to set a Textfield to Read Only?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 6732 Views
  • Tama
    Avatar
    Community Member
    130 Posts

    How to set a Textfield to Read Only? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    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();

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    Thank you for that Will - much appreciated.

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    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>

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    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;
       }
    }

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    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.

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    Another way is to set the Field to Disabled.

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

    $Name->setDisabled(true);

  • merrick_sd
    Avatar
    Community Member
    95 Posts

    Re: How to set a Textfield to Read Only? Link to this post

    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
       

    6732 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.