Sanganabasu
Sanganabasu

Reputation: 927

How do I validate multiple fields from a single location in CakePHP?

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.

Upvotes: 1

Views: 377

Answers (2)

Sunil kumar
Sunil kumar

Reputation: 771

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

Upvotes: 0

Pitsanu Swangpheaw
Pitsanu Swangpheaw

Reputation: 672

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

Upvotes: 4

Related Questions