【问题标题】:Form Validation Saved Callback表单验证保存回调
【发布时间】:2018-06-18 10:29:16
【问题描述】:

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

application/config/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 函数之前的“公共”访问说明符,我明白了,

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

否则我会得到,

语法错误,意外的“公共”(T_PUBLIC),预计文件结尾

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

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

【问题讨论】:

  • 你的回调方法应该在你的 UserController.php 中
  • @pradeep 我知道,它在文档中提到。但是我需要将该回调函数复制到我想要使用它的所有控制器文件中。这将导致冗余。有没有办法在我正在编写保存的验证的 form_validation.php 文件中定义。其他人也遇到了和我一样的错误 stackoverflow.com/questions/32835166/… 。但是对他有用的解决方案对我不起作用。
  • 您可以在控制器或 form_validation 中使用其他替代方法,例如 callable
  • @pradeep 谢谢它成功了
  • @pradeep Callable 解决了在 form_validation 文件中编写自定义验证的问题,正如我在下面的解决方案中所写的那样,但可重用性问题仍然存在,因为我需要在需要时再次编写相同的可调用函数在另一个验证规则组中。有什么解决办法吗?

标签: codeigniter


【解决方案1】:

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));        
   }    
}

application/config/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',
            )  
)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多