1

我想在一个地方验证多个字段。所以在一个表格中,我包含了 4 个字段,如下所示

  1. facebook_link
  2. 推特链接
  3. google_plus_link
  4. linked_in_link

用户至少键入以上任何一个字段。请帮助我获得解决方案,例如,用户在表单中键入任何链接。

4

2 回答 2

4

您可以添加自己的验证方法。

public $validate = array(
    'facebook_link' => array(
        'rule'    => array('validateLink'),
        'message' => '...'
    ),
    'twitter_link' => array(
        'rule'    => array('validateLink'),
        'message' => '...'
    ),
    'google_plus_link' => array(
        'rule'    => array('validateLink'),
        'message' => '...'
    ),
    'linked_in_link' => array(
        'rule'    => array('validateLink'),
        'message' => '...'
    ),
);

public function validateLink($link) {
    $allFieldsAreEmpty = (
        empty($this->data[$this->alias]['facebook_link']) &&
        empty($this->data[$this->alias]['twitter_link']) &&
        empty($this->data[$this->alias]['google_plus_link']) &&
        empty($this->data[$this->alias]['linked_in_link'])
    );

    return !$allFieldsAreEmpty;
}
于 2013-01-19T12:24:56.543 回答
0

希望这对你有用。

public $validate = array(
    'facebook_link' => array(
        'rule'    => array('customValidation','facebook_link'),
        'message' => 'Please enter facebook link.'
    ),
    'twitter_link' => array(
        'rule'    => array('customValidation','twitter_link'),
         'message' => 'Please enter twitter link.'
    ),
    'google_plus_link' => array(
        'rule'    => array('customValidation'),
         'message' => 'Please enter google plus link.'
    ),
    'linked_in_link' => array(
        'rule'    => array('customValidation'),
         'message' => 'Please enter linkedin link.'
    ),
);

function customValidation($data , $filed) {
    if(empty($data[$filed])) {
        return false;
    }
    return true;
}
于 2016-05-26T08:27:04.507 回答