# ivyforms/0.1.2/backend/src/Controllers/Form/GetFormController.php

The Innovative Form Builder – IvyForms, version 0.1.2. 92 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/GetFormController.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.1.2/raw/backend/src/Controllers/Form/GetFormController.php
- Modified: 2025-06-12T10:29:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.1.2/code/backend/src/Controllers/Form/GetFormController.php#L10-L20`.

```php
<?php

/**
 * @copyright © TMS-Plugins. All rights reserved.
 * @licence   See COPYING.md for license details.
 */

namespace IvyForms\Controllers\Form;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\ForbiddenException;
use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Common\Sanitizer\Sanitizer;
use IvyForms\Controllers\Controller;
use IvyForms\Services\Confirmation\ConfirmationService;
use IvyForms\Services\Field\FieldService;
use IvyForms\Services\Form\FormService;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Class GetFormController
 *
 * @package IvyForms\Controllers\Form
 */
class GetFormController extends Controller
{
    private FormService $formService;
    private FieldService $fieldService;
    private ConfirmationService $confirmationService;
    public function __construct(
        FormService $formService,
        FieldService $fieldService,
        ConfirmationService $confirmationService
    ) {
        $this->formService = $formService;
        $this->fieldService = $fieldService;
        $this->confirmationService = $confirmationService;
    }

    /**
     * @param WP_REST_Request $data
     *
     * @return WP_REST_Response
     *
     * @throws NotFoundException
     * @throws InvalidArgumentException|ForbiddenException
     */
    public function handle(WP_REST_Request $data): WP_REST_Response
    {
        // Verify the nonce
        Sanitizer::verifyNonce($data->get_header('X-WP-Nonce'));

       // Check if the form ID is provided
        if (empty($data->get_params())) {
            throw new InvalidArgumentException('Invalid request data.');
        }
        // Sanitize the input data
        $formId = Sanitizer::sanitizeId($data->get_param('id'));

        $form = $this->formService->getFormById($formId);

        $fields = $this->fieldService->getAllFields($form->getId());

        $fieldsMap = [];

        $confirmationsArr = $this->confirmationService->getConfirmationsById($formId);

        $confirmations = [];

        foreach ($confirmationsArr as $confirmation) {
            $confirmations[] = $confirmation->toArray();
        }

        foreach ($fields as $field) {
            $fieldsMap[] = $field->toArray();
        }

        $form->setFields($fieldsMap);

        return new WP_REST_Response([
            'message' => 'OK',
            'data'    => array_merge($form->toArray(), ['confirmationId' => $confirmations[0]['id']])
        ], 200);
    }
}

```
