Thursday, November 13, 2014

Cakephp 2.4.4 extending ModelValidator?

Is there a way to extend the ModelValidator?

I know you can do things like this in the controllers:


   
public $helpers = array(
       
// Core Helpers
       
'Session', 'Time',
       
'Html' => array('className' => 'Utilities.HtmlExt'),
       
'Paginator' => array('className' => 'Utilities.PaginatorExt'),
       
'Form' => array('className' => 'Utilities.FormExt' ),
    );

Is there a way to do this with ModelValidator?
My goal is to catch the validation error messages, and the posted data which I can with the callback, afterValidate().
However, it doesn't give me the actual validation name (e.g. 'notEmpty', 'alphaNumeric'), or the particular settings for this validation rule.

If I can't extend/overload the class instance, can is there another way to grab the above information without having to look for the message in the settings?
I can't rely on the message , because it can change based on how the message failed. Example:


class EquipmentDetail extends AppModel
{

   
public $validate = array(
       
'mac_address' => array(
           
'alphaNumeric'   => array(
               
'rule'       => 'alphaNumeric',
               
'required'   => true,
               
'allowEmpty' => false,
               
'message'    => 'This field is required, and only allows numbers and letters. (you may have a space at the end of the string).',
           
),
           
'Rule_MacAddress' => array(
               
'rule' => 'Rule_MacAddress',
               
'required'   => true,
               
'allowEmpty' => false,
               
//'on'=> 'create',
//                'message'    => 'This must be a unique value, there may already be a record with this MAC address.',
           
),
       
),
   
);

// ....

   
   
public function Rule_MacAddress($check)
   
{
       
if(!isset($check['mac_address']))
       
{
           
return __('The MAC Address isn\'t set.');
       
}
       
        $mac_address
= $check['mac_address'];
        $mac_address
= strtoupper(trim($mac_address));
       
       
if($mac_address == 'TBD') return true;
       
       
// check to make sure it's a mac address using the Extractor Behavior
        $type
= $this->EX_discoverType($check['mac_address']);
       
if($type != 'mac')
       
{
           
return __('The given MAC Address isn\'t detected as a valid MAC Address. (0-9, A-F, no : or -)');
       
}
       
       
// check to make sure it's unique
       
if(!$exists = $this->findByMacAddress($mac_address))
       
{
           
return true;
       
}
       
       
return __('This must be a unique value, there may already be a record with this MAC Address.');
   
}

}



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: