Form which class method to use

Asked

Viewed 61 times

0

I have a form that will display the fields to be inserted in the bd, so far so good, the problem lies in the following:

I created a class (this file will receive several different requests) with the methods add, edit, delete, find, etc. I need that when sending my form in the file that will receive the request it discover which request I am sending (add, edit, delete, etc.) and forward to the right method to treat the functionality, someone knows how to tell the form to execute a particular method of my class?


Edit - Added code

file: Servicoscontroller.php

class ServicosController extends AppController {

public function add() {
    $servico = $_POST['servico'];

    if (!$servico) {
        echo 'Serviço inválido.';           
    } else {
        $database->insert('servicos', [
            'title' => $servico['title'],
            'value' => $servico['value']
            ]);
    }
}

}

the shipping form does not have any particularity for now.. are only 2 msm inputs...

  • Put the code there.

1 answer

0

You can use the Symfony Form Component to perform this task. With this component, it is possible to use an object to render a form; just as it is possible to map the values submitted through a form, in an object.

See an example they provide themselves:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

$form = $formFactory->createBuilder()
    ->add('task', 'text')
    ->add('dueDate', 'date')
    ->getForm();

$request = Request::createFromGlobals();

$form->handleRequest($request);

if ($form->isValid()) {
    $data = $form->getData();

    // ... perform some action, such as saving the data to the database

    $response = new RedirectResponse('/task/success');
    $response->prepare($request);

    return $response->send();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.