1779 Posts in 582 Topics by 556 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 3561 Views |
-
How to set a Textfield to Read Only?

30 August 2010 at 1:46pm Last edited: 30 August 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
-
Re: How to set a Textfield to Read Only?

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();
-
Re: How to set a Textfield to Read Only?

2 September 2010 at 12:27pm
Thank you for that Will - much appreciated.
-
Re: How to set a Textfield to Read Only?

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
I don't get a standard input field. Just a span like this:$TotalAmountField = $TotalAmountField -> performReadonlyTransformation();
<span id="Form_PaymentForm_TotalAmount" class="readonly "><i>(none)</i></span>
-
Re: How to set a Textfield to Read Only?

16 September 2010 at 12:14am Last edited: 16 September 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;
}
} -
Re: How to set a Textfield to Read Only?

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.
-
Re: How to set a Textfield to Read Only?

16 September 2010 at 12:37pm
Another way is to set the Field to Disabled.
$Name = new TextField('Name', 'Name');
$Name->setDisabled(true);
-
Re: How to set a Textfield to Read Only?

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
| 3561 Views | ||
|
Page:
1
|
Go to Top |





