Hi all ok I'm playing around with my first form. My question is --
in the example below, headers_sent(...) resolves to true so it does this almighty heavy weight 'redirecting to...'. How do I avoid this so that Director::redirect() calls header(...) instead.
I looked at PageComments and it does the same thing as below but doesn't do the heavyweight redirect; what have I done wrong that means that a simple form submission (could it get any simpler? A single action) sends header information.
thanks in advance!
J
code below
---
/** an opinion is a gesture of thought by user -- marking as favourite or comment or whatever. */
function OpinionForm() {
$loggedInMember = Member::currentUser();
// user is authenticated, so return elements in the form.
// if the user isn't authenticated, we will require them to when they do something.
if( $loggedInMember ) {
$artwork = $this->data();
// we have a member so find whether this artwork was marked as favourite and highlight as appropriate.
// returns a ComponentSet.
$members = $artwork->getManyManyComponents('MemberFavourites');
if( $members ) {
$memberMap = $members->map();
// this is not marked as a favourite, so give them the option to do this
if( !$memberMap[$loggedInMember->ID]) {
$faveAction = new ImageFormAction('doMarkAsFavourite', 'Mark Artwork as favourite', 'mysite/images/fave_unselected.gif', 'mysite/images/fave_selected.gif' );
// ALREADY MARKED. Present option to unmark.
} else {
$faveAction = new ImageFormAction('doUnmarkFavourite', 'Remove from favourites list', 'mysite/images/fave_selected.gif', 'mysite/images/fave_unselected.gif' );
}
}
$fields = new FieldSet();
$actions = new FieldSet( $faveAction );
return new Form( $this, 'OpinionForm', $fields, $actions );
// TODO -- change icon based on state of favourite
}
}
/** user chose this to be a favourite against currently authenticated member. */
function doMarkAsFavourite() {
$loggedInMember = Member::currentUser();
if( $loggedInMember ) {
$artwork = $this->data();
$members = $artwork->getManyManyComponents('MemberFavourites');
$members->add( $loggedInMember );
Director::redirectBack();
}
}