Skip to content

Instantly share code, notes, and snippets.

@TakahashiIkki
Last active August 11, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakahashiIkki/70eb219b78297454601aec2ebcba7eaa to your computer and use it in GitHub Desktop.
Save TakahashiIkki/70eb219b78297454601aec2ebcba7eaa to your computer and use it in GitHub Desktop.
<div class="">
{$this->Form->create($tests, ['novalidate' => 'novalidate'])}
1:  {$this->Form->input('test.0.mail', ['name'=>'tests[0][mail]'])}<br/>
2:  {$this->Form->input('test.1.mail', ['name'=>'tests[1][mail]'])}<br/>
3:  {$this->Form->input('test.2.mail', ['name'=>'tests[2][mail]'])}<br/>
4:  {$this->Form->input('test.3.mail', ['name'=>'tests[3][mail]'])}<br/>
5:  {$this->Form->input('test.4.mail', ['name'=>'tests[4][mail]'])}<br/>
<input type="submit" value="test">
{$this->Form->end()}
</div>
<div>
{$this->Form->create($test, ['novalidate' => 'novalidate'])}
{$this->Form->input('mail')} <br>
<input type="submit" value="test">
{$this->Form->end()}
</div>
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Test Entity.
*
* @property int $id
* @property string $mail
*/
class Test extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'*' => true,
'id' => false,
];
}
<?php
namespace App\Controller;
use Cake\Event\Event;
/**
* クラスの説明文を記述
*
* @property \App\Model\Table\TestsTable $Tests
*/
class TestsController extends UserBaseController
{
public function beforeFilter(Event $event)
{
$this->loadModel('Tests');
return parent::beforeFilter($event);
}
public function one()
{
$test = $this->Tests->newEntity();
if ($this->request->is(['patch', 'post', 'put'])) {
$test = $this->Tests->patchEntity($test, $this->request->data);
debug($test);
} else {
$test = $this->Tests->newEntity();
}
$this->set(compact('test'));
}
public function many()
{
$tests = [];
for ($i = 0; $i < 5; $i++) {
$tests[] = $this->Tests->newEntity();
}
if ($this->request->is(['patch', 'post', 'put'])) {
$tests = $this->Tests->patchEntities($tests, $this->request->data);
debug($tests);
//debug($this->request->data);
} else {
for ($i = 0; $i < 10; $i++) {
$tests[] = $this->Tests->newEntity();
}
}
$this->set(compact('tests'));
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class TestsTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tests');
$this->displayField('id');
$this->primaryKey('id');
$this->alias('tests');
}
public function validationDefault(Validator $validator)
{
$validator
->allowEmpty('id', 'create');
$validator
->email('mail', false, 'メールの形式で入力してください。')
->notEmpty('mail');
return $validator;
}
}

不明点

patchEntity の場合

下記のように ValidationErrorが発生した場合は、エラーが返る。 画像

patchEntities の場合

下記のように ValidationErrorが発生した場合は、エラーしか返らない。

画像

サンプルコード

コントローラー

one アクション(patchEntiry)と manyアクション (patchEntities)をサンプルとして作成しました。

ソースリンク

Table

idmail だけを持つテーブルを作成.

ソースリンク

Entity

ソースリンク

Template

※ smarty を 使用しています。

one.tpl

ソースリンク

many.tpl

ソースリンク

試してみた事

・ patchEntiries を 使わずに foreach で patchEntity を回してみた。(・・が、ダメ。) ・ tpl 内で 直接 value で値を埋め込んでみました。 (・・が、ダメ)

サンプルコードのように修正する事でうまくいくように!!!

解決方法

$this->alias('tests');

上記のように テーブルに alias を 設定すると解決。

FormHelper は $this->Form->create($tests)に関して、渡したものが Entity クラスであるなら、 EntityContext というクラスにキャストする。

その際、 フィールドの値は、 POSTした request->data より取得して、エラーメッセージは、Entityより取得するようになっている。

取得を行う際のフィールド名は

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment