【问题标题】:Codeigniter 3/ form_multiselect / Cannot understand how to do validationCodeigniter 3/ form_multiselect / 无法理解如何进行验证
【发布时间】:2020-05-04 06:31:15
【问题描述】:

我认为在其他领域中有以下几点:

echo form_multiselect('person_tags[]', $options, set_value('person_tags[]'), $atribute_tag);

如预期的那样,上面生成了一个标签列表。正如预期的那样,可以从列表中做出多个选择。

当表单提交时,验证运行如下;

$this->form_validation->set_rules('person_tags[]', 'Tags', 'required');

如果存在其他验证错误,表单会重新显示验证错误列表,但不幸的是,它不会像预期的那样使用最初选择的项目重新填充 form_multiselect()。另外,我注意到如果我删除了 person_tags 的验证规则,当表单上出现其他错误时,person_tags multiselect() 会使用最初选择的项目重新填充列表。验证应该如何工作?它似乎删除或转换了帖子。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    不要用name[] 定义set_value(),而是尝试在没有它们的情况下提供它。我创建了一个演示,它对我有用。看看这是否也适合你。

    查看

    <?php 
    
    $attributes = array('method' => 'POST');
    
    echo form_open('home/form', $attributes);
    
    $options = array(
        'small'         => 'Small Shirt',
        'med'           => 'Medium Shirt',
        'large'         => 'Large Shirt',
        'xlarge'        => 'Extra Large Shirt',
    );
    
    $attribute_tag = array(
        'class'         => 'some-class',
    );
    
    // this is the one you should be paying attention to ↓↓
    echo form_multiselect('person_tags[]', $options, set_value('person_tags'), $attribute_tag);
    
    $data = array(
        'type'  => 'text',
        'name'  => 'email',
        'id'    => 'email',
    );
    
    echo form_input($data, set_value('email'));
    
    echo form_submit('mysubmit', 'Submit Post!');
    echo form_close(); 
    
    ?>
    

    控制器

    function form(){
    
        $this->load->library('form_validation');
    
        $this->form_validation->set_rules('person_tags[]', 'Tags', 'required');
        $this->form_validation->set_rules('email', 'text', 'required');
    
        if ($this->form_validation->run() == FALSE){    
    
            $this->load->view('vwHome');
    
        }else{
            echo 'No validation error'; die;  // save in database(Your logic)
        }
    }
    

    TL;DR
    将您当前的代码更改为-

    echo form_multiselect('person_tags[]', $options, set_value('person_tags'), $attribute_tag);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2016-08-31
      • 2018-01-21
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多