This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

guillaume.batier2373
guillaume.batier2373

Hi everyone, i want to do a simple things: Using the controller (method create) to make a popup a retrieve the form of an other controller. and put it in a popup to make an ajax request.

Exemple, in my plugins i've a Customers controller, and a Invoices controller, i list my items, create and update them. But, i want to create a customer from the Invoice view if the user doesn't exist, i want to call the form of Customer create form in a popup so, in my Invoices controller i've put this code:

public function OnLoadContent()
    {
        $config = $this->makeConfig('$/prestasafe/erp/models/customer/fields.yaml');

        $config->model = new Customer;
        $config->recordUrl = 'prestasafe/erp/customers/create/';

        $widget = $this->makeWidget('Backend\Widgets\Form', $config);

        $widget->bindToController();
        $this->vars['widget'] = $widget;
        return $this->makePartial('$/prestasafe/erp/models/customer/_add_customer_form.htm');
    } 

I call the popup with this button:

<a data-control="popup" data-handler="onLoadContent" href="nojavascript...;" class="btn btn-primary btn-lg"> Launch Ajax Form </a>

It works fine. There is my popup view:

<?= Form::open(); ?>
    <?= $widget->render() ?>
    <!-- <button type="submit" class="btn btn-primary">
        Add user
    </button> -->
    <button data-request="onAddUser" class="btn btn-primary">
        Add user
    </button>
<?= Form::close() ?>

But now, i want to call the create action of the CustomersController, so i've tried this:

public function onAddUser()
{
         $ct = new CtrlCustomer;
         $ct->asExtension('FormController')->create_onSave('create');
}

It seems to work, but it create an empty Customer. So i try this:

public function onAddUser()
    {
        $data = collect(post());

        $data->forget('_session_key');
        $data->forget('_token');
        \trace_log($data->toArray());
        $c = Customer::create($data->toArray());
        \trace_log($c);
        \Flash::success('Success!!!');
    } 

Same probleme. Someone have an idea ?

Thanks

roni.sommerfeld
roni.sommerfeld

You need to put which array name in the model. Something in this style ...

$config->arrayName = 'Customer';

guillaume.batier2373
guillaume.batier2373

Yeap, i've found it,

 public function onAddUser()
    {
        $data = $this->widgetCustomer->getSaveData();

        $model = new \Prestasafe\Erp\Models\Customer;

        $model->fill($data);

        $model->save();   

        return [
            '#currency-dropdown' => 'dropdown update'
        ];
    }

I've created a widget and it's done

$this->widgetCustomer = $this->createCustomerWidget();
public function createCustomerWidget()
    {
        $config = $this->makeConfig('$/prestasafe/erp/models/customer/fields.yaml');
        $config->alias = 'customer';
        $config->arrayName = 'Customer';
        $config->url = Backend::url('prestasafe/erp/customers/create');
        $config->model = new \Prestasafe\Erp\Models\Customer;
        $widget = $this->makeWidget('Backend\Widgets\Form', $config);
        $widget->bindToController();
        return $widget;
    }

Thanks to you !

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.