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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Willr, Where Are You? (Can Rename To: "Custom Form and Database Interaction Problems")

Go to End

2 Posts   1272 Views


Community Member, 2 Posts

28 June 2013 at 11:18pm

Edited: 28/06/2013 11:28pm

[Don't want to discourage anyone else from dropping input, but (let's face it) Willr's answers are guaranteed to be impeccable.]

After thoroughly following the tutorials and reading and re-reading the official book, I am still having trouble understanding the difference in functionality between the model and controller portion of the MyPage.php file. I do understand that the model handles the content and the controller handles the processing of data (as per Intuitively, it would seem that only the form information and attributes should go in the model portion and the submit function should go in the controller. After viewing numerous code snippets, it seems to be possible to do it either way (but this is the most common). Also having trouble understanding proper DataObject creation. Should it be in its own file for permanent reference, or is it supposed to be declared in the Code/MySite.php file (which seems more temporary, so-to-speak).

The Final Goal: Have a 'userprofile' table to store individual user information and a 'profiledatabase' table to nest the 'userprofile' tables into (much like the ArticlePage and ArticleHolder in the aforementioned tutorial link). The form should then create a 'userprofile' object, write the form information into the corresponding attributes of the 'userprofile' object, and then write that 'userprofile' table/object into a new row of the 'profiledatabase' table/object. I'm thinking I might also need to store the username as a separate field in the 'profiledatabase' for profile lookup, assuming I store the whole 'userprofile' object in the 'profiledatabase' as its own attribute (please confirm). Otherwise, the 'userprofile' attributes would need to be written across the columns of the row for that particular 'userprofile'. Before this post gets any longer, please correct any incorrect assumptions (or confirm correct ones) that I have made in the previous paragraphs and then take a look the actual code. The form submit result is posted after the code,

Now to the implementation:

*Note: This is a mix of code inspired from both the tutorial previously mentioned as well as material found in the Official SilverStripe Manual (complete Guide to CMS Development) from pages 146-150.

1: Database Creation

--> mysite/code/UserProfile.php


class UserProfile extends DataObject
    static $db = array(

--> mysite/code/ProfileDatabase.php


class ProfileDatabase extends DataObject
  //Not sure about this yet

2: UserForm.php Page/Controller

--> /mysite/code/UserForm.php


class UserForm extends Page

    function Form()
        $fields = new FieldList(
            new TextField('First Name:'),
            new TextField('Middle Name:'),
            new TextField('Last Name:'));
        $actions = new FieldSet(new FormAction('submitUserForm', 'Submit'));
        $validator = new RequiredFields('First Name', 'Last Name');
        $form = new Form($this, 'Form', $fields, $actions, $validator);
        return $form;   

class UserForm_Controller extends Page_Controller

    function submitUserForm($data, $form)
        $userprofile = new UserProfile();
        // Almost certain this next line is way wrong
        $userprofile->UserRegistrationFormID = $this->dataRecord->ID;
        $form->sessionMessage('Form Submitted Successfully', 'good');

3: Layout

--> /themes/mytheme/templates/layout

<div id="User Form">
  <h2>User Form</h2>


Current Result:

The form displays correctly when called for and the database tables seem to appear created in phpmyadmin, but when clicking submit, I am redirected to the page:

Page not found - Sorry, it seems you were trying to access a page that doesn't exist. Please check the spelling of the URL you were trying to access and try again.

--and the tables displayed in phpmyadmin are not updated with the information in the form.

"Willr" you please work your magic? Sorry about the formatting, the indentations aren't appearing.


Forum Moderator, 5523 Posts

1 July 2013 at 6:33pm

Ha well I'm glad I can help. In the process of producing new documentation and how-to instructional videos for a couple developers so they will be released to the community at some point. Know documentation is tight but we're a lot better at it than a couple years ago!

IRC (if you get sometime for UT-12 timezone guys) is also a great place for discussions

Let me try and answer your questions one by one

Also having trouble understanding proper DataObject creation. Should it be in its own file for permanent reference, or is it supposed to be declared in the Code/MySite.php file (which seems more temporary, so-to-speak).

You can define classes anywhere you want. Put everything in 1 big site.php file if you want. BUT SilverStripe convention (and practise) will recommend 1 file per classname (i.e MyDataObject.php, MyPageType.php).

For your code, you have a number of things missing. It's best to work on solving things one step at a time but a few things that have caught you out.

1) You need to state that your form action is an allowed action on the controller.

class UserForm_Controller extends Page_Controller {
static $allowed_actions = array(

2) Saving the form looks like you've copied a bit too much, should look like

$profile = new UserProfile(); 

Not completely sure I understand the need for profiledatabase , userprofile database. ArticlePage, ArticleHolder is different, it's used to describe parent relationships. In your application I figure you'd have a single page UserForm.php which creates UserProfile objects (DataObjects). You may also then have another pagetype UsersPage which outputs the content of the UserProfiles on the page but you wouldn't need to setup any relationships.