Lithium アプリの動作を確認するためにテスト用の Lithium アプリを作成していますが、フォーム ヘルパーが返されたデータや検証エラーを認識していないようです。
現時点では、手動でエラーを返し、ビューで処理する必要があります。
QuestionsController::ask
public function ask() {
if (!empty($this->request->data)) {
$question = Questions::create($this->request->data);
if ($question->save()) {
return $this->redirect(array('Questions::view', 'args'=>$question->id));
} else {
$errors = $question->errors();
}
if (empty($question)) {
$question = Questions::create();
}
return compact('question', 'errors');
}
}
ビュー/質問/ask.html.php
<?php
// Assign the protected object to a variable so we can get at it
if(isset($question)){
$data = $question->data();
}else{
$data['title'] = '';
$data['text'] = '';
}
?>
<?=$this->form->create();?>
<?=$this->form->field('title', array('placeholder'=>'Title your question', 'value'=>$data['title']));?>
<?php if(isset($errors['title'])){
echo "<div class='alert alert-error'><a class='close' data-dismiss='alert' href='#'>×</a>";
foreach($errors['title'] as $e){
echo $e."<br/>";
}
echo "</div>";
}?>
<?=$this->form->field('text', array('placeholder'=>'Enter your question (supports Markdown)', 'type'=>'textarea', 'value'=>$data['text']));?>
<?php if(isset($errors['text'])){
echo "<div class='alert alert-error'><a class='close' data-dismiss='alert' href='#'>×</a>";
foreach($errors['text'] as $e){
echo $e."<br/>";
}
echo "</div>";
}?>
<p>Text input supports <?php echo $this->html->link('markdown', 'http://en.wikipedia.org/wiki/Markdown');?></p>
<?=$this->form->submit('Ask', array('class'=>'btn'));?>
<?=$this->form->end();?>
lithium\template\helper\Form
から、field()
メソッドがtemplate
パラメーターを受け取ることができることがわかります。この例では<li{:wrap}>{:label}{:input}{:error}</li>
、検証メッセージを表示するためのヘルパーに容量があるためです。
では、コントローラーでデータを整理して、ヘルパーがフィールドにデータを入力し、エラーを表示するためにビューに戻されるようにするにはどうすればよいでしょうか?
編集
例の「Sphere」アプリもこの方法を使用していることを追加する必要がありますが、それは標準ですか? (参照)