【问题标题】:CodeIgniter error- Message: Array to string conversionCodeIgniter 错误 - 消息:数组到字符串的转换
【发布时间】:2014-10-25 21:47:07
【问题描述】:

我有一个非常简单的方法,旨在从表单中添加验证规则(如果没有 ID,则需要密码,否则不需要 - 新用户与更新用户)。

public function edit($id = NULL){
  $rules = $this->user_m->rules;
  $id || $rules['password'] .= '|required';
  $this->form_validation->set_rules($rules);
  $this->load->view('_layout_admin_main',$this->data);
}

$rules 在模型中设置为(虽然我看不出它是如何相关的):

public $rules = array(
        'name' => array(
                'field' => 'name',
                'label' => 'Name',
                'rules' => 'trim|required|xss_clean'
        ),
        'email' => array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
        ),
        'password' => array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'trim|matches[password_c]'
        ),
        'password_c' => array(
                'field' => 'password_c',
                'label' => 'Password Confirmation',
                'rules' => 'trim|matches[password]'
        ),
        'username' => array(
                'field' => 'username',
                'label' => 'Username',
                'rules' => 'trim|is_unique|xss_clean|required'
        )
    );

错误是 Message: Array to string conversion ... Line Number: 27$id 引用是在 $idNULL 时引起的。当设置$id 时,它很好而且花花公子。 $id 绝对不是数组。

我知道这很模糊,但实际上就是这样。如果您有任何想法,请提出问题,我会发布您需要的更多内容。

【问题讨论】:

  • 我认为是$rules['password'] .= '|required'; 导致了问题。 rules 是一个数组,其中 required 是字符串。您正在尝试将字符串与数组连接起来。我不确定你,但我也想知道是否有人为你解决了我也很想知道实际问题是什么。

标签: php codeigniter


【解决方案1】:

我认为您将required 连接到规则的方式存在一个小问题,请尝试以下方法:

public function edit($id = NULL){
  $rules = $this->user_m->rules;
  $id || $rules['password']['rules'] .= '|required'; // issue was here
  $this->form_validation->set_rules($rules);
  $this->load->view('_layout_admin_main',$this->data);
}

【讨论】:

  • 不敢相信这是显而易见的事情。谢谢@MaxiWheat!
猜你喜欢
  • 1970-01-01
  • 2012-12-02
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多