Vinayak Phal
Vinayak Phal

Reputation: 8919

CakePHP How to use Built in validation rule in Custom validation function?

I've a field which should accept Integer value and its sum with another field should be 100. In order to do this I've written custom method like this.

'share' => array(
    'share' => array(
        'rule' => array('share'),
        'message' => 'This field is required.',
        'last' => true, 
    ),

Here I want to use the built validation method to check weather this field is numeric.

function share() {
        if( $this->data['Model']['commission_type'] == "usage_based" ) {
            // if($this->data['SmeCommission']['share']) { // Want to check this is a valid integer How can I built in Numeric validation here
                // Next validation to sum is equal to 100 with another field in the data.
            // }
        } else { 
            return true;
        }
    }

Upvotes: 2

Views: 928

Answers (1)

ADmad
ADmad

Reputation: 8100

Use multiple rules for that field. As shown below first rule checks the value is numeric and then your custom rule which checks the sum.

'share' => array(
    'numeric' => array(
        'rule' => 'numeric'
    ),
    'share' => array(
        'rule' => array('share'),
   )
),

If you do want to directly use a validation rule you can do like:

Validation::numeric(array('key' => 'value'));

Upvotes: 2

Related Questions