【问题标题】:Add function in cakephp suddenly stopped workingcakephp 中的添加函数突然停止工作
【发布时间】:2014-10-23 15:48:11
【问题描述】:

我是 cakePHP 新手,所以我可能遗漏了一些明显的东西。

我有一个正在工作并保存到数据库的添加表单,但突然停止了。我认为我没有添加任何重要的东西,主要是样式化的东西......不幸的是,我的最后一次备份是在我完成添加功能之前,所以我没有办法回到它工作的时候。如果有人可以看看我哪里出错了,我将不胜感激!

我的任务模型:

App::uses('AuthComponent', 'Controller/Component');

class Task extends AppModel {

public $belongsTo = 'User';

public $validate = array(
    'task_name' => array(
        'custom' => array(
            'rule' => array('custom', '/^[a-z0-9 ]*$/i'),
            'required' => true,
            'message' => 'This field accepts letters and numbers only.'
        ),
        'maxLength' => array(
            'rule' => array('maxLength', 50),
            'message' => 'Task name cannot exceed 50 characters'
        )
    ),
    'frequency' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'day' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => 'numeric'
        )
    ),
    'month_type' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    ),
    'month_number' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    ),
    'month_day' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'allowEmpty' => true
        )
    ),
    'day_number' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'required' => false
        )
    )
);
}

我的 TasksController 文件中的添加函数:

public function add() {
    if ($this->request->is('post')) {   
        $this->Task->create();
        if ($this->Task->save($this->request->data)) {
            $this->Session->setFlash(__('Task saved!'));
            $this->redirect(array('action' => 'index'));
        } 
        else {
            $this->Session->setFlash(__('The task could not be saved'));
        }   
    }
}

还有我的 tasks/add.ctp 文件中的表单:

<?php 
echo $this->Form->create('Task',array('class' => 'taskForm'));
$userId = $this->Session->read('Auth.User.id');?>

<fieldset>
    <legend class="welcomeText"><?php echo __('Add a Task'); ?></legend>
    <?php 

        echo $this->Form->hidden('user_id', array('value' => $userId));

        echo $this->Form->input('task_name', array('label' => 'Task Name', 'maxLength' => 50));

        //set frequency
        echo "<p>How often should this task be done?</p>";
        $options = array('unset' => 'Decide Later','daily' => 'Daily','weekly' => 'Weekly','monthly' => 'Monthly');
        $attributes = array('value' => 'unset','separator' => '<br/>','class' => 'frequencyRadio','legend' => false);
        echo $this->Form->radio('frequency', $options, $attributes);

        //optional answers

        //if "weekly" is selected
        echo "<div id=\"weeklyRadio\" >";
        echo "<p>Set a day of the week for this task?</p>";
        $options = array('unset' => 'Decide later','monday' => 'Monday','tuesday' => 'Tuesday','wednesday' => 'Wednesday',
        'thursday' => 'Thursday','friday' => 'Friday','saturday' => 'Saturday','sunday' => 'Sunday');
        $attributes = array('value' => 'unset','separator' => '<br/>','class' => 'weeklyRadio','legend' => false);
        echo $this->Form->radio('day',$options,$attributes);
        echo "</div>"; 

        //if "monthly" is selected
        ?>
        <div id="monthlyRadio">
            <p>Schedule this task?</p>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeUnset" 
                value="unset" class="monthlyRadio" required="required" checked="checked" />
            <label for="TaskMonthTypeUnset">Decide later</label><br/>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeNumber" 
                value="number" class="monthlyRadio" required="required" />
            <label for="TaskMonthTypeNumber">
                <?php

                    echo "On the ";
                    $options = array(1 => '1st',2 => '2nd',3 => '3rd',4 => '4th',5 => '5th',6 => '6th',7 => '7th',8 => '8th',9 => '9th',
                            10 => '10th',11 => '11th',12 => '12th',13 => '13th',14 => '14th',15 => '15th', 16 => '16th',
                            17 => '17th',18 => '18th',19 => '19th',20 => '20th',21 => '21st',22 => '22nd',23 => '23rd',
                            24 => '24th',25 => '25th',26 => '26th',27 => '27th',28 => '28th',29 => '29th',30 => '30th',31 => '31st');                       
                    echo $this->Form->select('month_number',$options,array('value' => null));
                    echo " of the month";
                ?>
            </label><br/>
            <input type="radio" name="data[Task][month_type]" id="TaskMonthTypeDay" value="day" class="monthlyRadio" required="required" />
            <label for="TaskMonthTypeDay">
                <?php

                    echo "On the ";
                    $options = array(1 => '1st',2 => '2nd',3 => '3rd',4 => '4th',5 => '5th',6 => 'last');
                    echo $this->Form->select('day_number',$options,array('value' => null));
                    echo "&nbsp";
                    $options = array('monday' => 'Monday','tuesday' => 'Tuesday','wednesday' => 'Wednesday',
                            'thursday' => 'Thursday','friday' => 'Friday','saturday' => 'Saturday','sunday' => 'Sunday');
                    echo $this->Form->select('month_day',$options,array('value' => null));
                    echo " of the month";
                ?>
            </label>
        </div>




    <?php 
    echo $this->Form->submit('Add Task', array('class' => 'formSubmit',  'title' => 'Create Task') ); 
?>
</fieldset>
<?php echo $this->Form->end(); ?>

还有一个显示和隐藏可选单选按钮的 javascript 文件,但我认为这与它停止保存到数据库的原因没有任何关系。

当我单击表单上的“添加任务”按钮时,它根本不执行任何操作(不保存到数据库,也不像以前那样重定向到 index.ctp 视图)。不知道我在这里错过了什么!

更新

解决了问题!我在表单中指定默认值为 null 的位置,它传递了一个空字符串。数据模型需要 'month_number' 和 'day_number' 的数值,所以它没有处理。

我通过在选择列表中添加 0 => '' 并指定 0 作为默认值来修复它,现在它可以正常工作了。

感谢您的帮助!

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    我没有在您的代码中看到错误。 将此添加到控制器中添加并进行调试以获取有关发生情况的更多信息。

    public function add() {
        $this->loadModel('Task');
        debug($this->request->is('post')); //try this first
        //debug($this->request); //after try this
        if ($this->request->is('post')) {   
            $this->Task->create();
            if ($this->Task->save($this->request->data)) {
                $this->Session->setFlash(__('Task saved!'));
                $this->redirect(array('action' => 'index'));
            } 
            else {
                $this->Session->setFlash(__('The task could not be saved'));
            }   
        }
    

    }

    loadModel 添加了这个。使用显示调试更新您的问题,然后我更新 mi 答案。

    【讨论】:

    • 谢谢。我用调试代码运行它,它给了我一个“真”的值。
    • 现在在调试中($this->request->data);只是为了看看值是否有错误
    • 修复了!问题是,当我指定默认值 null 时,它传递的是一个空字符串。该模型需要其中几个的数值,所以它没有处理。
    • 好吧,如果你能解决你的问题,根据我说的调试程序,解决。为我服务,直到我回答 7 个问题才能问。如果不是这样。 >)。
    【解决方案2】:

    看起来您将提交表单按钮分配给了不同的类,这就是它不触发表单的原因。

    echo $this->Form->submit('Add Task', array('class' => 'formSubmit',  'title' => 'Create Task') );
    

    尝试将其更改为以下任一

    echo $this->Form->submit('Add Task', array(
    'class' => 'taskForm',  
    'title' => 'Create Task')); 
    
    echo $this->Form->button('Add Task', array(
    'class' => 'taskForm',  
    'title' => 'Create Task',
    'action' => 'submit;));  
    

    或修改表单结束标记以查看它是否正确触发。

    改变这个

    echo $this->Form->end();
    

    echo $this-Form->end('Submit');
    

    【讨论】:

    • 我尝试了这两个建议,但不幸的是我仍然遇到同样的问题。 formSubmit 类是为了让我可以设置按钮的样式,但我把它拿出来以防它引起问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2021-02-01
    • 2016-12-29
    • 2018-05-09
    • 2015-12-07
    • 2015-08-23
    相关资源
    最近更新 更多