zoghal / cakephp-MultiTranslateBehavior

extended Cakephp 2.1 TranslateBehavior for support save/edit/validate of Multi Language in Forms
19 stars 11 forks source link

Edit Function not working. #1

Closed skiv3r closed 12 years ago

skiv3r commented 12 years ago

Hello, this is a good workaround for 1 page for all translation. Follow exactly your code, the ADD function works flawlessly but the EDIT does not.

public function edit($id = null) { $this->Content->setLocale(array('eng','chi')); $this->Content->id = $id; if (!$this->Content->exists()) { throw new NotFoundException(__('Invalid content')); }

    $this->Content->multiTranslateOptions(array('validate'=>true,'find'=>true));
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->Content->save($this->request->data)) {
            $this->Session->setFlash(__('The content has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The content could not be saved. Please, try again.'));
        }
    } else {

        $this->request->data = $this->Content->read(null, $id);
    }
}

Here's the debug of $this->request->data

array( 'Content' => array( 'id' => '1', 'title' => 'Welcome bla', 'body' => 'Welcomebla.', 'meta_description' => 'test', 'meta_keywords' => 'test', 'created' => '2012-05-07 06:37:20', 'modified' => '2012-05-07 06:37:20', 'locale' => 'eng' ) )

mitchkramez commented 12 years ago

This will happen if you haven't removed the translated field from your primary schema. Make sure you've removed 'title' and 'body' fields from your Contents table.

adico commented 12 years ago

hi - thanks for the Behavior! Following on the problem above, if i do remove the translated field the 'isUnique' validation seems to fail on not finding these fields. Also, what happens if there is a missing translation (e.g. i added a language later)? It seems that entries without the new translations will not show up (i would expect the default, now removed, field to be used instead).

thanks again for your work.

zoghal commented 12 years ago

hi adico,

1- You can create your own customized validation,isUnique core does not support it.

2- This behavior is in any language that supports the function is defined setLocale. So you can easily add any new language.

I've tested this behavior in CakePHP 2. But in version 2.2 and .2.3, many changes were made to the original behavior. May not work properly

adico commented 12 years ago

Thanks for the quick response! For your comments and to help anyone else, i added the following custom validation rule which seems to work (although i am not quite sure how it figures out the locale :) - but it does).

In model file (e.g. Content.php), add this:

public $validate = array(
    'title' => array(
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Title is required'
        ),
        'unique' => array(
            'rule' => 'ensureUnique',
            'message' => 'Title is not unique'
        )
     )
);

public function ensureUnique($check) {
    // $check will have value: array('name' => 'some-value')
    $fieldName = array_keys($check);
    $fieldName = $fieldName[0];
    $fieldValue = $check[$fieldName];

    $contentI18nInstance = ClassRegistry::init('ContentI18n');
    $count = $contentI18nInstance ->find('count', array('conditions' => array(
                'ContentI18n.field' => $fieldName, 
                'ContentI18n.content' => $fieldValue)));
    return ($count == 0);
}
adico commented 12 years ago

zoghal - hi again and thanks again for the behavior.

  1. I am pretty sure "isUnique" rule is part of the core - unless i did not understand your response (http://book.cakephp.org/2.0/en/models/data-validation.html)
  2. My question was specifically about how backward compatibility will work in this case. For objects without entries for the new language - it seems that "find" will simply ignore them which is not what one would want to happen.

For example:

  1. I have Post->title->eng = "Thanks" and Post->title->fre = "Merci"
  2. Now i am adding Spanish but did not yet go back and update Post->title->spa entry

What i was expecting in this case is that the default title field in the original Model will be used - but instead when you are in the Spanish locale - nothing is shown.

zoghal commented 12 years ago

I'm sorry, my English is not so good.

1- You can use the Custom Validation Rules. IsUnique in the core does not support

2-You are right. I did not implement this feature. I do not have enough time to change. I'd be delighted if you can add this feature.