Hi all I’m creating an invoice system and trying to make sure that the person sending the request, is sending it to a person who exists. The code that I currently have isn’t working and was wondering if someone could give me a hand.
model
'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;
}
and the validation in relationship the controller
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.'); }
}
here is my current model
<?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;
}
}
its currently going straight to errors
Have you read the book on validation?
And for controller:
It is something along those lines, try that.