【问题标题】:Field being split on input, not output字段在输入上被分割,而不是输出
【发布时间】:2016-02-11 21:14:13
【问题描述】:

我正在使用这个功能

public $year;
public $month;
public $day;

protected function afterFind() {
    parent::afterFind();

    $dob = explode('/', $this->dob);
    $this->year = $dob[0];
    $this->month = $dob[1];
    $this->day = $dob[2];

    return $this;
}

protected function beforeSave() {
    $this->dob = $this->year .'/'. $this->month .'/'. $this->day;
}

<div class="col-md-2 dayForm noPadding"> 
<?php
echo $form->textFieldGroup(
    $user, 
    'day', 
    array(
        'widgetOptions' => array(
            'htmlOptions' => array(
                'placeholder' => 'DD'
            )
        )
    )
);
?> 
</div>

<div class="col-md-3 monthForm ">
<?php
echo $form->dropDownListGroup(
    $user,
    'month',
    array(
        'widgetOptions' => array(
            'data' => array('01' => 'January' , '02' => 'February' , '03' => 'March' , '04' => 'April' , '05' => 'May' , '06' => 'June' , '07' =>'July' , '08' =>'August' , '09' =>'September' , '10' =>'October' , '11' =>'November' , '12' =>'December'),
            // 'data' => 'Jan','Feb';
            'htmlOptions' => array(
                'class' => 'col-md-3 ',
                'prompt' => 'Choose month',
            ),
        )
    )
);
?>
</div>

<div class="col-md-2 yearForm noPadding"> 
<?php
echo $form->textFieldGroup(
    $user, 
    'year', 
    array(
        'widgetOptions' => array(
            'htmlOptions' => array(
                'placeholder' => 'YYYY',
                'class' => 'col-md-3',
            )
        )
    )
);
?> 
</div>

将出生日期字段拆分为 3 个单独的字段。很简单,用户输入日期、月份和年份,并以正确的格式输入。我遇到的问题是,当用户去更新整个 dob 字段时,所有字段都显示在 year textfieldgroup 中,不太好。

如何在退出时将其重新展开,以便所有相应的字段都出现在它们的 textfieldgroups / dropdownlist 组中?

【问题讨论】:

  • remove return $this; in after find 并将parent::afterFind() 放在函数的最后一行
  • 所以完全删除 return $this 并在最后添加 parent::afterfind() ?尝试了这个,它没有改变。有什么想法吗?
  • 你能发布控制器代码吗?
  • 我应该在控制器中寻找什么?我有 actionCreate、actionView 和 actionUpdate
  • 您遇到问题的操作,即我认为的更新

标签: php yii


【解决方案1】:

我总是尽量避免使用afterFind,因为它会使事情变得复杂,难以解决像您这样的问题。您通常可以通过像这样添加 getter(以及可选的 setter)来获得类似的结果;

protected function getDobParts()
{
    return explode('/', $this->dob);
}
protected function changeDobPart($part, $newValue)
{
    $parts = $this->getDobParts();
    $parts[$part] = $newValue;
    $this->dob = implode('/', $parts);
}
public function getYear()
{
    return $this->getDobParts()[0];
}
public function setYear($value)
{
    $this->changeDobPart(0, $value);
}
public function getMonth()
{
    return $this->getDobParts()[1];
}
public function setMonth($value)
{
    $this->changeDobPart(1, $value);
}
public function getDay()
{
    return $this->getDobParts()[2];
}
public function setMonth($value)
{
    $this->changeDobPart(2, $value);
}

Yii 会自动将这些 getter/setter 作为属性提供(例如 $class->year)。

您的视图代码应该保持不变。

ps。在覆盖父方法时返回它们的结果通常是最佳实践。例如beforeSave() 的最后一行通常应该是return parent::beforeSave();。如果您不这样做,您的课程甚至可能无法正确保存。

【讨论】:

  • 这个逻辑看起来不错,但我在这一行遇到错误,public function getYear() { return $this->getDobParts()[0]; }
  • 看起来代码示例以两个 setMonths 结尾,将第二个重命名为 setDay。如果这仍然不起作用,那么您介意告诉我您的 PHP 版本吗? (php -v)
  • 是的,我修复了我看到的,php版本是5.6.3,它仍然默认dob字段为最后一个字段“year”
  • 您确定您的日期格式是 yyyy/mm/dd 而不是 yyyy-mm-dd?
  • 我应该提到,使用 - 或 / 在函数内部使用它时没有区别,它仍然输入正常但不管我使用什么,它要么不输入回textifelds 或在年份字段中填写。
猜你喜欢
  • 2013-08-26
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多