/** */ public function testRuleNotEqualTo() { $validation_rules = array('choice_one' => 'required', 'choice_two' => 'required notEqualTo=choice_one', 'choice_three' => 'required notEqualTo=choice_one|choice_two'); $validator = new SimpleValidator($validation_rules); $good_form_data = array('choice_one' => 'red', 'choice_two' => 'blue', 'choice_three' => 'green'); $errors = $validator->validate($good_form_data); $this->assertEquals(array(), $errors); $bad_form_data = array('choice_one' => 'red', 'choice_two' => 'red', 'choice_three' => 'green'); $expected_fail = array('choice_two' => $validator->errorForNotEqualTo); $errors = $validator->validate($bad_form_data); $this->assertEquals($expected_fail, $errors); $bad_form_data = array('choice_one' => 'red', 'choice_two' => 'blue', 'choice_three' => 'blue'); $expected_fail = array('choice_two' => $validator->errorForNotEqualTo); $errors = $validator->validate($bad_form_data); $this->assertEquals($expected_fail, $errors); }
/** * @expectedException SimpleValidatorException */ public function testInvalidRuleSetException() { // note that the rules could be an empty string $invalid_rules = "string"; $data = array('sample' => 'foo', 'another' => 'bar'); $validator = new SimpleValidator($invalid_rules); $validator->validate($data); }
public function testRuleRequiredIfToNotRequired() { $validation_rules = array('new_member' => 'required choices=yes,no', 'email' => 'email required_if=new_member->yes'); $data = array('new_member' => 'no', 'email' => '*****@*****.**'); $expected = array(); $validator = new SimpleValidator($validation_rules); $errors = $validator->validate($data); $this->assertEquals($expected, $errors); }
/** * @expectedException SimpleValidatorException */ public function testUnexpectedKeysException() { $validation_rules = array('title' => 'required phrase maxlength=255'); $form_data = array('title' => 'This is a title', 'description' => 'A description of the bank'); $expected = array(); $validator = new SimpleValidator($validation_rules); $validator->ignoreUnexpectedKeys = false; $errors = $validator->validate($form_data); }
public function testNotRequiredAndEqualTo() { // [NOTE] // Best practice on this to NOT set the type of the second field (email) // but instead simply use the equalTo $validation_rules = array('email' => 'email', 'email_confirm' => 'equalTo=email'); $validator = new SimpleValidator($validation_rules); $this->assertEquals(array('email_confirm' => $validator->errorForEqualTo), $validator->validate(array('email' => '*****@*****.**', 'email_confirm' => '')), "Problem when first is valid email, and second is empty on line " . __LINE__); $validator = new SimpleValidator($validation_rules); $this->assertEmpty($validator->validate(array('email' => '', 'email_confirm' => '')), "Problem when both are empty on line " . __LINE__); }
public function testDateGreaterThanEpoch() { $validation_rules = array('start_date' => 'date', 'end_date' => 'dateGreaterThan=start_date'); $validator = new SimpleValidator($validation_rules); $this->assertEquals(array(), $validator->validate(array('start_date' => '2/01/2011', 'end_date' => '6/01/11')), "Problem when date is in epoch and greater than on " . __LINE__); $validator = new SimpleValidator($validation_rules); $this->assertEquals(array('end_date' => $validator->errorForDateGreaterThan), $validator->validate(array('start_date' => '6/01/11', 'end_date' => '2/01/2011')), "Problem when date is in epoch and less than on " . __LINE__); $validator = new SimpleValidator($validation_rules); $this->assertEquals(array(), $validator->validate(array('start_date' => '2/01/11', 'end_date' => '6/01/2011')), "Problem when date is in epoch and greater than on " . __LINE__); $validator = new SimpleValidator($validation_rules); $this->assertEquals(array('end_date' => $validator->errorForDateGreaterThan), $validator->validate(array('start_date' => '6/01/2011', 'end_date' => '2/01/11')), "Problem when date is in epoch and less than on " . __LINE__); }