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