4

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」アプリもこの方法を使用していることを追加する必要がありますが、それは標準ですか? (参照)

4

1 に答える 1

2

TL; DR

簡単に言うと、フォームをエンティティのサブクラス、つまりに示すようにレコードまたはドキュメントにバインドできます(リンクはリンクパーサーを壊すためにForm::create()短縮されます)。::

コードは次のようになります。

<?= $this->form->create($question); ?>
<?= $this->form->field('title'); ?>
<?= $this->form->field('text'); ?>

長い答え:

QuestionsController::ask()

public function ask() {
    $question = Questions::create();
    if (!empty($this->request->data)) {
        if ($question->save($this->request->data)) {
            return $this->redirect(array('Questions::view', 'args'=>$question->id));
        }
    }
    return compact('question');
}

views/questions/ask.html.php

<?= $this->form->create($question); ?>

    <?= $this->form->field('title', array('placeholder'=>'Title your question'));?>

    <?= $this->form->field('text', array('placeholder'=>'Enter your question (supports Markdown)', 'type'=>'textarea'));?>

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

フォームヘルパーがエラーが発生した場合に自動的にエラーを表示する方法に注意してください:)

#li3哲学へのいくつかの有用なリンク:

1- http://www.slideshare.net/nateabele/lithium-the-framework-for-people-who-hate-frameworks

2-ほぼ同じプレゼンテーションのビデオ(フォームテンプレートの変更に関する良い例付き)

于 2012-05-02T14:19:47.727 に答える