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.

Data Model Questions /

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

DataObject


Go to End


6 Posts   1682 Views

Avatar
Rhodin

Community Member, 3 Posts

21 June 2011 at 10:01pm

Hey guys, I am new to Silverstripe and am trying to make sth.
I have made a custom class that extends DataObject (myClass), has some fields (title, a link, some text to enter)
I have made a page that extends Page that is using DataObject::get() in function myFunction to get all the DataObjects (myClass) and template that is using <% control myFunction %> the function to loop through all of myClass and display them
Now, I have a form for entering new myClass, but I need one to edit it.
Any1 can help me with frontend edit form and how to call it?
Should I make a new page that will have the form? How to call it?

Avatar
Rhodin

Community Member, 3 Posts

24 June 2011 at 7:07pm

Edited: 24/06/2011 7:12pm

Not sure that i follow what would help me there?
I already made the DataObject with a front end form to enter a new DataObject on the page to display them.
When I loop through them on that page i am displaying all the info (the data I fill in the form to enter a new myClass), I have also been able to create a delete function so I can delete the chosen myClass, but I haven't found a way to edit one...

delete:

function delmyClass($request) {
$param = $request->allParams();
$myClassID=(int)$param['ID'];
DB::query("DELETE FROM myClass WHERE ID = '$myClassID'");
Director::redirectBack();
}

and from template I call it with

<% control myFunction %>
<li>

...

<% if Top.canDelmyClass %>
<a href="{$delURL}">Delete</a>
<% end_if %>
<% if Top.canEditmyClass %>
<a href="{$EditURL}">Edit</a>
<% end_if %>

</li>

<% end_control %>

Now, delete function works, and I have been trying to make edit function, but whatever i do I can't seem to make it work.
Has any1 tried this?
Can any1 tell me where to make edit form? I have to go to another page that will have edit form and fill it with data... How to send the data from specific DataObject myClass to it?
I don't want to have that page in my site, so I would like to do it with render with (page) or sth...
Can any1 help me with that? How to link to the form, give needed data to it, display it with that data, edit it, save, then redirect back to the page I was on.
Thank you all in advance!

Avatar
swaiba

Forum Moderator, 1899 Posts

24 June 2011 at 8:40pm

Edited: 28/06/2011 1:13am

ok, well you asked for help on creating an edit form, so I pointed you to the documentation on forms...

here is some code from a frontend editting form I have, hope this helps instead...

function MyDataObjectForm() {
	$doMyDataObject = DataObject::get_by_id('MyDataObject',<ID>);
	$fields = $doBusinessProfile->getCMSFields();
	$ts = $fields->findOrMakeTab('Root.Main');
	$fields = $ts->Fields();

	$actions = new FieldSet(new FormAction('CreateUpdateMyForm', 'Update'));

	$form = new Form($this, 'MyEditForm', $fields, $actions, $validator);

	if(Session::get("MyFormData")) {
		$previous_value = Session::get("MyFormData");
		Session::clear("MyFormData");
		$form->loadDataFrom($previous_value);
	}
	else {
		$form->loadDataFrom($doMyDataObject);
	}

	return $form;
}

function CreateUpdateMyForm($data,$form) {
	$bValid = true;
	Session::set("MyFormData", $data);

	only do the password change if they both have text in and match
	if($data['SomeField'] != 'SomeValue) {
		$form->addErrorMessage('SomeField','Some custom error','required');
		$bValid = false;
	}

	

	if (!$bValid){
		return Director::redirectBack();
	}

	$doMyDataObject = DataObject::get_by_id('MyDataObject',<ID>); 
	$doMyDataObject->update($data);
	$doMyDataObject->write();

	Director::redirect($this->Link().'&posted=1');
}

Avatar
Rhodin

Community Member, 3 Posts

28 June 2011 at 12:59am

Hey, thank you.
Looks nice, but a bit confusing, I don't have all of your code so I don't know what couple of things are, seems a bit of it missing for the whole picture. I will try to figure it out though. Anyways, how do you call it from template? Where do you use $strURL (i guess that is the url where the form should be rendered or what?

Avatar
swaiba

Forum Moderator, 1899 Posts

28 June 2011 at 1:12am

I've removed the $strURL bit as that related to the success page - the current page plus a GET url var to indicate it's been posted to...

where this can go into the controller along with the functions from above...

function Posted() {
	if (isset($_GET['posted']))return true;
	return false;
}

and the template code...

<% if Posted %>
thanks you filling in the form
<% else %>
$MyDataObjectForm
<% end_if %>