msvdev/bitrix-extension-forms

Bitrix symfony forms

1.0.8 2020-09-17 12:19 UTC

This package is auto-updated.

Last update: 2024-12-17 21:59:03 UTC


README

Установка composer require msvdev/bitrix-extension-forms

Пример

<?php
namespace Components\User\Login;

use Modules\User\Entities\Login;
use Modules\User\Forms\LoginType;
use Msvdev\Bitrix\Forms\Form;

class Component extends \Msvdev\Bitrix\Component\Component
{
            
    public function executeComponent()
    {
        global $APPLICATION;
        global $USER;
        $entity = new Login($USER);       
        $symfonyForm = new Form($this);
        $form = $symfonyForm->getFormBuilder()->create(LoginType::class, $entity);
        $form->handleRequest();
        if ($this->request->isAjaxRequest() && $form->isSubmitted()) {
            $APPLICATION->RestartBuffer();
            $result = [
                'result' => false,
                'errors' => [],                
            ];
            if($form->isValid()){
                $entity->auth();
                $result['result'] = true;                
            } else { // Get ajax errors
                $result['errors'] = $symfonyForm->getErrorsArray($form->getErrors(true));
            }
            echo json_encode($result);
            die();
        }
        $this->arResult['formView'] = $form->createView();
        $this->arResult['entity'] = $entity;
        $this->includeComponentTemplate();
    }

}