Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » PhoneNumberField setAttribute isn't working

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

Page: 1
Go to End
Author Topic: 283 Views
  • flipsidenz
    Avatar
    Community Member
    49 Posts

    PhoneNumberField setAttribute isn't working Link to this post

    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>

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: PhoneNumberField setAttribute isn't working Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: PhoneNumberField setAttribute isn't working Link to this post

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

    283 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.