3

I'm creating a simple contact component in Joomla 3.3 (which is called Contact Pay).

I created a 'message' and a 'messages' view in the back-end, with associated models and controllers. 'message' is a JForm form, and 'messages' is the list view. Everything works fine there.

However in the front-end I only have a message view, for site visitors to send message. I use the same back-end model in the front-end, using

$controller->addModelPath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_contactpay'.DS.'models' );

in the component entry file.

I simply copied the form XML file (message.xml) from the back-end, removed a few fields, and used it for the front-end. I output the form with the following layout file:

<form action="<?php echo JRoute::_( 'index.php?option=com_contactpay&task=message.save'); ?>"
      method="post" id="contactpay-message-form" class="form-validate form-horizontal" enctype="multipart/form-data">

    <fieldset>

        <?php foreach( $this->form->getFieldset('message_fields') as $field ) : ?>
            <div class="control-group">
                <div class="control-label">
                    <?php echo $field->label; ?>
                </div>
                <div class="controls">
                    <?php echo $field->input; ?>
                </div>
            </div>
        <?php endforeach; ?>

    </fieldset>

    <div class="form-actions">
        <button type="submit" class="btn btn-primary validate"><?php echo JText::_('JSUBMIT');?></button>
        <a class="btn" href="<?php echo JRoute::_('');?>" title="<?php echo JText::_('JCANCEL');?>"><?php echo JText::_('JCANCEL');?></a>
        <input type="hidden" name="option" value="com_contactpay" />
        <input type="hidden" name="task" value="message.save" />
        <?php echo JHtml::_('form.token');?>
    </div>
</form>

When a use fills out the form on the front-end it is saved to the database as it should be.

The Problem

The problem is that after the form is submitted it redirects to a 'messages' view which doesn't exist on the front-end, so I get the following error:

500 View not found [name, type, prefix]: messages, html, contactpayView 

I tried controlling the redirect in the message controller which extends JControllerForm using the following code in the constructor:

$this->setRedirect( JRoute::_( 'index.php?option=com_contactpay&view=message' ));

However, this produces the same results.

How do control the component redirect?

3

The redirect is likely getting overridden by the save function in your controller. You should be able to override the redirect with the method that you listed, we just need to get it in the right spot.

It looks like you should have a message controller on the front end. If you don't have a save function within this controller, add one like so:

public function save($key = null, $urlVar = null)
{
    $return = parent::save($key, $urlVar);
    $this->setRedirect( JRoute::_( 'index.php?option=com_contactpay&view=message' ));
    return $return;
}

This utilizes the existing save method in JControllerForm that loads the model and saves the form, but it makes sure that your redirect overrides the default redirect to the list view.

| improve this answer | |
2

You should have a file called components/com_contactpay/controller.php This is the primary controller for your component. The class within the file should be called ContactPayController.

Add the following construct function. It should look something like this.

class ContactPayController extends JControllerLegacy {

    public function __construct(){
        if (JFactory::getApplication()->input->get('view') == "messages"){
            JFactory::getApplication()->redirect('index.php?option=com_contactpay&view=message');
        } else {
            parent::__construct();
        }
    }
    ...

That should handle the redirect for you before the controller looks for the messages view.

| improve this answer | |
0

Just declare a $view_list variable at your JControllerForm equal to your prefer view name (view folder normally)

protected $view_list = 'message';

and all your form event will redirect to it, unless you specifiably declare equivalent functions as David Fritsch suggests! Best solutions are the simplest!:)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.