【发布时间】:2018-11-07 18:43:57
【问题描述】:
我有一个用我自己的表单构建器构建的表单,我的表单构建器类似于 WordPress 的 Gravity 表单。除验证外,一切正常。每次我提交表单时,它都会返回 true。
特别是,我正在验证select 字段,因为用户可以从网络检查器编辑值并提交值,这就是为什么我要根据保存动态表单的数据库中的已保存值检查该值。
这是我的尝试
where $post is $_POST[] array, $postKey is fieldName, & $postValue is submitted value by user.
public function validate()
{
$post = $this->ci->input->post();
//check form tempering
if(empty($post))
return false;
$formId = $post['form_id'];
//get form from db
$data=[];
$this->ci->db->select('formData');
$this->ci->db->where('formId', $formId);
$query = $this->ci->db->get(TBL_FORM_DATA);
if($query->num_rows() > 0)
{
$data = $query->row();
}
if(empty($data) )
return false;
//unset submit button and 0 if occurs
unset($post['submit']);
unset($post['0']);
//decode json
$data = json_decode($data->formData,true);
$fields = $data['field'];
//debug($fields);
//iterate post values and keys
foreach ($post as $postKey => $postValue)
{
//echo '<br>'.$postKey;
if(strpos($postKey, '@'))
{
list($fieldName,$field_id) = explode('@', $postKey);
$field_display_name = ucwords(str_replace('_', ' ', $fieldName));
if(strpos($field_id,'|')) {
list($field_id,$price) = explode('|', $field_id);
}
$field = $fields[$field_id];
//debug($field['choice'],false);
if(isset($field['choice']) && (isset($field['choice']['label']) && isset($field['choice']['value'])) )
{
/*if(!in_array($postValue, $field['choice']['value']))
{*/
//$list = implode(',', $field['choice']['value']);
$this->ci->form_validation->set_rules($postKey,$field_display_name,'callback_check_field['.$list.']');
/*$this->ci->form_validation->set_rules($postKey, $field_display_name,"in_list[".$list."]",
array('in_list' => 'Invalid Selection. Please Select from available choices.')
);*/
/*}*/
}
if(isset($field['required']) && $field['required']=='on'){
$this->ci->form_validation->set_rules($postKey, $field_display_name,'required|trim');
}
//$duplicate = isset($field['no_duplicate']) ? true : false;
//$this->ci->form_validation->set_rules('form_id',' ','required');
//$this->ci->form_validation->set_rules('inkform_total',' ','required');
}
$this->ci->form_validation->set_error_delimiters('<div class="clearfix"></div><p class="alert alert-danger">','</p>');
return $this->ci->form_validation->run();
}
//echo validation_errors();
}
检查选择字段值的回调函数
function check_field($field,$list)
{
if(!is_array($list)) {
$list = explode(',', $list);
}
if(!in_array($field, $list)){
$this->ci->form_validation->set_message('check_field','Invalid Post');
return false;
}else{
return true;
}
}
稍后我只想访问validate() 函数进行验证。
【问题讨论】:
-
究竟是什么“不工作”......一切?还是只是您的选择验证?
-
@Alex,只有验证不起作用,对于所有字段,不仅仅是选择。它在每次提交时返回 True
-
mkay,并且可以肯定的是,您故意尝试使其失败正确,例如没有输入有效数据?此外,我没有看到您的代码有任何明显的错误,但使用提供的信息进行故障排除并不容易。请阅读:stackoverflow.com/help/mcve。本质上生成一个示例,我们可以轻松地在我们的系统上运行以进行故障排除(完整/可验证)。删除不必要的位,使其最小。
-
我只是分享完整的代码。请再检查一次。
-
@Alex,你的回答有帮助,但现在我得到了
Unable to access an error message corresponding to your field name What Do You Want ?.(check_field),还没有完全解决。返回bu回调函数。
标签: php codeigniter validation codeigniter-3