0

フィールドの検証を自分で行うという問題があります。これで、フォームに5〜6個のフィールドがあります。だから私は私のコントローラーでそれぞれをチェックしています、そして間違っているなら私はビューを再びロードしてエラー配列をそれに渡したいと思います。

私はこれで上記の機能を実現しました:

<html>
<head>
<title>My Form</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

</head>
<body>


<?php
    echo $fullname;
?>

<?
 echo form_open('/membership/register');    
?>


<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<h5>Mobile</h5>
<input type="text" name="mobile" value="" size="15" />

<h5>Home</h5>
<input type="text" name="home" value="" size="15" />

<h5>Full Name</h5>
<input type="text" name="fullname" value="" size="100" />
<br><br>
<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>

コントローラのコードは次のとおりです。

            if (preg_match('#[0-9]#',$fullname))
            { 
                $errors['fullname'] = 'wrong name format!';
                $this->load->view('register', $errors); 
            }

今私が抱えている本当の問題は、多くのフィールドが間違っているかどうかです。$ errors配列を渡して表示し、そこに含まれるすべての値にアクセスしたいと思います。したがって、値を取得するために$fullnameまたは$mobileを指定する必要はありません。これはどのように行うことができますか?一度に不足しているすべてのものをユーザーに表示するように

4

2 に答える 2

4

まず、codeigniter の組み込みフォーム検証クラスを使用することをお勧めします

コントローラーでの検証の通常の処理方法は次のとおりです。

if ($this->input->post()) 
{
    // process the POST data and store accordingly
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean');
    // the rest of your form fields validation can be set here
    if ($this->form_validation->run() == FALSE)
    {
        // validation hasnt run or has errors, here just call your view
        $this->load->view('same_view_file_with_the_form', $data);
    }
    else
    {
        // process the POST data upon correct validation
    }
}

私のビューファイルでは、次のように各エラーを呼び出します。

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />
<span class="error-red"><?php echo form_error("username"); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<span class="error-red"><?php echo form_error("password"); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="cpassword" value="" size="50" />
<span class="error-red"><?php echo form_error("cpassword"); ?></span>
于 2013-03-11T16:24:25.343 に答える
0

ビューにバインドする前に、コントローラーですべてのチェックをerrors行います。

例えば

$errors = array();

if (preg_match('#[0-9]#',$fullname))
{ 
    $errors['fullname'] = 'wrong name format!';
}

if ( do_something_to_validate(mobile) )
{
    $errors['mobile'] = 'invalid mobile';
}

// after checking everything do this
$this->load->view('register', $errors); 
于 2013-03-11T14:59:41.857 に答える