用于同意条款的Codeigniter表单模型

时间:2014-05-09 08:05:11

标签: php codeigniter

您好我有同意条款的codeigniter表单,但除非我有模型,否则我的表单错误不会显示。

是否有可用于同意条款复选框的模型。不确定为同意条款的模型添加什么。

这是到目前为止所做的。

控制器部分

public function index() {
        $data = array();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('terms', 'Agree to License', 'required');
        if ($this->form_validation->run($this) == FALSE) {
            $data = array();
            $data['heading_step_1'] = $this->lang->line('heading_step_1');
            $data['heading_step_1_small'] = $this->lang->line('heading_step_1_small');
            $data['text_license'] = $this->lang->line('text_license');
            $data['text_installation'] = $this->lang->line('text_installation');
            $data['text_configuration'] = $this->lang->line('text_configuration');
            $data['text_finished'] = $this->lang->line('text_finished');
            $data['text_project'] = $this->lang->line('text_project');
            $data['text_documentation'] = $this->lang->line('text_documentation');
            $data['text_footer'] = $this->lang->line('text_footer');
            $data['text_support'] = $this->lang->line('text_support');
            $data['button_continue'] = $this->lang->line('button_continue');
            $data['text_terms'] = $this->lang->line('text_terms');

            $this->load->view('template/stage_1', $data);
        } else {
            redirect('install/stage_2');
        }

    }

查看部分

     <div class="panel panel-default">
<div class="panel-body">
<?php echo form_open('install/stage_1/index');?>
<?php echo validation_errors('<p class="error">', '</p>'); ?>
<div class="form-group">
<div class="terms"  scroll; height: 550px;"><?php echo $text_terms; ?></div>
</div>
<div class="form-group">
<?php
$data = array(
    'name'  => 'terms',
    'id'          => 'terms',
    'value'       => '1',
    'checked'     => FALSE,
    'style' => 'margin-right: 10px;'
    );

echo form_checkbox($data);
$data = array(
    'value'       => $button_continue,
    'role'       => 'button',
    'type' => 'submit',
    'class' => 'btn btn-primary'
    );
echo form_submit($data);
?>
</div>

<div class="form-group">
</div>
<?php echo form_close();?>
</div>
</div> 

3 个答案:

答案 0 :(得分:0)

$this->load->library('form_validation');
$this->form_validation->set_rules('accept', 'Agree to License', 'trim|required');
$this->form_validation->set_message('required', 'You must agree to the license before you can install CMS Canvas!');

if ($this->form_validation->run())
{
     redirect('install/stage_2'); 
}
else
{
    $this->load->view('template/stage_1', $data);
} 

答案 1 :(得分:0)

扩展表单验证库

<强>应用/库

MY_Form_validation extends CI_Form_validation
{
    public function checkboxIsOn($value)
    {
        $this->form_validation->set_message('checkboxIsOn', '...message...');
        return ($value == 'on') ? true : false;
    }
}

$this->form_validation->set_rules('accept', 'Agree to License', 'trim|checkboxIsOn');

如果您要排除接受字段,请不要使用mass assignment方法,只有@unset()验证后才能使用

答案 2 :(得分:0)

解决了问题。我不得不删除表单中的“else”部分现在验证工作似乎codeigniter用户指南有时不正确。

public function agree() {
        $data = array();

      $this->form_validation->set_rules('accept', 'Agree to License', 'trim|required');
      $this->form_validation->set_message('required', 'You must agree to the license before you can install!');

      if ($this->form_validation->run()) {
            redirect('install/step2');
      }

      $data = array();
        $data['heading_step_1'] = $this->lang->line('heading_step_1');
        $data['heading_step_1_small'] = $this->lang->line('heading_step_1_small');
        $data['text_license'] = $this->lang->line('text_license');
        $data['text_installation'] = $this->lang->line('text_installation');
        $data['text_configuration'] = $this->lang->line('text_configuration');
        $data['text_finished'] = $this->lang->line('text_finished');
        $data['text_project'] = $this->lang->line('text_project');
        $data['text_documentation'] = $this->lang->line('text_documentation');
        $data['text_footer'] = $this->lang->line('text_footer');
        $data['text_support'] = $this->lang->line('text_support');
        $data['button_continue'] = $this->lang->line('button_continue');
        $data['text_terms'] = $this->lang->line('text_terms');

        $this->load->view('template/stage_1', $data);

        }