CakePHP 2.1:以平滑的方式验证来自元素的表单? [英] CakePHP 2.1: Validating forms coming from elements in a smooth way?
问题描述
我有以下情况。
帖子有多条评论
评论belongsTo Posts
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屋!