Forms 将此选择转换为CakePHP友好选择

Forms 将此选择转换为CakePHP友好选择,forms,validation,cakephp-2.2,Forms,Validation,Cakephp 2.2,我有以下模型关系 Post belongsto PostCategory PostCategory belongsto Forum PostSubcat belongsto PostCategory PostCategory hasmany Post PostCategory hasmany PostSubcat Forum hasmany PostCategory. 我正在使用ForumsController add()方法,无法使用标准的查找(“列表”)来显示用户可以添加帖子的所有论坛、

我有以下模型关系

Post belongsto PostCategory
PostCategory belongsto Forum
PostSubcat belongsto PostCategory

PostCategory hasmany Post
PostCategory hasmany PostSubcat
Forum hasmany PostCategory.
我正在使用ForumsController add()方法,无法使用标准的查找(“列表”)来显示用户可以添加帖子的所有论坛、类别和子类别。例如,下拉列表应该看起来像

- Forum #1 Title
    - Category #1.1 Title
        - Subcat #1.1 Title
        - Subcat #1.2 Title
    - Category #1.2 Title
    - Category #1.3 Title
        - Subcat #2.1 Title
        - Subcat #2.2 Title
- Forum #2 Title
    - Category #2.1 Title
    - Category #2.2 Title
所以我做了一个查找(‘全部’)就像这样

$forums = $this->Forum->find('all', array(
    'fields' => array('Forum.title'),
    'conditions' => array('Forum.page_id' => $page_id),
    'contain' => array(
        'PostCategory' => array(
            'fields' => array(
                'PostCategory.id', 'PostCategory.title'
            ),
            'PostSubcat' => array(
                'fields' => array(
                    'PostSubcat.id', 'PostSubcat.title'
                ),
            )
        )
    )
));

$this->set('forums', $forums);
My add.ctp forum有以下代码用于选择

<?php if($forums): ?>
    <div class="input select required"><label for="PostCategoryId">Category</label>
    <select name="data[Post][category_id]" id="PostCategoryId">
        <option value="">Select a Category</option>
        <?php foreach($forums as $forum): ?>
            <?php if(!empty($forum['PostCategory'])): ?>
                <option value="" class="SelectSeparator"><?php echo $forum['Forum']['title']; ?></option>
                <?php foreach($forum['PostCategory'] as $category): ?>
                   <?php if($category_id == $category['id']){
                       $selected = "selected";
                   }else{
                       $selected = "not";
                   } ?>
                   <option value="<?php echo $category['id']; ?>" <?php echo $selected;?>><?php echo $category['title']; ?></option>
                   <?php foreach($category['PostSubcat'] as $subcat): ?>
                       <?php if($category_id == $subcat['id']){
                           $selected = "selected";
                       }else{
                           $selected = "not";
                       } ?>
                       <option value="<?php echo $subcat['id']; ?>"<?php echo $selected;?>>-- <?php echo $subcat['title']; ?></option>
                   <?php endforeach; // subcat?>
               <?php endforeach; // category ?>
           <?php endif; // if forum has categories ?>
       <?php endforeach; // forum?>
   </select> 
   </div>
<?php endif; // if there are forums ?>

类别
选择一个类别
这将以我希望的方式显示选择选项,并正确验证——如果用户选择论坛标题或选择“选择类别”而不是类别标题,则不会添加帖子。但是,它不是蛋糕友好格式,因此不会显示验证错误


是否有一种方法可以轻松地将其转换为CakePHP表单->输入帮助程序,以便正确显示验证错误消息?

逻辑问题:当您想在add函数中添加论坛/类别/子类别时,如果您只有id,如何识别哪个是哪个?(我知道这与你的问题无关,但我很好奇你是如何处理这个问题的。)类别和子类别来自同一个表posts\u类别。该表有一个名为parent_id的列,如果您要向子类别添加某个内容,则可以获取其父类别。它还有一个名为filter_id的列,在my ForumsController中,filter_id是表forums的外键。我有其他控制器,其中文件管理器\u id将与其他内容相关,因此筛选器\u id听起来像是一个很好的中性名称。您的帖子模型中是否有类别\u id的验证?是的,我的帖子模型有类别\u id的数字规则,但allowed empty为true,required为false。因此,类别id可以为空,但如果已填写,则必须为数字。但在我的论坛控制器中,我取消了category_id的验证规则,并将其完全设置为上面的方式。验证工作正常,只是没有显示错误消息。如果我使用标准的find('list')和this->Form->input('category_id'),那么将显示验证消息