CakePHP 2.1: Validating forms coming from elements in a smooth way?

流过昼夜 提交于 2019-12-12 17:49:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11347098/cakephp-2-1-validating-forms-coming-from-elements-in-a-smooth-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!