Validation: At least one of n fields has to be notEmpty

Validation: At least one of n fields has to be notEmpty

goetzigoetzi Posts: 3Questions: 2Answers: 0

Hi!

I have two fields, "email" and "phone". When saving an entry, at least one of both fields has to be filled in. How can I validate this?

->validator( 'Validate::notEmpty' )
doesn't help me here

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You'd need a custom validator. The second parameter contains the data for the whole row so you can check to see if a valid is supplied for at least one.

    Allan

  • goetzigoetzi Posts: 3Questions: 2Answers: 0

    Thank you!
    This custom validator did the trick:

    ->validator( function ( $val, $data, $opts ) {
        if($data['rex_csndb_person']['email'] == '' && $data['rex_csndb_person']['telefon'] == '') {
            return 'E-Mail-Adresse oder Telefonnummer muss ausgefüllt sein';
        } else {
            return true;
        }
    } )
    
This discussion has been closed.