Hi,
I'm trying to implement a voting system, but I want to be able to vote for images, without having to go into each individual image page (although they exist). I created an image page, with a controller where you can vote, and I want to call this same form from the page that displays all of the images together. The problem is that I can't call the form if it's in the controller from the parent, and the form field asks me for the name of the controller where the form is. So, if it's in the model (where it shouldn't be anyhow) I'll get a message saying: Action 'UpVoteForm' isn't allowed on class PicturePage_Controller.
What's the correct way to do this?
Should I have two forms that are exactly the same, one in the PictureHolder_Controller, and the other one in the PicturePlace_Controller? Or should the form be inside the model (which doesn't seem right, since it should be part of the view). Or should I have most of the form functionality inside the model and add a function inside PicturePage_Controller that only calls the model?
Thanks!
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.
I think the easiest way would be to create a custom form class (VoteForm), as described here:
http://doc.silverstripe.org/doku.php?id=form#by_subclassing
Then instantiate the class where you need it, eg. in your controller:
public function VoteForm(){
return new VoteForm($this, 'VoteForm');
}
You could also move the action to the form class, although it doesn't seem like a nice design. Example:
In the form class:
public function handleFormData($data, $form){
// do something with the form data
}
In the controller:
// change vote to whatever your action is named
public function vote($data, $form){
$form->handleFormData($data, $form);
return array();
}
Thanks for your help!
I'm trying to do this, but then, when generating the form, I'd need to pass the id to the form, so it would look like this:
function UpVoteForm($id_articlepage) {
}
I thought I could do it directly from my view with something like this:
<% control Children %>
<% Top.UpVoteForm($id) %>
<% end_control %>
And I get an error.
So, if passing dynamic variables is not supported, how do you generate a form on the fly? Can I get my current place in the control even if I'm on the parent?
I have a feeling I'm going about this in a completely wrong way, but I can't figure out another way to do it.
I couldn't find an appropriate solution for this, so what I did was create a single form with a hidden field that contains the potential chosen image. Whenever the vote for this image button is clicked, the form will change this hidden field's value to the clicked image and submit the form. Here's the code in case anyone is interested, or it helps anyone. id_articlepage is the name of my hidden field.
<a href = "$ID" class="votelink">Vote for me</a>
$UpVoteForm
<script type="text/javascript">
$(document).ready(function() {
$('a.votelink').click(function() {
$('#Form_UpVoteForm_id_articlepage').val($(this).attr('href'));
$('#Form_UpVoteForm').submit();
return false;
});
})
</script>