Home Articles

表单验证保存回调

Asked
Viewed 265 times
0

应用/控制器/ 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;
        }
}

如果删除了在username_check函数之前写入的'public'访问说明符,我得到了,

{“status”:404,“message”:{“first_name”:“无法访问与您的字段名first_name相对应的错误消息 . (username_check)”}}

我得到了,

语法错误,意外的'public'(T_PUBLIC),期待文件结束

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

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

1 Answer

  • 0

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

    应用/控制器/ UserController.php

    public function reg_form() 
    {   
       $input_data_array = (array)json_decode(file_get_contents('php://input'));   
    
       $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' => array(
                            'required',
                             array(
                                    'username_callable',
                                    function($str)
                                    {
                                        if($str == 'admin')
                                        {
                                            return TRUE;
                                        }
                                        else{
                                            return FALSE;
                                        }
                                    }
                            )
                    ),
                    'errors' => array(
                        'username_callable' => 'Invalid Name',
                )  
    )));
    

Related