相关模型未经过验证

发布于 2024-10-08 19:38:56 字数 2519 浏览 1 评论 0原文

我目前有以下模型:

class Category extends AppModel {
    var $name = 'Category';
    /*var $validate = array(
        'name' => 'multiple'
    );
        no idea how to use this
        */
    var $hasAndBelongsToMany = array(
        'Post' => array(
            'className' => 'Post'
        )
    );

class Post extends AppModel {
    var $name = 'Post';
    var $hasAndBelongsToMany = array(
        'Category' => array(
            'className' => 'Category'
        )
    );
    var $belongsTo = array(
        'Page' => array(
            'className' => 'Page'
        )
    );

class Page extends AppModel {
    var $name = 'Page';
    var $order = array('Page.modified' => 'desc');

    var $hasOne = array(
        'Post' => array(
            'className' => 'Post'
        ));

我的视图中也有此表单:

<div id="content-wrap">
    <div id="main">
            <h2>Add Post</h2>
            <?php echo $this->Session->flash();?>
            <div>
            <?php
            echo $this->Form->create('Post');
            echo $this->Form->input('Post.title');
            echo $this->Form->input('Category.Category', array('multiple' => 'checkbox'));
            echo $this->Form->input('Post.body', array('rows' => '3'));

            echo $this->Form->input('Page.meta_keywords');
            echo $this->Form->input('Page.meta_description');

            echo $this->Form->end('Save Post');
            ?>
            </div>
    <!-- main ends -->
    </div>

我的控制器:

function admin_add() {
    // pr(Debugger::trace());
    $this->set('categories', $this->Post->Category->find('list'));

    if ( ! empty($this->data)) {

        $this->data['Page']['title'] = $this->data['Post']['title'];
        $this->data['Page']['layout'] = 'index';

        if ($this->Post->saveAll($this->data)) {
            $this->Session->setFlash('Your post has been saved', 'flash_good');
            $this->redirect($this->here);
        }
    }
}

我遇到的问题是我可以保存帖子而不选择类别。

我尝试将以下内容作为规则添加到类别模型中:

var $validate = array(
    'rule' => array('multiple', array('in' => array(1, 2, 3, 4))),
        'required' => TRUE,
        'message' => 'Please select one, two or three options'
);

帖子和页面模型验证。 如何激活类别验证?

I currently have the following models:

class Category extends AppModel {
    var $name = 'Category';
    /*var $validate = array(
        'name' => 'multiple'
    );
        no idea how to use this
        */
    var $hasAndBelongsToMany = array(
        'Post' => array(
            'className' => 'Post'
        )
    );

class Post extends AppModel {
    var $name = 'Post';
    var $hasAndBelongsToMany = array(
        'Category' => array(
            'className' => 'Category'
        )
    );
    var $belongsTo = array(
        'Page' => array(
            'className' => 'Page'
        )
    );

class Page extends AppModel {
    var $name = 'Page';
    var $order = array('Page.modified' => 'desc');

    var $hasOne = array(
        'Post' => array(
            'className' => 'Post'
        ));

I also have this Form in the view:

<div id="content-wrap">
    <div id="main">
            <h2>Add Post</h2>
            <?php echo $this->Session->flash();?>
            <div>
            <?php
            echo $this->Form->create('Post');
            echo $this->Form->input('Post.title');
            echo $this->Form->input('Category.Category', array('multiple' => 'checkbox'));
            echo $this->Form->input('Post.body', array('rows' => '3'));

            echo $this->Form->input('Page.meta_keywords');
            echo $this->Form->input('Page.meta_description');

            echo $this->Form->end('Save Post');
            ?>
            </div>
    <!-- main ends -->
    </div>

My Controller:

function admin_add() {
    // pr(Debugger::trace());
    $this->set('categories', $this->Post->Category->find('list'));

    if ( ! empty($this->data)) {

        $this->data['Page']['title'] = $this->data['Post']['title'];
        $this->data['Page']['layout'] = 'index';

        if ($this->Post->saveAll($this->data)) {
            $this->Session->setFlash('Your post has been saved', 'flash_good');
            $this->redirect($this->here);
        }
    }
}

The problem I am having is that I could save a Post without choosing a category for it.

I've tried adding the following as a rule to the Category Model:

var $validate = array(
    'rule' => array('multiple', array('in' => array(1, 2, 3, 4))),
        'required' => TRUE,
        'message' => 'Please select one, two or three options'
);

The Post and Page Model validates.
How do I activate validation for the Category?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-10-15 19:38:56

首先,您没有正确设置 $validate 变量。 $validate 数组中的键必须是字段名称。其次,multiple 规则用于检查字段的值是否位于一组值内。

var $validate = array(
    'color' => array(
        'multiple' => array(
            'rule' => array('multiple', array('in' => array('Red', 'Blue', 'Green'))),
            'required' => false,
            'message' => 'Please select one, two or three options'
         ),
    ),
);

我检查了书中的示例中的multiple,发现那里有一个拼写错误。上面的代码是正确的。

接下来,如果您想验证相关模型,我建议您在 beforeSave() 函数中执行此操作:

function beforeSave(){
    if (isset($this->data['Category']['Category']) && empty($this->data['Category']['Category'])) {
        return false;
    }
    return true;
}

在这里,从 beforeSave() 返回 false 将阻止保存进行。因此,您将成功验证您的要求。

First off, you haven't set up the $validate variable properly. The keys in the $validate array must be field names. Secondly, the multiple rule is used to check if the value(s) of a field lies within a set of values.

var $validate = array(
    'color' => array(
        'multiple' => array(
            'rule' => array('multiple', array('in' => array('Red', 'Blue', 'Green'))),
            'required' => false,
            'message' => 'Please select one, two or three options'
         ),
    ),
);

I checked the example in the book for multiple and it's a typo there. The above code is correct.

Next, if you want to validate related models, I suggest you do that in your beforeSave() function:

function beforeSave(){
    if (isset($this->data['Category']['Category']) && empty($this->data['Category']['Category'])) {
        return false;
    }
    return true;
}

Here, returning false from the beforeSave() would prevent the save from going through. So, you will have successfully validated your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文