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.

Form Questions

Custom form template, error message for server side validation


Reply

4 Posts   3803 Views

Avatar
Ben_W

24 June 2009 at 12:49pm Community Member, 80 Posts

I have read the following doc http://doc.silverstripe.com/doku.php?id=Form. It works 99% percent the way I wanted. What I would like to know is that, is there more documentation on how to customize the form template. You see, in the example given from above link, only contain two 'text' input filed, what happen if I need dropdown, radio, textarea etc, I would think it needs something like, $dataFieldByName('dropDownName'). I will try them out, but more documentation would be very helpful here.

Second, even though I manage to get this simple form up and running, one thing I found is that if I turn the javascript off, the server side validation seems work all right by not letting script proceed any further, yet, the error message does not show anywhere on the page, some thing is off.

Any advice would be appreciated, I am running out of time.

Thank you.

Avatar
Hamish

24 June 2009 at 1:29pm (Last edited: 24 June 2009 1:30pm), Community Member, 712 Posts

To add new form elements you just keep adding them to the fields array:

function MyCustomForm() {
   $fields = new FieldSet(
      new EmailField("Email"),
      new EncryptedField("Password"),
      new DropdownField("Foo1", "Bar1"),
      new CheckboxField("Foo2", "Bar2"),
   );
   $actions = new FieldSet(new FormAction("login", "Log in"));
   return new Form($this, "MyCustomForm", $fields, $actions);
}

This page might be more useful to you: [url]http://doc.silverstripe.com/doku.php?id=recipes:forms[/url]

Most likely, you have not created the form action that handles the form response, or your form action does not return anything to the user via a template.

In the above example, you need to have a 'login' method in the controller of the page where the form is located. This login method should return a template, or redirect to another page.

Please supply you code (preferable via http://pastie.org) and I can have a closer look at the issue.

Avatar
Ben_W

24 June 2009 at 2:10pm Community Member, 80 Posts

Hi Hamish

Thank you for your reply. I have posted my code on pastie.org. Here is the link: http://pastie.org/522439

I am aware of these doc you mentioned. I know how to add different type of input into the form, what I am not certain is how to display them in my custom template. Most important, I can't get server side validation error message to be displayed on my custom template.

Thanks again!

Avatar
Piklets

10 December 2009 at 8:29pm Community Member, 36 Posts

I know it has been a while, but here:
$dataFieldByName(CreditCardNumber)
      <span class="message $dataFieldByName(CreditCardNumber).MessageType">$dataFieldByName(CreditCardNumber).Message</span>