CakePHP 3.1 : My validation for translate behaviour fields, need some help in review/comment

别来无恙 提交于 2019-12-11 10:33:27

问题


I have worked on a hook for validate my translated fields, based on this thread : https://stackoverflow.com/a/33070156/4617689. That i've done do the trick, but i'm looking for you guys to help me to improve my code, so feel free to comment and modify

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;
    }

}

I'm not proud of my trick with the $data, but i've not found a method to get the data of the validator into my nestedValidator...

Important part here is to note that i only rule of my nestedValidator on 'translations', this is very important !

class Contentbuilder extends Entity
{

    use TranslateTrait;

}

Here basic for I18ns to work

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);
    }

}

With that formHelper we gonna get the errors of nestedValidation and inject them into the input, i'm not confortable with the templates, so that's why it's very ugly.

<?= $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();
?>

Here my fr_FR.slug is required when type_id is not set to Type::TYPE_HOMEPAGE, yeah my homepage has not slug, note that the en_US.slug is not required at all, because i only required 'translations.xx_XX.xxxx' and not '_translations.xx_XX.xxxx'.

And the last part of the code, the controller

$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]);
    }
}

Here a .gif of the final result om my side : http://i.giphy.com/3o85xyrLOTd7q0YVck.gif

来源:https://stackoverflow.com/questions/33426427/cakephp-3-1-my-validation-for-translate-behaviour-fields-need-some-help-in-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!