1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
<?php
class FormConstraintFieldsInequality implements FormConstraint
{
private $js_message;
private $first_field;
private $second_field;
public function __construct(FormField $first_field, FormField $second_field, $js_message = '')
{
if (!empty($js_message))
{
$this->js_message = $js_message;
}
else
{
$this->js_message = LangLoader::get_message('form.fields_must_not_be_equal', 'status-messages-common');
}
$this->first_field = $first_field;
$this->second_field = $second_field;
$this->second_field->add_form_constraint($this);
}
public function validate()
{
$first_value = $this->first_field->get_value();
$second_value = $this->second_field->get_value();
if ($first_value !== null && $second_value !== null)
{
if ($first_value != $second_value)
{
return true;
}
}
return false;
}
public function get_js_validation()
{
return 'inequalityFormFieldValidator(' . TextHelper::to_js_string($this->first_field->get_id()) .
', ' . TextHelper::to_js_string($this->second_field->get_id()) . ', ' . TextHelper::to_js_string($this->get_validation_error_message()) . ')';
}
public function get_validation_error_message()
{
return StringVars::replace_vars($this->js_message,
array('field1' => $this->first_field->get_label(), 'field2' => $this->second_field->get_label()));
}
public function get_related_fields()
{
return array($this->first_field, $this->second_field);
}
}
?>