php - Lithium form helper 数据和错误缺失?

标签 php php-5.3 lithium

我正在构建一个测试 Lithium 应用程序以了解它的工作原理,我发现表单助手似乎无法识别我传回的数据或任何验证错误。

目前我不得不手动传回我的错误,然后在 View 中处理它们。

QuestionsController::询问

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

views/questions/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>所以 helper 中有显示验证消息的能力。

那么我如何在我的 Controller 中组织我的数据,以便它被传递回 View ,以便帮助程序填充我的字段并显示错误?

编辑
我应该补充一点,示例“Sphere”应用程序也使用了这种方法,所以它是标准的吗? ( ref )

最佳答案

长话短说

简短的回答是您可以将表单绑定(bind)到 Entity 的子类,即 RecordDocument如图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 - Video of roughly the same presentation (w/ a good example on changing form templates)

关于php - Lithium form helper 数据和错误缺失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412664/

相关文章:

error-handling - 为什么 li3 HTML 助手有时不能生成正确的路径?

php - 锂通用型号过滤器

php - 如何删除magento2数据库中的所有产品数据? **不可能**

php-5.3 - 成功执行worker后获取数据 gearman

php - 将字符串转换为数组 PHP

PHP Magic 比简单地设置类属性更快?

php - 禁用锂中的渲染过程

php - 无法使用表单发布到数据库

php - 使用php-gd循环从图像的每个像素获取imagecolorat()数据

php - 如何在html表中打印具有 "one to many"关系的两个表的列?