简体   繁体   中英

Codeigniter Form Validation - Showing Errors Individually problems

My form is in table and I try to write error message of form validation next to input space. (I put it in the third column)

the problems is the form_error() function that make more line-height between row in table. How should I do? because if I use CSS, it will make the "before error message occurred's line-height" wrong. Do I need to write the if-else of before-after button submit situation?

<table>
        <tr>
            <td>Username : </td>
            <td><input type="text" name="username" value="<?php echo set_value('username'); ?>"/></td>
            <?php
            if($this->input->post("btn") == null)
            {
                echo "<td><span style='font-size: 10px;color:red'>ขั้นต่ำ 6 ตัวอักษร</span></td>";
            }
            else
            {
                echo "<td><span style='font-size:10px;color:red;'>" . form_error('username') ."</span></td>";
            }
            ?>
        </tr> 
        <tr>
            <td>Password : </td>
            <td><input type="password" name="pass" value=""/></td>
            <?php
            if($this->input->post("btn") == null)
            {
                echo "<td><span style='font-size: 10px;color:red'>ขั้นต่ำ 6 ตัวอักษร</span></td>";
            }
            else
            {
                echo "<td><span style='font-size:10px;color:red;'>" . form_error('pass') ."</span></td>";
            }
            ?>
        </tr>
        <tr>
            <td>ยืนยัน Password : </td>
            <td><input type="password" name="pass_confirm" value=""/></td>
            <td><span style="font-size:10px;color:red;"><?php echo form_error('pass_confirm');?></span></td>
        </tr>
        <tr>
            <td>Email : </td>
            <td><input type="text" name="email" value="<?php echo set_value('email'); ?>"/></td>
            <td><span style="font-size:10px;color:red;"><?php echo form_error('email');?></span></td>
        </tr>
        <tr>
        <td colspan="2"><span style="font-size: 10px;color:red;">กรุณากรอกเบอร์มือถือ เพื่อรับ Code ยืนยันการสมัคร (ตัวอย่างเบอร์มือถือ 08xxxxxxxx)</span></td>
        </tr>
        <tr>
            <td>เบอร์มือถือ : </td>
            <td><input type="text" name="phone" value="<?php echo set_value('phone'); ?>"/></td>
            <td><span style="font-size:10px;color:red;"><?php echo form_error('phone');?></span></td>
        </tr>
        <tr>
            <td>กรอกรหัสภาพ :</td>
            <td>    
                <?php echo $captcha['image']; ?>
                <br>
                <input type="text" autocomplete="off" name="userCaptcha" placeholder="Enter above text" value="<?php if(!empty($userCaptcha)){ echo $userCaptcha;} ?>" />
                <?php echo form_error('userCaptcha','<p style="font-size:10px;color:red">','</p>'); ?>
            </td>

        </tr>
    </table>

You can load error delimiters for all the errors in one place,in relevant controller's function, need not to define them separately,

you can control the error message height and styles there

$this->form_validation->set_error_delimiters('<span style="font-size: 10px;color:red">', '</span>');

and need not to use 'if-else' in 'form-error' so use

 <tr>
     <td>Username : </td>
     <td><input type="text" name="username" value="<?php echo set_value('username'); ?>"/>
        <?php
           // if there is validation error, it shows here
          echo form_error('username');
        ?>
   </td>
  </tr> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM