LoginSignup
0
0

More than 3 years have passed since last update.

[CodeIgniter]バリデーションのエラーメッセージの前後に付与する文字列を変更する方法

Posted at

はじめに

バリデーションのエラーメッセージにデフォルトで<p>タグが付与されます。

if ( $this->form_validation->run() )
{
    $data['error_message'] = $this>form_validation->error_string();
    $this->load->view('purchase_form', $data);
}

echo $data['error_message'];
/*
<p>名前 は必須項目です。</p>
<p>数量 は数値を入力してください。</p>
*/

今回、<p>タグを付与せず指定の文字列に変更する方法をまとめました。

エラーメッセージの変更

set_error_delimiters()メソッドを使用します。
第一引数、第二引数でエラーメッセージの前後に付与する文字列を指定します。

$this->form_validation->set_error_delimiters('<div class="err">', '</div>');
$error_message = $this>form_validation->error_string();
echo $error_message;
/*
<div class="err">名前 は必須項目です。</div>
<div class="err">数量 は数値を入力してください。</div>
*/

参考

CodeIgniter Guide

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0