表单验证保存回调

时间:2018-06-18 10:29:16

标签: codeigniter

应用/控制器/ UserController.php

public function reg_form() 
{   
   $input_data_array = (array)json_decode(file_get_contents('php://input'));         
   $this->form_validation->set_rules('first_name','first_name','required|trim|alpha|callback_username_check'); 
   $this->form_validation->set_data($input_data_array);         
   if ($this->form_validation->run('signup') == FALSE)      
   {
    $result = array('status' => 404,'message' => $this->form_validation->error_array());
    $this->output->set_output(json_encode($result));        
   }
   else 
   {
    $result = array('status' => 200,'message' => 'Executed Succesfully','data'=>$input_data_array);
    $this->output->set_output(json_encode($result));        
   }    
}

应用/配置/ form_validation.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$config = array(
    'signup' => array(
            array(
                    'field' => 'first_name',
                    'label' => 'first_name',
                    'rules' => 'callback_username_check'                    
            )    
));

public function username_check($str)
{
        if ($str == 'admin')
        {
                $this->form_validation->set_message('username_check', 'The {field} field can not be the word "admin"');
                return FALSE;
        }
        else
        {
                return TRUE;
        }
}

如果删除&#39; public&#39;在username_check函数之前写入的访问说明符,我得到,

  

{&#34; status&#34;:404,&#34; message&#34;:{&#34; first_name&#34;:&#34;无法访问错误   与您的字段名称对应的消息   。FIRST_NAME(username_check)&#34;}}

否则我得到了,

  

语法错误,意外的&#39;公共&#39; (T_PUBLIC),期待文件结束

我已经提到了Callback function for form validation in config file上给出的解决方案,但它没有成功。

简而言之,当我在form_validation.php中放置回调函数定义(用于重用)而不是在Controller文件中时,我遇到了错误。 请帮忙。

1 个答案:

答案 0 :(得分:0)

Callable为我工作了 所以现在我的Controller函数和form_validation.php文件如下:

应用/控制器/ UserController.php

nchar

应用/配置/ form_validation.php

sqlSave
相关问题