私はCIに新しいものではないですが、何か違うことをしようと私のコントローラからの私の検証を動かす(ありますたくさんおよびその取得乱雑)。\CodeIgniterの3のコントローラ/メソッドバリデーション
私が使用しようとしています方法は、私がdocumentation(何度も)を読んでいると私は他の見ている、それはあなたが$this->form_validation->run()
を実行する場所に基づいてルールを自動的にロードする必要がありますコントローラ/法に基づく関数でありますstackoverflowの投稿と私には解決策が与えられていません...
私の現在の設定は以下の通りです...
アプリケーション/設定/ form_validation.php
//I know the file is being loaded as these work
$config['error_prefix'] = '<span class="text-danger">';
$config['error_suffix'] = '</span>';
/**
* METHOD SPECIFIC VALIDATIONS
*/
/* Controller: Account
* Method: Register
*/
$config = array(
'account/register' => array(
'field' => 'company',
'label' => 'Company',
'rules' => 'required|is_unique[company.companyName]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'alpha_numeric|trim|required|is_unique[users.username]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
),
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'required'
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|min_length[6]',
array(
'min_length' => '{field} must have at least {param} characters.'
)
),
array(
'field' => 'passconf',
'label' => 'Confirm Password',
'rules' => 'required|matches[password]'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[users.email]',
array(
'required' => 'You have not provided {field}.',
'is_unique' => 'This {field} already exists.'
)
)
);
コントローラー:
class Account extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('account_model');
}
public function register()
{
//Form not yet submitted, user not logged in, display login page
if ($this->form_validation->run() == FALSE and $this->session->userdata('loginuser') == FALSE) {
$this->load->view('templates/header');
$this->load->view('account/register');
$this->load->view('templates/loadjs');
} else {
}
ビュースニペット:
で行く<input class="form-control" name="company" placeholder="Company Name" type="text" value="<?php echo set_value('company'); ?>" autofocus />
</div>
<div><?php echo form_error('company'); ?></div>
私がリンクしているドキュメントでは、$this->form_validation->run()
とiこれらのルールは自動的に呼び出されますか?
わかりやすくするために、コントローラとメソッド名を記述できますか?それは実際にaccountという名前のコントローラのregisterという名前のメソッドですか?検証を呼び出すコードと、おそらくフォームのhtmlスニペットを表示できますか?私が尋ねる理由は、あなたの記述の表面上では、それが正しくやっているように聞こえるからです。しかし、それが機能していないので、何かが間違っているはずです。 – colonelclick
私はこれを行っていますが、通常のインライン '$ this-> form_validation-> set_rules( 'company'、 'Company'、 'required | is_unique [company.companyName]ありがとう –
あなたが指定した例では、直接呼び出すときに、form_validation.phpにあるエラーメッセージ配列を使用していないことがわかります。定義済みのエラーメッセージ配列なしでform_validation.phpファイルを試しましたか?または逆にset_messageを使用して成功していますが、あなたの例にはありませんか? – colonelclick