CakePHP 3.1:我对翻译行为字段的验证,需要一些帮助来审核/评论

时间:2015-10-29 23:29:57

标签: validation cakephp internationalization multilingual cakephp-3.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的技巧感到自豪,但是我没有找到一种方法将验证器的数据放入我的嵌套值_Validator ......

这里重要的一点是要注意我只在'翻译'上规定了我的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

0 个答案:

没有答案