Pergunta

I want to check two field combination validation for duplicate values. I have two fields name and area group.

$this->validate['Name'] = array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => __('err_required', array(__('lbl_Name', true))), 
        ),
       'Name' => array(

            'rule' => array('uniqueClick', 'GroupID'),
            'message' => __(__('lbl_Combination', true)), 
        )
    );  

    $this->validate['GroupID'] = array(
        'notempty' => array(
            'rule' => array('notEmpty'),
            'allowEmpty' => true,
            'message' => __('err_required', array(__('lbl_GroupID', true))), 
        )
    );

    public function uniqueClick ($ip)
        {   
            $count = $this->find('count', array(
              'conditions' => array(
                  'Name' => $ip,
                  'GroupID' => $this->data[$this->alias]['GroupID'])
           ));
           return $count == 0;
        }

By this code it check combination in both add and update case ,i want to check combination in both case but by this code it check in edit case always after add. so please give me appropriate solution. reply fast.

Foi útil?

Solução

Create a custom method use it in either in name or group and pass the value of name/group to custom function and and place the check in function:

$this->validate['Name'] = array(
    'notEmpty' => array(
        'rule' => array('notEmpty'),
        'message' => __('err_required', array(__('lbl_Name', true))), 
    )              
);  
 $this->validate['GroupID'] = array(
    'notempty' => array(
         'rule' => array('notEmpty'),
         'message' => __('err_required', array(__('lbl_GroupID', true))), 
     ),
    'duplicate' => array(
         'rule' => array('isDuplicate', $this->data['ModelName']['name']),
         'message' => __('err_required', array(__('lbl_GroupID', true))), 
     )
 );

public function isDuplicate($data, $name){
    // check here
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top