Take the 2-minute tour ×

I have four fields. Lets call them a, b, c and d. I need to validate them.

Error is when:

  • One til three fields are not empty;

Error is not when:

  • All fields are not empty,
  • All fields are empty;

Any neat solution here? Thanks in advice.


Only relationships are that all four variables are prefixed with event_. It gives me event_name, event_description etc..

Edit #2:

At the moment I have something like...

if (
    && !empty($values['event_time'])
    && !empty($values['event_name'])
    && !empty($values['event_description'])
) {

It checks that all fields are filled up and then, if that's true, adds event.

As I said before, I need to display user-friendly error when some field isn't filled up (for example, user had forgot to enter description). Anyway, when all fields are filled up (it means - all okay) or when no fields are filled up (it means - user ignores event adding and don't want to add one) - no error should be displayed.

I could write code with 16 'if' statements, but isn't there any better way? :)

share|improve this question
Sorry, we're going to need to see more code to be able to help.. What is the error message at least? –  mdskinner Jul 27 '11 at 11:52
@mdskinner Ha, ha. I think that you didn't understood my problem. I need to show user-friendly error when... upper are conditions. I could right like 16 'if' statements, but I'm looking for some neat way using Validate class. –  daGrevis Jul 27 '11 at 12:35

2 Answers 2

up vote 1 down vote accepted

This isn't beautiful, but as long as you have something unique about the fields you want to check (such as "event_..."), you could loop through the variable array ($values, $_POST, etc) and check only the fields that matter. Then, you can easily check for an all or none situation.

Here is a quick example:

$total = 0;
$filled = 0;
foreach($values as $field => $val) {
    if(strpos($field,'event_') === 0) {
        if( ! empty($val)) {
if($filled == 0 OR $total == $filled) {
} else {
share|improve this answer

Is there a relationship between one of the entered values and the none entered values?? could you just parse it as an empty value?

if ( ! isset($post->a) )  $post->a = '';
share|improve this answer
What did you mean with relationship? –  daGrevis Jul 26 '11 at 7:00
Well, do they depend on each other somehow (if $a != '' $e == $b)? Can you post some of your actual form markup, and the error message that your getting. Along with the controller logic and any validation definitions in the relevant model. (just relevant snippets) –  mdskinner Jul 26 '11 at 23:25
See my post's edit. –  daGrevis Jul 27 '11 at 5:54
Edited it again... can't explain better. –  daGrevis Jul 27 '11 at 15:27

Your Answer


