Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation cakePHP表单自定义验证_Validation_Cakephp - Fatal编程技术网

Validation cakePHP表单自定义验证

Validation cakePHP表单自定义验证,validation,cakephp,Validation,Cakephp,大家好,我正在创建一个发票系统,并试图确保发送请求的人正在将请求发送给一个存在的人。我目前拥有的代码不起作用,我想知道是否有人能帮我一把 模型 以及控制器中的验证关系 if($this->request->is('post')){ if($this->Relationship->validates(array('fieldlist'=>array('partywo','Relationship.userExists')))){ $this->R

大家好,我正在创建一个发票系统,并试图确保发送请求的人正在将请求发送给一个存在的人。我目前拥有的代码不起作用,我想知道是否有人能帮我一把

模型

以及控制器中的验证关系

if($this->request->is('post')){
    if($this->Relationship->validates(array('fieldlist'=>array('partywo','Relationship.userExists')))){
    $this->Relationship->create(); 
    if ($this->Relationship->save($this->request->data)) 
    {
        $id=$this->Relationship->id;
        $this->Session->setFlash('The relationship has been saved');  

    }}
    else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); } 
    } 

这是我目前的模型

  <?php
    class Relationship extends AppModel{ 
    var $name='Relationship'; 
    public $useTable = 'relationships_users';
    public $primaryKey = 'id';

    var $validate = array(
        'date' => array(
        'rule' => array('datevalidation', 'systemDate' ),
        'message' => 'Current Date and System Date is mismatched'),
        'partytwo'=>array(
                'partytwoExists'=>array(
                'rule'=> 'userExists',
                'message'=>'That username doesnt exist.'
             ))); 

 function datevalidation( $field=array(), $compare_field=null ) 
    {
        if ($field['date'] > $compare_field)

        return TRUE;
        else return FALSE;
    }

 function userExists($check)
    {

        $userExists= $this->find('count', array('conditions'=>$check));
        if($userExists == 1)
        {return TRUE;
        }
        else
        return FALSE;
    }
    }

你读过关于验证的书吗

// MODEL

var $validation = array(
    'field_name' => array(
        'rule' => array( 'customFunction', param ),
        'message' => 'Message here'
        )
    );

function customFunction($field=array(), $compare_field=null )
{
    if($field['exists'] == $compare_field)
    return TRUE;
    else return FALSE;
}
对于控制器:

// CONTROLLER


if($this->request->is('post')){

    $this->Relationship->set( $this->request->data );

    if($this->Relationship->validates(array('fieldlist'=>array('partywo','Relationship.userExists')))){
        $this->Relationship->create(); 
        if ($this->Relationship->save($this->request->data)) 
        {
            $id=$this->Relationship->id;
            $this->Session->setFlash('The relationship has been saved');  

        }

    } else { 
        $this->Session->setFlash('The relationship could not be saved. Please, try again.'); 
        } 

    } 

这是沿着这些思路的东西,试试看。

仍然停留在验证的第二部分,如果我把它贴上去,你能看一下吗?
// CONTROLLER


if($this->request->is('post')){

    $this->Relationship->set( $this->request->data );

    if($this->Relationship->validates(array('fieldlist'=>array('partywo','Relationship.userExists')))){
        $this->Relationship->create(); 
        if ($this->Relationship->save($this->request->data)) 
        {
            $id=$this->Relationship->id;
            $this->Session->setFlash('The relationship has been saved');  

        }

    } else { 
        $this->Session->setFlash('The relationship could not be saved. Please, try again.'); 
        } 

    }