1

基于此线程,我研究了一个钩子来验证我的翻译字段:https ://stackoverflow.com/a/33070156/4617689 。我已经做到了,但我正在寻找你们来帮助我改进我的代码,所以请随时评论和修改

class ContentbuildersTable extends Table
{

    public function initialize(array $config)
    {
        $this->addBehavior('Tree');
        $this->addBehavior('Timestamp');
        $this->addBehavior('Translate', [
            'fields' => [
                'slug'
            ]
        ]);
    }

    public function validationDefault(Validator $validator)
    {
        $data = null; // Contain our first $context validator

        $validator
            ->requirePresence('label')
            ->notEmpty('label', null, function($context) use (&$data) {
                $data = $context; // Update the $data with current $context
                return true;
            })
            ->requirePresence('type_id')
            ->notEmpty('type_id')
            ->requirePresence('is_activated')
            ->notEmpty('is_activated');

        $translationValidator = new Validator();
        $translationValidator
            ->requirePresence('slug')
            ->notEmpty('slug', null, function($context) use (&$data) {
                if (isset($data['data']['type_id']) && !empty($data['data']['type_id'])) {
                    if ($data['data']['type_id'] != Type::TYPE_HOMEPAGE) {
                        return true;
                    }
                    return false;
                }
                return true;
            });

        $validator
            ->addNestedMany('translations', $translationValidator);

        return $validator;
    }

}

我对我的 $data 技巧并不感到自豪,但我还没有找到将验证器的数据放入我的nestedValidator 的方法......

这里的重要部分是要注意,我的nestedValidator 只对“翻译”进行规则,这非常重要!

class Contentbuilder extends Entity
{

    use TranslateTrait;

}

这里是 I18ns 工作的基础

class BetterFormHelper extends Helper\FormHelper
{

    public function input($fieldName, array $options = [])
    {
        $context = $this->_getContext();
        $explodedFieldName = explode('.', $fieldName);
        $errors = $context->entity()->errors($explodedFieldName[0]);

        if (is_array($errors) && !empty($errors) && empty($this->error($fieldName))) {
            if (isset($errors[$explodedFieldName[1]][$explodedFieldName[2]])) {
                $error = array_values($errors[$explodedFieldName[1]][$explodedFieldName[2]])[0];
                $options['templates']['inputContainer'] = '<div class="input {{type}} required error">{{content}} <div class="error-message">' . $error . '</div></div>';
            }
        }

        return parent::input($fieldName, $options);
    }

}

使用那个 formHelper,我们会得到 nestedValidation 的错误并将它们注入到输入中,我对模板不满意,所以这就是它非常难看的原因。

<?= $this->Form->create($entity, ['novalidate', 'data-load-in' => '#right-container']) ?>

<div class="tabs">

    <?= $this->Form->input('label') ?>
    <?= $this->Form->input('type_id', ['empty' => '---']) ?>
    <?= $this->Form->input('is_activated', ['required' => true]) ?>
    <?= $this->Form->input('translations.fr_FR.slug') ?>
    <?= $this->Form->input('_translations.en_US.slug') ?>

</div>

<?php
    echo $this->Form->submit(__("Save"));
    echo $this->Form->end();
?>

当 type_id 未设置为 Type::TYPE_HOMEPAGE 时,此处需要我的 fr_FR.slug,是的,我的主页没有 slug,请注意根本不需要 en_US.slug,因为我只需要 'translations.xx_XX.xxxx' 而不是'_translations.xx_XX.xxxx'。

以及代码的最后一部分,控制器

$entity = $this->Contentbuilders->patchEntity($entity, $this->request->data);

// We get the locales
$I18ns = TableRegistry::get('I18ns');
$langs = $I18ns->find('list', [
    'keyField' => 'id',
    'valueField' => 'locale'
])->toArray();

// Merging translations
if (isset($entity->translations)) {
    $entity->_translations = array_merge($entity->_translations, $entity->translations);
    unset($entity->translations);
}

foreach ($entity->_translations as $lang => $data) {
    if (in_array($lang, $langs)) {
        $entity->translation($lang)->set($data, ['guard' => false]);
    }
}

这是我这边的最终结果的 .gif:http: //i.giphy.com/3o85xyrLOTd7q0YVck.gif

4

0 回答 0