不插入空值

时间:2016-07-14 08:32:51

标签: php html yii2 isset

我正在尝试在表单的todo输入中插入多个值。但是如果我没有为任何输入输入值,则在数据库中插入空值。这是我的控制器动作:

             -E'  <- branch y
            /
A--B'--C'--D'   <- branch x

表格是:

public function actionAdd()
{       
    if (Yii::$app->request->isAjax) {
        $request = Yii::$app->request;
        $add = new Project();
        $add->project_name = $request->post('project');
        $add->deadline = $request->post('deadline');
        $add->profile_id = $request->post('profile_id');
        $add->project_status = "Running";
        $add->save();
        $getlast = Yii::$app->db->getLastInsertId();
        $todo = $request->post('todo');

        if (isset($todo)) {
            foreach ($todo as $to) {
                $add = new Todo();
                $add->todo_name=$to;
                $add->status="Running";
                $add->project_id=$getlast;
                $add->save();
            }
        }
        echo json_encode(TRUE); die;
    }
    echo json_encode(FALSE);die;
}

jquery是:

<form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/todo/add/" role="form"  id="register-form" novalidate="novalidate">

                  <label>    Project Name: </label>       <input type="name" name="project" class="form-control"  placeholder="Project Name" required><br><br>
                  <input type="hidden" name="profile_id" value="<?php echo $profile_id;?>">
                  <label>    Todo: </label><br>
                  <textarea type="text" name="todo[]" placeholder="Todo Description..."></textarea>
                  <div id="dynamicInput">
                  <span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
                    </div><br>
                  <label> Project Deadline:</label><br>
                  <input type="date" name="deadline"><br><br>
                  <button type="submit" class="btn btn-default">Add Project</button><BR><BR>                      
        </form>  

1 个答案:

答案 0 :(得分:0)

你的问题可能与验证规则有关..

检查此项并仅用于调试尝试使用$ add-&gt; savel(false)

         $add = new Todo();
            $add->todo_name=$to;
            $add->status="Running";
            $add->project_id=$getlast;
            $add->save(false);

如果在这种情况下插入行检查验证规则(最终评论临时)..用于查找错误的规则或条件