Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can final logic be elsewhere than on the Form itself? #35

Closed
Einenlum opened this issue Aug 31, 2023 · 1 comment
Closed

Can final logic be elsewhere than on the Form itself? #35

Einenlum opened this issue Aug 31, 2023 · 1 comment

Comments

@Einenlum
Copy link

Hi there,

First, thank you for your work. It really helps.

It seems to me that the only way right now to do something when the form is finally submitted, is to add logic in the onStep closure of the final step.

// CarRegistration.php

$form = Form::make($viewName, [
    'title' => 'Car Registration',
])->namespaced('car_registration')
    ->canNavigateBack(true);

$form->addStep(1, Step1::make());
$form->addStep(2, Step2::make());
$form->addStep(3, Step3::make());

$form->onStep('*', function (Form $form) {
    if ($form->request->get('submit') === 'reset') {
        $form->reset();
    }
});

$form->onStep($form->lastStep(), function (Form $form) {
    $data = $form->toArray();
    $form->reset();

    Car::create([
        'country' => $data['country'],
        'brand' => $data['brand'],
        'model' => $data['model'],
        'year' => $data['year'],
    ]);

    return redirect()->route('cars.index');
});

return $form;

I would like to have my logic in my controller (to avoid having logic in different places). Something like:

// CarRegistration.php

$form = Form::make($viewName, [
    'title' => 'Car Registration',
])->namespaced('car_registration')
    ->canNavigateBack(true);

$form->addStep(1, Step1::make());
$form->addStep(2, Step2::make());
$form->addStep(3, Step3::make());

$form->onStep('*', function (Form $form) {
    if ($form->request->get('submit') === 'reset') {
        $form->reset();
    }
});

return $form;
// CarRegistrationController.php

class CarRegistrationController extends Controller
{
    public function form(): Form
    {
        $form = CarRegistration::make('car-registration');
        if ($form->isOver()) {
            $data = $form->toArray();
            $form->reset();

            Car::create([
                'country' => $data['country'],
                'brand' => $data['brand'],
                'model' => $data['model'],
                'year' => $data['year'],
            ]);

            return redirect()->route('cars.index');
        }

        return $form;
    }
}

Is something like this planned or already possible?

Thanks!

@Einenlum Einenlum changed the title Have final logic elsewhere than in the Form itself? Can final logic be elsewhere than on the Form itself? Aug 31, 2023
@bayareawebpro
Copy link
Owner

bayareawebpro commented Dec 25, 2023

Added onComplete method to newest release. reset is called automatically after your callback logic.

$form->onComplete(function($form){

    $values = $form->toCollection()->only([
        'country', 'brand', 'model', 'year'
    ]);

    Car::create($values->toArray());
    
    return redirect()->route('cars.index');
});

Happy Holidays!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants