1

i need help how to develop validation rule for next case...

my cakephp web form contains several dynamically generated checkboxes. end user must select at least one of them.

none of those checkboxes are fields in db table.

can you help me how to create validation rule for that?

tried to do by adding in model something like

var $validate = array(
    'topic' => array(
      'rule' => 'myRule',
      'message' => 'Check on at least one checkbox'
    )
)

function myRule() {
  //just give me false to know it works...
  return false;
}

also, for that case, how to place error message, below those checkboxes?

tnx in adv!

user198003
  • 11,029
  • 28
  • 94
  • 152

1 Answers1

0

With Custom Validation rules in CakePHP, if you are calling a function instead of a RegEx it must be the first parameter in an associative array, so it would look something like this instead.

var $validate = array(
    'topic' => array(
      'rule' => array('myRule'),
      'message' => 'Check on at least one checkbox'
    )
)

function myRule() {
  //just give me false to know it works...
  return false;
}

Additionally if you wanted to include function arguments you would put them as extra parameters in the rule array.

var $validate = array(
    'topic' => array(
      'rule' => array('myRule', 10),
      'message' => 'Check on at least one checkbox'
    )
)

function myRule($id) {
if (array_shift($id) = 10) {
    return false;
    }
}

http://book.cakephp.org/view/152/Adding-your-own-Validation-Methods

Stoosh
  • 2,408
  • 2
  • 18
  • 24
  • mark is correct, apologies. The first function argument for myRule would be the form value. – Stoosh Jan 31 '11 at 00:43
  • yes, i tried with that also before i post my question, so still have same problem... it works just like custom validation is not defined at all. – user198003 Jan 31 '11 at 11:58
  • the rule does not have to be in an array, 'rule' => 'myRule' is perfectly fine. the array is only used when there is other info to pass along to the method doing the validation. – dogmatic69 Jan 31 '11 at 13:02
  • tried both of them, and a lot of other options, but still the same... what i'm doing wrong? like i said, looks like custom validation is not defined at all. – user198003 Jan 31 '11 at 15:42