문제

I wanna validate multiple fields at one place. So in a form I have included 4 fields as follows

  1. facebook_link
  2. twitter_link
  3. google_plus_link
  4. linked_in_link

The user atleast type any one field of above. Please help me to get the solution like, the user types anyone of the links in the form.

도움이 되었습니까?

해결책

you may add your own Validation Methods.

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;
}

다른 팁

Hope this will work for you.

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;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top