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

PhoneNumberField setAttribute isn't working


Reply


3 Posts   433 Views

Avatar
flipsidenz

Community Member, 49 Posts

5 November 2013 at 10:03am

Hi,

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(
         $this,
         __FUNCTION__,
         FieldList::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'),
            HiddenField::create("CarId")->setValue($id)
         ),
         new FieldList(
            new FormAction('EnquireAction', 'Send an Enquiry')
         ),
         new RequiredFields(
            'Email'
         )
      );

      $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">
</div>

Avatar
Willr

Forum Moderator, 5513 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)

<?php

class MyPhoneNumber extends PhoneNumberField {

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

$fields->getChildren()->dataFieldByName($this->name.'[Number]')->setAttribute(...);

return $fields;
}
}

Avatar
Willr

Forum Moderator, 5513 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 (https://github.com/silverstripe/silverstripe-framework/blob/3.1/forms/DatetimeField.php#L218) then you should be able to do PhoneNumberField::create('Phone')->getNumberField()->setAttribute('...');