不插入空值


Not inserting null values

我正在尝试在表单的待办事项输入中插入多个值。 但是,如果我不在任何输入中输入值,则空值将插入到数据库中。这是我的控制器操作:

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;
}

表格为:

<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>  

jquery是:

var counter = 1;
var limit = 10;
function addInput(divName)
{ 
  if (counter != limit) 
   {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<br><textarea type='text' name='todo[]' placeholder='Todo Description...'></textarea>";
          document.getElementById(divName).appendChild(newdiv);    
          counter++;     
    }
}
可能是

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

要检查这一点并且仅用于调试,请尝试使用 $add->savel(false)

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

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