在 Twitter bootstrap 中提交空值

submit null values in Twitter bootstrap Datepicker

我正在使用 codeigniter 和 doctrine.php,我被困在这个地方,当我不 select 来自日期选择器的任何值时,它应该在数据库中提交空值,但它发送昨天的日期每次都这样。

function initDatePicker(datePickerID, dateInput){
    $( "#" + datePickerID )

    // Initializing the datepicker
    .datepicker()

    // Detecting the on change date event
    .on('changeDate', function (data) {
      var date = new Date(data.date);
      var dateString = date.getDate() + '-' + parseInt(date.getMonth() + 1) + '-' + date.getFullYear();
      $("#" + dateInput).val(dateString);
    });
}

控制器

 $salesOrder->setDateOfBilling($post_data['date_of_billing']);
  if($date_of_billing)
  {
  $salesOrder->setBillingStatus("Paid");
}
else
{
  $salesOrder->setBillingStatus("Not Paid");
}
  $salesOrder->setDateOfPayment($post_data['date_of_payment']);

  $em->persist($salesOrder);
  $em->flush();

型号

/**
 * @Column(type="datetime", nullable=true)
 */
protected $date_of_billing;

public function getDateOfBilling()
{
    return $this->date_of_billing;
}

public function setDateOfBilling($date_of_billing)
{
    $this->date_of_billing = new \DateTime($date_of_billing);
}

查看 这是我使用日期选择器的表格

<div class="form-group">
<label class="col-sm-4 control-label">Date of Billing</label>
<div class="col-sm-5">
  <input type="text" name="date_of_billing" id="date_of_billing" class="form-control" placeholder="Date Of Billing" 

  readonly>
  <div id="billing_date_picker"></div>
</div>

可能是这里的问题“$( "#" + datePickerID )

试试这个:

var dpid = "#" + datePickerID;
$(dpid).datepicker();

在上述代码及其工作的控制器和模型中得到 answer.did 更改...

控制器

$billing_date = $post_data['date_of_billing'] === "" ? null : $post_data['date_of_billing'];
  echo $billing_date;
  $salesOrder->setDateOfBilling($post_data['date_of_billing']);

  $salesOrder->setBillingStatus("Paid");

型号

public function setDateOfBilling($date_of_billing)
{
    if ($date_of_billing == null) {
        $this->date_of_billing = null;
    } else {
        $this->date_of_billing = new \DateTime($date_of_billing);    
    }
}