【问题标题】:how to get the substring in yii如何在yii中获取子字符串
【发布时间】:2013-01-03 08:54:02
【问题描述】:
    <tr style="display:none">       
  <td><?php echo $form->labelEx($model,'classcode'); ?></td>
        <td>    
            <?php echo $form->textField($model,'classcode',array('size'=>60,'maxlength'=>100,'id'=>'new-base-ref-si-classification-classcode','disabled'=>'')); ?>
            <?php echo $form->error($model,'classcode'); ?>

        </td>
    </tr>   
    <tr>
        <td><?php echo $form->labelEx($model,'Classification'); ?></td>
        <td>    
            <?php echo $form->textField($model,'Classification',array('size'=>60,'maxlength'=>100,'id'=>'new-base-ref-si-classification-Classification','disabled'=>'')); ?>
            <?php echo $form->error($model,'Classification'); ?>
        </td>
    </tr>   

我很难弄清楚该怎么做,请帮助我..我需要的是隐藏类代码..当我输入一个字符串时,它的代码将获得第一个字母,然后它将被输入点击提交按钮之前的类代码

例如 我在分类文本字段中输入了办公用品 代码将得到 O 和 S 然后在提交之前将其输入到 classcode texfield

【问题讨论】:

    标签: php yii substring textfield


    【解决方案1】:

    由于此字段不显示并且根本不会接收用户输入,因此您不需要在视图文件中使用它。

    最好的方法是将类代码设置为不安全,这样就不会被大量分配

    然后在你的模型中提交后在服务器端计算:

    public function rules()
    {
        return array(
            // ... snip ... other rules here
            array('classcode', 'unsafe'), // will not be massively assigned, prevents hacks
            array('Classification', 'filterClasscode'), // custom validator
        );
    }
    
    /**
     * Custom filter to set classcode
     * @param string $attribute field to set classcode from
     */    
    public function filterClasscode($attribute)
    {
        $this->classcode = '';
        foreach (explode(' ', $this->$attribute) as $val) {
            $this->classcode .= substr($val, 0, 1);
        }
    
        $this->classcode = strtoupper($this->classcode);
    }
    

    【讨论】:

    • 它工作得很好,谢谢你的帮助,虽然我必须改变 => 来工作,但这真的很好谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2011-10-04
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多