user1393064
user1393064

Reputation: 411

Validation always coming back false

why is my model taking it consistently to return false everytime I try input two names into my database. here is my entire model. All I'm doing is checking to see that both to/biller, partytwo/partyone exist in the relationship table. when i check debug kit, it shows that the form is taking in the correct variables but when I check this->validationErrors - it comes back as nothing? please help.

class Invoice extends AppModel{ 
        var $name='Invoice'; 
        public $useTable = 'invoices';
        public $primaryKey = 'id';
        public $belongsTo = array(
        'Relationship' =>array(
            'className' => 'Relationship',
            'foreignKey' =>'relationship_id',
            )
        ); 
        var $validate = array(
            'to' => array(
                'relationshipExists' => array(
                    'rule' => array(
                        'relationshipExists',
                        ),
                    'message' => 'sorry you dont have a relationship with that user.'
                    ),
                ),
            );

        public function relationshipExists($check){ 
            $relationshipExists=$this->Relationship->find('count', array('conditions' => array('Relationship.partyone','Relationship.partytwo'=>$check)));
                if ($relationshipExists == true) {
                    return TRUE;
                    }
                else
                    return FALSE;
                    }}

-relationship model

<?php

class Relationship extends AppModel
{

    var $name = 'Relationship';
    public $useTable = 'relationships_users';
    public $hasMany = array(
        'Invoice' =>
            array(
                'className'              => 'Invoice',
                'joinTable'              => 'invoice',
                'foreignKey'             => 'invoice_id'));
    public $belongsTo = array(
        'User' =>array(
            'className' => 'User',
            'foreignKey' =>'partyone','partytwo',
            'associationForeignKey'  => 'username',
            )); 

    var $validate = array(
        'date' => array(
            'rule' => array(
                'datevalidation',
                'systemDate'
            ),
            'message' => 'Current Date and System Date is mismatched'
        ),
        'partytwo' => array(
            'userExists' => array(
                'rule' => array(
                    '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->User->find('count', array('conditions' => array('User.username'=>$check)));
        if ($userExists == 1) {
           return TRUE;
        }
        else
            return FALSE;
    }

}

Upvotes: 0

Views: 161

Answers (2)

Felipe Zavan
Felipe Zavan

Reputation: 1813

What version of CakePHP are you using?

Anyway, you don't need the "public $useTable = 'invoices';", since your table's name is the Model name pluralized. You also don't need "public $primaryKey = 'id';", since it's 'id' by default.

check this link

Upvotes: 0

jeremyharris
jeremyharris

Reputation: 7882

Your find is going to do interesting things...

$relationshipExists=$this->Relationship->find('count', array(
  'conditions' => array(
    'Relationship.partyone',
    'Relationship.partytwo' => $check
  )
));

This would probably produce something similar to

SELECT COUNT(*) FROM relationships as Relationship 
  WHERE Relationship.partyone = 0
  AND Relationship.partytwo = Array;

This is because you're passing nothing to Relationship.partyone and an array to partytwo. You'll need to change it to something like this:

$relationshipExists=$this->Relationship->find('count', array(
  'conditions' => array(
    'Relationship.partyone <>' => null,
    'Relationship.partytwo' => current($check) // get the value from the passed var
  )
));

Which would produce something like

SELECT COUNT(*) FROM relationships as Relationship 
  WHERE Relationship.partyone IS NOT NULL
  AND Relationship.partytwo = 'fieldvalue';

Debug $check to understand what value is being sent to your validation message, and debug the find results to see the results. Also, look at the SQL log to see the SQL it generated. This should give you a better understanding of what's happening.

Upvotes: 1

Related Questions