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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

PhoneNumberField setAttribute isn't working

Go to End

3 Posts   1929 Views


Community Member, 49 Posts

5 November 2013 at 10:03am


Preview the code below. I am trying to set an attribute "placeholder" on the field, however, it isn't showing up in the template when rendered onto the screen. Any thoughts?

public function Enquire(){

		//Detect action and id from url parameters for later use
		$action = $this->request->param('Action');
		$id = $this->request->param('ID');
		//Create the form
		$form = Form::create(
				TextField::create("Name")->setTitle('Name')->setAttribute('placeholder', 'Name'),
				EmailField::create("Email")->setAttribute('placeholder', 'Email*'),
				PhoneNumberField::create("Phone", "Phone Number")->setAttribute('placeholder', 'Phone'),
				TextAreaField::create("Comments")->setAttribute('placeholder', 'Comments'),
			new FieldList(
				new FormAction('EnquireAction', 'Send an Enquiry')
			new RequiredFields(

		$this->extend('updateForm', $form);

		return $form;

What is generated into the browser:

<div class="fieldholder-small">
	<input type="text" name="Phone[Number]" class="numeric text nolabel" id="Phone-Number" maxlength="10" size="10">


Forum Moderator, 5523 Posts

6 November 2013 at 5:57pm

This is because PhoneNumberField is a composite field (a field type made up of lots of other form fields) so attributes aren't applied to children inputs.

The API doesn't actually give you the way to edit the individual phone number fields so the easiest way to add your placeholder rather than editing core is to subclass PhoneNumberField and override the Field() method to modify the form fields (not tested)


class MyPhoneNumber extends PhoneNumberField {

public function Field($properties = array()) {
$fields = parent::Field($properties);


return $fields;


Forum Moderator, 5523 Posts

6 November 2013 at 6:00pm

You don't need to create your own subclass (i.e all that code could be in your Form but it's a bit nasty).

If you're looking to contribute a fix to make this easier, I recommend following the DateTimeField API where the children fields are available as methods ( then you should be able to do PhoneNumberField::create('Phone')->getNumberField()->setAttribute('...');