Codeigniter-form-validation

提供:Dev Guides
移動先:案内検索

CodeIgniter-フォーム検証

検証は、Webアプリケーションを構築する際の重要なプロセスです。 取得するデータが適切であり、保存または処理するのに有効であることを保証します。 CodeIgniterはこのタスクを非常に簡単にしました。 簡単な例でこのプロセスを理解しましょう。

ビューファイル myform.php を作成し、以下のコードを application/views/myform.php に保存します。 このページには、ユーザーが名前を送信できるフォームが表示されます。このページを検証して、送信中に空にならないようにします。

<html>

   <head>
      <title>My Form</title>
   </head>

   <body>
      <form action = "" method = "">
         <?php echo validation_errors(); ?>
         <?php echo form_open('form'); ?>
         <h5>Name</h5>
         <input type = "text" name = "name" value = "" size = "50"/>
         <div><input type = "submit" value = "Submit"/></div>
      </form>
   </body>

</html>

ビューファイル formsuccess.php を作成し、 application/views/formsuccess.php に保存します。 フォームが正常に検証されると、このページが表示されます。

<html>

   <head>
      <title>My Form</title>
   </head>

   <body>
      <h3>Your form was successfully submitted!</h3>
      <p><?php echo anchor('form', 'Try it again!'); ?></p>
   </body>

</html>

コントローラーファイル Form.php を作成し、 application/controller/Form.php に保存します。 このフォームは、適切に検証されていないか、 formsuccess.php ページにリダイレクトされていない場合、エラーを表示します。

<?php

   class Form extends CI_Controller {

      public function index() {
        /*Load form helper*/
         $this->load->helper(array('form'));

        /*Load form validation library*/
         $this->load->library('form_validation');

        /*Set validation rule for name field in the form*/
         $this->form_validation->set_rules('name', 'Name', 'required');

         if ($this->form_validation->run() == FALSE) {
         $this->load->view('myform');
         }
         else {
            $this->load->view('formsuccess');
         }
      }
   }
?>
*application/config/routes.php* に次の行を追加します。
$route['validation'] = 'Form';

ブラウザで次のURLにアクセスして、この例を実行します。 このURLは、サイトによって異なる場合があります。

http://yoursite.com/index.php/validation

それは次の画面を生成します-

検証フォーム

コントローラーに検証を追加しました-フォームを送信する前に*名前*は必須フィールドです。 したがって、名前フィールドに何も入力せずに送信ボタンをクリックすると、以下の画面に示すように、送信する前に名前を入力するように求められます。

検証に失敗しました

名前を正常に入力すると、次のように画面にリダイレクトされます。

正常に検証されました

上記の例では、必要なルール設定を使用しています。 CodeIgniterには、以下で説明する多くのルールがあります。

検証ルールのリファレンス

以下は、使用可能なすべてのネイティブルールのリストです-

以下に、最も一般的に使用されるネイティブルールのリストを示します。

Rule Parameter Description Example
required No Returns FALSE if the form element is empty.
matches Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
regex_match Yes Returns FALSE if the form element does not match the regular expression. regex_match[/regex/]
differs Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. Note − This rule requires Query Builder to be enabled in order to work. is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter than the parameter value. min_length[3]
max_length Yes Returns FALSE if the form element is longer than the parameter value. max_length[12]
exact_length Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]
greater_than Yes Returns FALSE if the form element is less than or equal to the parameter value or not numeric. greater_than[8]
greater_than_equal_to Yes Returns FALSE if the form element is less than the parameter value, or not numeric. greater_than_equal_to[8]
less_than Yes Returns FALSE if the form element is greater than or equal to the parameter value or not numeric. less_than[8]
less_than_equal_to Yes Returns FALSE if the form element is greater than the parameter value, or not numeric. less_than_equal_to[8]
in_list Yes Returns FALSE if the form element is not within a predetermined list. in_list[red,blue,green]
alpha No Returns FALSE if the form element contains anything other than alphabetical characters.
alpha_numeric No Returns FALSE if the form element contains anything other than alphanumeric characters.
alpha_numeric_spaces No Returns FALSE if the form element contains anything other than alphanumeric characters or spaces. Should be used after trim to avoid spaces at the beginning or end
alpha_dash No Returns FALSE if the form element contains anything other than alphanumeric characters, underscores or dashes.
numeric No Returns FALSE if the form element contains anything other than numeric characters.
integer No Returns FALSE if the form element contains anything other than an integer.
decimal No Returns FALSE if the form element contains anything other than a decimal number.
is_natural No Returns FALSE if the form element contains anything other than a natural number − 0, 1, 2, 3, etc.
is_natural_no_zero No Returns FALSE if the form element contains anything other than a natural number, but not zero − 1, 2, 3, etc.
valid_url No Returns FALSE if the form element does not contain a valid URL.
valid_email No Returns FALSE if the form element does not contain a valid email address.
valid_emails No Returns FALSE if any value provided in a comma-separated list is not a valid email.
valid_ip No Returns FALSE if the supplied IP is not valid. Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.
valid_base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters.