3

I want to create page with form via my custom extension. I find this page in documentation: https://docs.civicrm.org/dev/en/latest/framework/quickform/ But when I create a class according to the documentation, I get this error:

Error: Call to a member function setName() on string in CRM_Core_Form->__construct() (line 271 of /var/www/mvp1/web/sites/all/modules/civicrm/CRM/Core/Form.php).

My class:

class CRM_JosiahVenture_Page_RolePrimaryAlert extends CRM_Core_Form {

  public function preProcess() {
  }

  public function buildQuickForm() {
    $this->add('text', 'my_text_field', ts('Enter Some Text'));
  }

  public function addRules() {
    $this->addFormRule(array('CRM_Example_Form', 'myRules'));
  }

  public static function myRules($values) {
    $errors = array();
    if ($values['foo'] != 'abc') {
      $errors['foo'] = ts('You entered the wrong text!');
    }
    return empty($errors) ? TRUE : $errors;
  }

  public function postProcess() {
  }
}

My xml file:

<item>
    <path>civicrm/RolePrimary</path>
    <page_callback>CRM_JosiahVenture_Page_RolePrimaryAlert</page_callback>
    <title>CustomActivity</title>
    <access_arguments>access CiviCRM</access_arguments>
  </item>

Tpl file is empty for now.

2
  • Are you using url civicrm/RolePrimary?reset=1&action=add ? Feb 11, 2022 at 22:33
  • @PradeepNayak yes.
    – vitius
    Feb 12, 2022 at 18:33

2 Answers 2

2

Maybe it is not connected to your issue, but if you implement a form, it supposed to be inside a Form directory, and the class name should follow this (has to contain _Form_). In your case the class name contains the _Page_ string which is strange. I would like to suggest you to generate the basics of your form with the civix tool. After the file generation you can check that the dummy form works well or not. If it works well, you can start to modify the dummy code.

1
  • Thank you! I am stupid... I put it in wrong location... I had created a page and I transform it to the form. It is neccessery that the form is in form directory. And also civix will help with this.
    – vitius
    Feb 12, 2022 at 18:50
0

In preprocess/postprocess/buildquickform, you should call the parent, i.e. your class should be:

class CRM_JosiahVenture_Page_RolePrimaryAlert extends CRM_Core_Form {

  public function preProcess() {
    parent::preProcess(); // <-- ************ add this
  }

  public function buildQuickForm() {
    parent::buildQuickForm();  // <-- ************ and this
    $this->add('text', 'my_text_field', ts('Enter Some Text'));
  }

  public function addRules() {
    $this->addFormRule(array('CRM_Example_Form', 'myRules'));
  }

  public static function myRules($values) {
    $errors = array();
    if ($values['foo'] != 'abc') {
      $errors['foo'] = ts('You entered the wrong text!');
    }
    return empty($errors) ? TRUE : $errors;
  }

  public function postProcess() {
    parent::postProcess();  // <-- ************ and this
  }
}
3
  • What do you mean parent? Do you mean "CRM_Core_Form" from which I extend? Can be more specific please? I want seperate page where preprocess can be empty now, in buildquickform i want 1 input field and in postprocess I want call api3. The most simple form from which I can understand how it works. I also try make copy of form, which extend "CRM_Core_Form" and I got the same error.
    – vitius
    Feb 11, 2022 at 20:35
  • I'll update my answer more fully.
    – Demerit
    Feb 11, 2022 at 20:40
  • Thanks, but this not help :/ Still I get same error..
    – vitius
    Feb 11, 2022 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.