If you take a look at the formRule()
method, you will notice that it is static:
public static function formRule($params) {
...
When methods are invoked statically, you don't have access to $this. There is a similar construct to $this->
, which is self::
(see http://php.net/manual/en/language.oop5.static.php) for more info, but I don't think it will help in your case as assign is expecting a form object to work with.
If you want to add validation to a form, you probably want to use the addRules()
method. You can define a custom function (callback) that will accept the values submitted into the form and expects you to return an array of errors.
/**
* If your form requires special validation, add one or more callbacks here
*/
function addRules() {
$this->addFormRule(array('CRM_Example_Form', 'myRules'));
}
/**
* Here's our custom validation callback
*/
static function myRules($values) {
$errors = array();
if ($values['foo'] != 'abc') {
$errors['foo'] = ts('You entered the wrong text!');
}
return empty($errors) ? TRUE : $errors;
}
see https://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference for more details.
Another method I have used is
public function validate() {
$this->setElementError(); // <-- more details here...
return parent::validate();
}
See https://pear.php.net/manual/en/package.html.html-quickform.html-quickform.setelementerror.php for more info, though I think the above method is a little off piste.
Though having said that, the correct method really depends on why you are modifying the MembershipRenewal.php form.
If you are making a modification for your local site, you should really create an extension and use the https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_validateForm/ hook.
If you are looking to make a more generic improvement and submit it to core, then the approach described above is the way to go, but you'll want to discuss your planned modifications with others before doing too much work somewhere like https://chat.civicrm.org/civicrm/channels/dev.