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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

TextField Readonly


Reply


6 Posts   3095 Views

Avatar
TerryMiddleton

Community Member, 108 Posts

4 May 2009 at 11:03pm

I'm trying to set a textfield readonly.

I see in the documentation that I can set a TextField to readonly, however how is this implemented?

I have a custom form, but where do I put the readonly part for the field I want to make readonly?

function Form() {
      return new Form($this, "Form", new FieldSet(

         // List your fields here
         new EmailField("Email", "Email address"),
         new PasswordField("Password", "Password"),
         new TextField("FirstName", "First name"),
         new TextField("LastName", "Last name"),
         new TextField("Phone", "Phone"),
         new TextField("Company", "Company/Hospital"),
         new TextField("Address1", "Address 1"),
         new TextField("Address2", "Address 2"),
         new TextField("City", "City"),
         new TextField("State", "State"),
         new TextField("Zip", "Zip"),
         new TextField("Country", "Country"),
         new TextField("IPAdress","IPAddress",$_SERVER['REMOTE_ADDR'])
         

      ), new FieldSet(

         // List the action buttons here
         new FormAction("SignupAction", "Register")

      ), new RequiredFields(

         // List the required fields here: "Email", "FirstName"
         "Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

      ));
   }

Avatar
Double-A-Ron

Community Member, 604 Posts

4 May 2009 at 11:47pm

Edited: 04/05/2009 11:48pm

Hmm, yes nothing in the Docs on the usage of this.

I did find this inherited method however:

[url=http://api.silverstripe.com/forms/core/FormField.html#methodsetReadonly]setReadonly[/url]

So maybe construct your field (I assume it's the IP field) seperately and try this before the new form declaration:

function Form() {

$IpField = new TextField("IPAdress","IPAddress",$_SERVER['REMOTE_ADDR']) ;
$IpField->setReadonly(true);

return new Form($this, "Form", new FieldSet(

// List your fields here
new EmailField("Email", "Email address"),
new PasswordField("Password", "Password"),
new TextField("FirstName", "First name"),
new TextField("LastName", "Last name"),
new TextField("Phone", "Phone"),
new TextField("Company", "Company/Hospital"),
new TextField("Address1", "Address 1"),
new TextField("Address2", "Address 2"),
new TextField("City", "City"),
new TextField("State", "State"),
new TextField("Zip", "Zip"),
new TextField("Country", "Country"),
$IpField

.......

Worth a stab

Aaron

Avatar
Double-A-Ron

Community Member, 604 Posts

4 May 2009 at 11:57pm

Meh,

[url=http://doc.silverstripe.com/doku.php?id=readonlyfield&s=readonlyfield]ReadonlyField[/url]

Avatar
TerryMiddleton

Community Member, 108 Posts

5 May 2009 at 12:35am

Double-A-Ron,

I seriously need to take a class on php5 functions and classes. I lack some serious knowledge that would go a long way in working with SS.

Thanks for the inside.

I certainly got the IPAddress to show up (easy) but would you expect the field to still be editable? I can erase the ip address.

I'm testing now to see if the IPField still comes across in the email.

Thanks again,

Terry

Avatar
Double-A-Ron

Community Member, 604 Posts

5 May 2009 at 8:06am

Did you try making it a readOnlyField rather than use my code above?

Avatar
TerryMiddleton

Community Member, 108 Posts

5 May 2009 at 3:25pm

Double-A-Ron,

One million thank you's....That did it. I did the following:

new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])

Worked perfectly...

Thanks again,

Terry