处理codeigniter中的单个表单验证错误

时间:2016-09-17 17:05:00

标签: php codeigniter codeigniter-3

如何检查codeigniter中单个元素的表单验证错误?

我需要设置' has-error'类到验证失败的元素。 这与打印验证错误无关。

这就是我目前所拥有的:

public partial class frmWorkshopSelector : Form
{

    public frmWorkshopSelector()
    {
        InitializeComponent();
      }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();     //When clicking the exit button, the program will close
    }

    private void btncalc_Click(object sender, EventArgs e)
    {
        int wsregistration = 0;
        int lcost = 0;
        const decimal DAYS = 3;


        //For the following if statements, depending on what workshop and location is selected,
        //their correstponding registration and lodging fees will be displayed

        {
            if (rbtHandlingStress.Checked == true)
            {
                wsregistration = 1000;
            }
            else if (rbtSupervisionSkills.Checked == true)
            {
                wsregistration = 1500;
            }
            else if (rbtTimeManagement.Checked == true)
            {
                wsregistration = 800;
            }

            else
            MessageBox.Show("Please Select a Workshop");
            lblTotalCost.Text = "";
            lblLodgingCost.Text = "";
            lblRegistrationCost.Text = "";
            return;
        }

        {
            if (rbtAustin.Checked == true)
            {
                lcost = 150;
            }
            else if (rbtChicago.Checked == true)
            {
                lcost = 225;
            }
            else if (rbtDallas.Checked == true)
            {
                lcost = 175;
            }
            else
            {
                MessageBox.Show("Please Select a Location");
                lblRegistrationCost.Text = " ";
                lblTotalCost.Text = " ";
                lblLodgingCost.Text = " ";
                return;
            }
        }

        lblRegistrationCost.Text = wsregistration.ToString("C");
        lblLodgingCost.Text = lcost.ToString("C");
        lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C");

}


    private void btnReset_Click(object sender, EventArgs e)
    { //uncheks all radio buttons as well as clears out the previous calculations
        lblRegistrationCost.Text = "";
        lblLodgingCost.Text = "";
        lblTotalCost.Text = "";
        rbtHandlingStress.Checked = false;
        rbtSupervisionSkills.Checked = false;
        rbtTimeManagement.Checked = false;
        rbtAustin.Checked = false;
        rbtChicago.Checked = false;
        rbtDallas.Checked = false;
    }
}

2 个答案:

答案 0 :(得分:3)

你可以尝试下面的

<div class="form-group <?php  echo form_error('name') ? 'has-error' : ''; ?>">
<label for="name">Name</label>
<div class="validation-error">
    <?php echo form_error ( 'name' ); ?>
</div>
<div class="input-group">
    <div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
    <input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="<?php echo set_value('name'); ?>">
</div>

答案 1 :(得分:1)

如果该字段没有错误,form_error方法将为空。所以你必须这样检查:

<div class="form-group <?php echo form_error('name') !== '' ? 'has-error' : '' ?>">
    <label for="name">Name</label>
    <div class="validation-error">
        <?php echo form_error ( 'name' ); ?>
    </div>
    <div class="input-group">
        <div class="input-group-addon"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></div>
        <input id="name" type="text" class="form-control validate" name="name" placeholder="Your full name" value="<?php echo set_value('name'); ?>">
    </div>
</div>