Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top