0

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

模型

   'exists'=>array(
                   'rule'=>'partytwo',
           'message'=>'That username doesnt exist.'
        )); 



 function userExists($field=array(), $compare_field=null )
    {
        if($field['exists']= $compare_field)
        return TRUE;
        else return FALSE;
    }

和控制器关系的验证

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

它目前直接出错

4

1 回答 1

0

你读过关于验证的书吗?

// 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.'); 
        } 

    } 

这是沿着这些路线的东西,试试吧。

于 2012-05-28T05:15:22.947 回答