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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Custom form template, error message for server side validation


Go to End


4 Posts   5372 Views

Avatar
Ben_W

Community Member, 80 Posts

24 June 2009 at 12:49pm

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

Community Member, 712 Posts

24 June 2009 at 1:29pm

Edited: 24/06/2009 1:30pm

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: http://doc.silverstripe.com/doku.php?id=recipes:forms

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

Community Member, 80 Posts

24 June 2009 at 2:10pm

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

Community Member, 36 Posts

10 December 2009 at 8:29pm

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