?>

Advanced work with the form designer in SkeekS CMS

Advanced work with the form designer in SkeekS CMS
Sometimes tasks arise when you need to add complex, custom validation to forms that were created through the form designer. For example, there is an email in the form, and you really need to check that it is an email. For these purposes, there is a simple solution.

Предлагаю сделать следущее

1) Создать компонент common/components/ProjectComponent.php

<?php
/**
 * @link https://cms.skeeks.com/
 * @copyright Copyright (c) 2010 SkeekS
 * @license https://cms.skeeks.com/license/
 * @author Semenov Alexander <semenov@skeeks.com>
 */

namespace common\components;

use skeeks\cms\relatedProperties\models\RelatedPropertiesModel;
use skeeks\cms\validators\PhoneValidator;
use skeeks\modules\cms\form2\models\Form2FormSend;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yii\base\Event;
use yii\base\Model;

/**
 * @author Semenov Alexander <semenov@skeeks.com>
 */
class ProjectComponent extends Component implements BootstrapInterface
{
    public function bootstrap($application)
    {
        Event::on(RelatedPropertiesModel::class, Model::EVENT_BEFORE_VALIDATE, function($e) {
            /**
             * @var $model RelatedPropertiesModel
             * @var $formSend Form2FormSend
             */
            $model = $e->sender;
            $formSend = $model->relatedElementModel;



            if ($formSend->form->code == 'calculate') {
                $model->addRule(['email'], 'email', ['enableIDN' => true]);
                $model->addRule(['phone'], PhoneValidator::class);
                //print_r($model->toArray());die;
            }
        });
    }
}

2) Поправить файл frontend/config/main.php

'bootstrap' => ['project'],

'components' => [
        
        'project' =>  \common\components\ProjectComponent::class,
]

 

Таким образом вы перехватываете событие начала валдиации данных (вернее даже, событие, которое возникает до начала валидации), и пишите свою валидацию, воплощая любую логику!

All Comments (0)
No Comments