Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Admin scaffold // multiple forms
  • He folks,

    I have a Problem. I created a whole admin area with the admin scaffolding and edited to ma needs (but still using the crud logic given by the scaffolding).
    Now i want to  use many forms on one page separated in different tabs. The problem is by rendering all the create elements into one Parent Create element, it only get the data from the parent Controller. 
    So for example I have a controller 'article' and I have the controllers 'article/image', 'article/attributes' .... 

    So my question is how can I manage to use each controllers 'create' and 'edit' functionality in each tab ?  Or is this even possible ? Or do you guys have any ideas how to solve my problem in the fuelphp way ? 


    example code with tabs: views/admin/article/create.php

    <p><?php echo Html::anchor('admin/article', 'Zurück zur Artikel Übersicht'); ?></p>
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
      <li class="active"><a href="#article" role="tab" data-toggle="tab">Artikel Allgemein</a></li>
      <li><a href="#image" role="tab" data-toggle="tab">Bilder</a></li>
        <li><a href="#attribute" role="tab" data-toggle="tab">Attribute</a></li>
      <li><a href="#crossselling" role="tab" data-toggle="tab">Crossselling</a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
      <div class="tab-pane active" id="article"><?php echo render('admin/article/_form'); ?></div>
      <div class="tab-pane" id="image"><?php echo render('admin/article/image/create'); ?></div>
      <div class="tab-pane" id="attribute"><?php echo render('admin/article/attributegroups/create')  ?></div>
      <div class="tab-pane" id="crossselling"><?php  echo render('admin/article/crossselling/create')  ?></div>
    </div>

    Thanks a lot for your help,
    Ralf
  • HarroHarro
    Accepted Answer
    If you want to do this, you have to make sure the <form> action attribute of the different forms point to the correct controller method.

    By default it points to the controller that displayed it.
  • Hi Harro,

    ok, that sound logic to me. Another  thing is getting data for example from the admin/article/crossselling/create controller in the create form (i there have a list of all available articles to choose for crosslleing). By rendering the other create forms is there a way to go trough each 'action_controller' methods ? 

    Thanks a lot,
    Ralf
  • HarroHarro
    Accepted Answer
    The official answer is "use HMVC".

    So instead of embedding the view directly, call the method in the target controller that displays and processes the form, have the method return the View (if you're using Theme or Template) and use the return value in your tabbed view.

    This will run the controller action, so you can pre-populate the form. In the action, if you have POSTed data, process it, and redirect back to your tabbed form after that (otherwise the URL will suddenly change for the user after form submission).
  • Ok thank you, it took a bit to understand the but i think i've got it almost now.
    But here is another thing:
    that's my controller for the hmvc requests:
    class Controller_Admin_Test extends Controller_Admin
    {

    public function action_index($id=null){


    $article_hmvc['data'] = Request::forge('admin/article/edit/')->execute(array('id' => $id));
    $article_hmvc['name'] = 'Artikel Allgemein';
    $article_image_hmvc['data'] = Request::forge('admin/article/image/edit/')->execute(array('id' => $id));
    $article_image_hmvc['name'] = 'Bilder';
    $article_attributesgroups_hmvc['data'] = Request::forge('admin/article/attributegroups/edit/')->execute(array('id' => $id));
    $article_attributesgroups_hmvc['name'] = 'Attribute';
    $article_crossselling_hmvc['data'] = Request::forge('admin/article/crossselling/edit/')->execute(array('id' => $id));
    $article_crossselling_hmvc['name'] ='Crossselling';
    $view = View::forge('admin/test/index', array(
    'article' => $article_hmvc,
    'article_attributesgroups_hmvc' => $article_attributesgroups_hmvc,
    'article_image_hmvc' => $article_image_hmvc,
    'article_crossselling_hmvc' => $article_crossselling_hmvc
    ));
    }
    }

    when i use this it throws this error: 
    Form instance already exists, cannot be recreated. Use instance() instead of forge() to retrieve the existing instance.
    At first: is this the right way to use the hmvc ? 
    Another thing is that there is no instance() method.

    Thanks,

    Ralf

  • HarroHarro
    Accepted Answer
    execute() returns a Response object, you need to extract the data from it:

    $article_hmvc['data'] = Request::forge('admin/article/edit/')->execute(array('id' => $id))->body();

    Since the body is HTML, you can not pass it to the View like you do, because all data passed to the View gets escaped, so the HTML will be printed:

    $view = View::forge('admin/test/index')->set_safe(array(
         'article' => $article_hmvc,
         'article_attributesgroups_hmvc' => $article_attributesgroups_hmvc,
         'article_image_hmvc' => $article_image_hmvc,
         'article_crossselling_hmvc' => $article_crossselling_hmvc
    ));


    I can't say where the Form error comes from without knowing how you design your forms in the views. But it is caused by creating a second Form instance, without passing a form name, in which it will use 'default'. And a Form instance with the name 'default' already exists.
  • Thanks, 

    it is wokring as described, but it doesn't really do want i thought it may do. 
    Getting the forms of one standard admin scaffold element together isn't really working. 

    p.s. body() doesn't exist in the context you have to go over response()->body();


  • Yes, sure, that's a typo. You need to fetch the Reponse first, before you can get it's body.

    What doesn't it do, and what do you want to to do?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion