Search code examples
phpsilverstripesilvershop

how to render each form fieldlist like contactdetails form, billingaddress form to a custom template in Silvershop multi-step checkout


I am currently using Silverstripe module Silvershop (https://github.com/silvershop/silvershop-core) and customizing the checkout process with Multi-Step "Stepped" Checkout. My question is how to render each form fieldlist like contactdetails form, billingaddress from to a custom template, so that I can have more control over the style of the forms. I tried to follow silverstripe doc(https://docs.silverstripe.org/en/3.4/developer_guides/forms/form_templates/) to create a form template, but didn't get it working. The custom form fields are not outputing to the front end.

Some code in site/mysite/code/CheckoutStep_ContactDetailsCustom.php

public function ContactDetailsForm()
{
    .....

    $form->setTemplate('CustomContactForm');
    $this->owner->extend('updateContactDetailsForm', $form);

    return $form;
}

Some code in site/themes/custom/templates/Includes/CustomContactForm.ss

<form $AttributesHTML>
<fieldset>
    <% with Fields %>
        <% with FieldByName(FirstName) %>
            <div>
                {$Field}
            </div>
        <% end_with %>
        <% with FieldByName(Surname) %>
            <div>
                {$Field}
            </div>
        <% end_with %>
    <% end_with %>
</fieldset>
</form>

This is the default form layout: enter image description here


Solution

  • Firstly, you'll want to render {$FieldHolder} not {$Field} as the holder template contains $Message, which will show validation messages. (Not rendering this can make you wonder why a form isn't working for hours)

    Secondly, did you ?flush=all after adding the template? I can't think of another reason why this wouldn't work.