CakePHP 2.1:以平滑的方式验证来自元素的表单? [英] CakePHP 2.1: Validating forms coming from elements in a smooth way?

查看:91
本文介绍了CakePHP 2.1:以平滑的方式验证来自元素的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况。
帖子有多条评论



评论belongsTo Posts



在我的/ Views /显示帖子及其注释。此外,对于每个帖子,应显示评论表。因此,我必须使用元素add_comment.ctp到视图(如果我错了,请纠正我,但看到

/Views/Posts/view.ctp:

  //添加注释
echo $ this - >元素('add_comment',array('post_id'=> $ entry ['Post'] ['id']),array('cache'=> array('config'=>'long' ;

元素:

  / ** 
*添加评论的元素
*
* /
echo $ this - >形式 - > create('Comment',array('url'=> array(
'controller'=>'comment',
'action'=>'add',
$ post_id
)));
?>
< fieldset>
< legend><?php echo'添加注释'; ?>< / legend>
<?php
echo $ this - >形式 - > input('author_name');
echo $ this - >形式 - > input('author_email',array('type'=>'email required'));
echo $ this - >形式 - > input('author_website');
// echo $ this-> Form-> input('date_published');
echo $ this - >形式 - > input('text');
// echo $ this-> Form-> input('is_archived');
?>
< / fieldset>
<?php echo $ this - >形式 - > end(array('label'=>'Post!')); ?>

可以看到,表单被提交到CommentsController的添加操作。
现在,一个大问题:添加操作如何实际传递数据,如验证结果回到窗体?我的意思是,表单数据也应该被持久化,所以如果有人输入无效数据,它不会丢失。



通常,添加操作会渲染/查看/注释/添加,但我既不需要这个视图也没有定义一个。



到目前为止,我已经使用$ this-> redirect在注释保存后返回/ Views / Posts /视图 - 但重定向只是调用/视图/帖子/视图而不传递任何东西。那么如何使用元素组合平滑和自动表单处理?

解决方案

我认为如果你重定向后提交,它将丢失任何 $ this-> invalidFields(),这基本上触发验证错误等。

:您可以 重定向并渲染再次在您的 comments / add.ctp 视图中再次显示,因此表单将自动显示验证错误。然后,一旦保存成功并且验证通过后,再次将流程平滑重定向回您的帖子(您应该有Post ID)。



2 :此外,您可以在Comment模型中添加注释保存逻辑,然后在Post View操作中检查注释POST(更新表单以指向此操作),并调用您在其中创建的保存函数 $ this-> Post-> Comment-> saveMethodDefinedInModel($ this-> request-> data ['Post']);



3:或者....你可以选择一个选项,然后将它与ajax组合,这是非常粗糙的。


I have the following situation. Posts hasMany Comments

Comments belongTo Posts

In my /Views/Posts/view, I display the Post with its Comments. Also, with every Post, a Comment Form should be displayed. Therefore, I have to use an Element add_comment.ctp to the View (correct me please if I am wrong, but see this question here).

/Views/Posts/view.ctp:

// add comments
echo $this -> element('add_comment',array('post_id' => $entry['Post']['id']), array('cache' => array('config' => 'long')));

The element:

/**
 * Element for adding comments
 *
 */
echo $this -> Form -> create('Comment', array('url' => array(
        'controller' => 'comments',
        'action' => 'add',
        $post_id
    )));
?>
    <fieldset>
        <legend><?php echo 'Add Comment'; ?></legend>
    <?php
    echo $this -> Form -> input('author_name');
    echo $this -> Form -> input('author_email', array('type' => 'email required'));
    echo $this -> Form -> input('author_website');
    //echo $this->Form->input('date_published');
    echo $this -> Form -> input('text');
    //echo $this->Form->input('is_archived');
    ?>
    </fieldset>
<?php echo $this -> Form -> end(array('label' => 'Post!')); ?>

As you can see, the form is submitted to the add action of CommentsController. Now, the big question: How can the add action actually pass data such as Validation results back to the form? I mean, form data should also be persisted, so in case somebody entered invalid data, its not lost.

Normally, the add action would render /View/Comments/add, but I neither need this view nor have I even defined one.

So far I have used $this->redirect to go back to the /Views/Posts/view after the Comment was saved -- but redirecting just calls /Views/Posts/view without passing anything. So how can I make use of Elements in Combination with smooth and automagic form handling?

解决方案

I think if you're redirecting after the submit, it will lose any of the $this->invalidFields() which basically triggers the validation errors and such. There are a couple of ways around this....

1: You could not redirect and render the form once again in your comments/add.ctp view so the form will be displayed with validation errors automatically. Then to make the process smooth again redirect back to your post (you should have the Post ID somehow) once the save is successful and validation has passed.

2: Also You could put the comment saving logic in the Comment model, then check for a comment POST in your Post View action (update form to point to this action) and call the save function you create there with $this->Post->Comment->saveMethodDefinedInModel($this->request->data['Post']);.

3: Or.... You could take option one and combine it with ajax, that'd be rather gnarly.

这篇关于CakePHP 2.1:以平滑的方式验证来自元素的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆