3

I tried to get a component form from my module. I got the model and then tried to get the form, but the result was empty. I tried to define 'JPATH_COMPONENT' and it was useless.

define('JPATH_COMPONENT', JPATH_SITE . '/components/com_business');

jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_business/models');
$model = JModelLegacy::getInstance('Organization', 'BusinessModel'); //It is correct

$form = $model ->get('Form'); //empty
print_r($form);
4
  • Don't use JPATH_COMPONENT and JPATH_SITE. You only need to use JPATH_SITE
    – Lodder
    May 12, 2014 at 8:31
  • The same question was asked before. I thought if I define the constatn in helps
    – aspirisen
    May 12, 2014 at 9:03
  • Yes, you define the constant, but you need to use JPATH_SITE. JPATH_COMPONENT cannot be used outside the component, so seeing as you're using a module, it won't work ;)
    – Lodder
    May 12, 2014 at 9:07
  • JPATH_COMPONENT points to the currently component path. Now if you're in a module that may be used on other component pages - so not only will it not really help - but by defining the constant there you might screw up components who are relying that constant to be the component currently being viewed May 12, 2014 at 23:22

1 Answer 1

4

Try to add an additional path where to look for forms before your call to $model->get('Form').

JForm::addFormPath(YOUR_COMPONENT_FORMS_PATH);

You will need it because getForm calls loadForm (declared on the ancestor class JModelForm) which, before getting the form instance, uses this

JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');

Alternatively you may use JForm::getInstance to get an instance of your form.

Regards,

5
  • Thanks for replying, but I have some issues JForm::addFormPath('/components/com_business/models/forms'); JForm::addFieldPath('/administrator/components/com_business/models/fields'); JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_business/models'); $model = JModelLegacy::getInstance('Organization', 'BusinessModel'); $form = $model ->getForm(); print_r($form); //empty
    – aspirisen
    May 12, 2014 at 15:20
  • JForm::addFormPath('/components/com_business/models/forms'); JForm::addFieldPath('/administrator/components/com_business/models/fields'); $form = JForm::getInstance('Organization'); //throws error "0 JForm::getInstance(name, *string*)"
    – aspirisen
    May 12, 2014 at 15:22
  • 1
    This method works, but I think it's not good way $form = new JForm('organization'); $form ->loadFile(JPATH_SITE . '/components/com_business/models/forms/organization.xml'); print_r($form); //correct
    – aspirisen
    May 12, 2014 at 15:23
  • I forgot to set JPATH_SITE in JForm::addFormPath, now it is works, Sorry :)
    – aspirisen
    May 12, 2014 at 15:30
  • Nice to help you :)
    – Pep Lainez
    May 12, 2014 at 17:54

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.