gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:33:30 31 4
gpt4 key购买 nike

我正在构建一个测试 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com