<p>Based on your goal, I don't recommend taking this approach for the technical debt reasons in my comment, however your actual question is still a valid one. You can call any controller's method from a plugin/module like this:</p>
<pre><code>Craft::$app-&gt;runAction('users/save-user');
</code></pre>

CC BY-SA 4.0
2020-08-07T14:00:39.253Z
2020-08-07T14:00:39.253Z
James Smith
<blockquote>
<p>Is this a terrible idea? Is there a better way to accomplish this?</p>
</blockquote>
<p>One thing that makes a great designer/developer is knowing when to say no. It could be done but it's probably not worth it. If you need to ask the question, sometimes you just need to validate your own thoughts.</p>
<p>At the expense of not answering your other questions, I'd say Freeform is great for non-technical, end users who need to create custom forms that may need to change on a whim at some point.</p>
<p>However, I'm just curious as to why you're trying to use it to &quot;reinvent the wheel&quot; for the login/registration fields when Craft itself <em>already</em> has the proper validation and access control checking built in for those?</p>
<p>The great thing about Craft is the CSS is up to you, so I'm not really sure what you're really gaining by doing this?</p>
<p>If you're trying to get the styling to look right, something like <a href="https://getbootstrap.com/docs/4.0/examples/sign-in/" rel="nofollow noreferrer">Bootstrap</a>,  <a href="https://get.foundation/building-blocks/blocks/form-login.html" rel="nofollow noreferrer">Foundation</a> or <a href="https://tailwindcss.com/components/forms/" rel="nofollow noreferrer">Tailwind</a> might be useful.</p>
<p>Your <a href="https://craftcms.com/docs/3.x/dev/examples/login-form.html" rel="nofollow noreferrer">login form</a> is just 2 fields, user and password:</p>
<pre><code>&lt;form method=&quot;post&quot; accept-charset=&quot;UTF-8&quot;&gt;
    {{ csrfInput() }}
    {{ actionInput('users/login') }}

    &lt;h3&gt;&lt;label for=&quot;loginName&quot;&gt;Username or email&lt;/label&gt;&lt;/h3&gt;
    &lt;input id=&quot;loginName&quot; type=&quot;text&quot; name=&quot;loginName&quot;
        value=&quot;{{ craft.app.user.rememberedUsername }}&quot;&gt;

    &lt;h3&gt;&lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;/h3&gt;
    &lt;input id=&quot;password&quot; type=&quot;password&quot; name=&quot;password&quot;&gt;

    &lt;label&gt;
        &lt;input type=&quot;checkbox&quot; name=&quot;rememberMe&quot; value=&quot;1&quot;&gt;
        Remember me
    &lt;/label&gt;

    &lt;input type=&quot;submit&quot; value=&quot;Login&quot;&gt;

    {% if errorMessage is defined %}
        &lt;p&gt;{{ errorMessage }}&lt;/p&gt;
    {% endif %}
&lt;/form&gt;

&lt;p&gt;&lt;a href=&quot;{{ url('forgotpassword') }}&quot;&gt;Forget your password?&lt;/a&gt;&lt;/p&gt;
</code></pre>
<p><a href="https://craftcms.com/docs/3.x/dev/examples/user-profile-form.html" rel="nofollow noreferrer">User profile</a> is similar. It's just HTML with with some CMS specific stuff.
It may <em>look</em> complicated when you first start but it's really not when you start to break it down.</p>
<p>One other thought I had is you're trying to give CMS authors/editors a way to throw those forms anywhere on the site, similar to how Freeform (or WordPress/Gravity forms) might do it.</p>
<p>That's probably more of a different answer, but one way to pull that off would be with a custom field such as a <a href="https://craftcms.com/docs/3.x/dropdown-fields.html" rel="nofollow noreferrer">dropdown</a> and some Twig templates to see which form the user selected. Then embed the appropriate form using Twig <code>{% include %}</code> for example.</p>
<h1>TLDR</h1>
<p>I didn't even touch on <a href="https://craftcms.com/docs/3.x/dev/examples/entry-form.html" rel="nofollow noreferrer">Entry forms</a> or <a href="https://craftcms.com/docs/3.x/dev/examples/user-profile-form.html" rel="nofollow noreferrer">User Profile</a> but these are just as straightforward to code and setup. And you can always reuse code on other projects including any <a href="https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors" rel="nofollow noreferrer">custom validators</a> you setup.</p>
<p>I guess my point is if you're going to down the path of a custom module to hook into Freeform anyway, why not just cut out the middleman?</p>

CC BY-SA 4.0
2020-08-07T22:03:30.753Z
Thanks for the thoughtful answer. If you simply consider the login form, then yes Freeform seems overkill. But I have other forms I need to create on the frontend (for editing entries, user profile, registration, etc.) and I have custom user profile fields that I want to validate server side. And Freeform has the ability to build that validation easily in the backend and display it consistently on the frontend. From what I can tell, I'll have to build custom validation logic in order to have Craft do the validation on the server side. But, you're probably right: not worth contorting Freeform.
Garrett
CC BY-SA 4.0
2020-08-07T22:04:43.323Z
I'm still totally going to try using `Craft::$app->runAction('users/sogin')` to see what happens!
Garrett
CC BY-SA 4.0
2020-08-08T19:50:51.557Z
I've updated my answer with links to Craft's docs on Entries and User Profile. I guess my point is if you're going to down the path of a custom module to hook into Freeform anyway, why not just cut out the middleman?
RitterKnight
CC BY-SA 4.0
2020-08-07T16:30:36.010Z
2020-08-08T19:49:41.743Z
2020-08-08T19:49:41.743Z
RitterKnight
<p>I initially asked a question <a href="https://craftcms.stackexchange.com/questions/35917/is-it-possible-recommended-to-use-solspace-freeform-for-a-front-end-user-login-f/35918#35918">here</a> about how to use Solspace Freeform to build a custom front-end login form. I gave up too easily and accepted the &quot;it's possible but probably not a good idea&quot; answer. Full disclosure: I'm still trying to see if I can use Freeform as a replacement for all of the stock Craft front-end forms (login, user profile, entry, etc). There is so much available out of the box with Freeform in terms of form validation rules and styling, I really want to leverage that for all my forms.</p>
<ul>
<li>I have a Freeform login form with the appropriate fields</li>
<li>In my custom module, I'm hooking into Freeform's <a href="https://docs.solspace.com/craft/freeform/v3/developer/form-events.html#on-validating-the-form" rel="nofollow noreferrer">EVENT_FORM_VALIDATE</a></li>
<li>At this point the <code>Craft::$app-&gt;request</code> object has everything that the <code>craft\controllers\UsersController</code>'s <code>actionLogin()</code> method would need to attempt to log the user in</li>
<li>Is there some way to &quot;hand off&quot; the current request to the <code>UsersController</code>?</li>
<li>If the login is successful, will the <code>UsersController</code> go about its business and redirect the user properly?</li>
<li>If the login fails, I can add errors the Freeform's response as needed</li>
<li>Is this a terrible idea? Is there a better way to accomplish this?</li>
</ul>

CC BY-SA 4.0
2020-08-07T07:52:36.527Z
"Is this a terrible idea?" ... Maybe not _terrible_, but relying on a third-party plugin for such critical core functionality is something I'd definitely avoid. As you're finding out, it makes everything a bit more complicated than it needs to be, and you end up tying yourself in knots and coding workarounds on top of workarounds. Not to mention it stymies your ability to upgrade Craft and increases the burden of testing that everything still works.
James Smith
CC BY-SA 4.0
2020-08-07T08:55:02.267Z
Surely you could have ported the styles/validation over to the custom forms in the time it's taken you to fiddle with this  I admire your perseverance and genuinely interested to see where you get with this!
dmatthams
CC BY-SA 4.0
2020-08-08T01:12:03.897Z
I was able to take this a bit further, handing off the request to the `actionLogin()` method, and if unsuccessful, I could add errors to the response. However, I wasn't able to actually get a successful login; I'm clearly trying to hijack things and the `actionLogin()` method wasn't intended to be used this way. Oh well, it was a fun exercise. The route I'll end up taking is using the Craft/Yii validation framework to [extend validation](https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors) rules as I need. I appreciate all the input.
Garrett
<plugin-freeform><controller><modules><freeform>
Can you call a controller method from within a plugin/module in Craft 3?
# Can you call a controller method from within a plugin/module in Craft 3?

(Asked by: username_0 on 2020-08-07)

I initially asked a question [here](https://craftcms.stackexchange.com/questions/35917/is-it-possible-recommended-to-use-solspace-freeform-for-a-front-end-user-login-f/35918#35918) about how to use Solspace Freeform to build a custom front-end login form. I gave up too easily and accepted the "it's possible but probably not a good idea" answer. Full disclosure: I'm still trying to see if I can use Freeform as a replacement for all of the stock Craft front-end forms (login, user profile, entry, etc). There is so much available out of the box with Freeform in terms of form validation rules and styling, I really want to leverage that for all my forms.

* I have a Freeform login form with the appropriate fields
* In my custom module, I'm hooking into Freeform's [EVENT\_FORM\_VALIDATE](https://docs.solspace.com/craft/freeform/v3/developer/form-events.html#on-validating-the-form)
* At this point the `Craft::$app->request` object has everything that the `craft\controllers\UsersController`'s `actionLogin()` method would need to attempt to log the user in
* Is there some way to "hand off" the current request to the `UsersController`?
* If the login is successful, will the `UsersController` go about its business and redirect the user properly?
* If the login fails, I can add errors the Freeform's response as needed
* Is this a terrible idea? Is there a better way to accomplish this?

**Tags:** plugin-freeform, controller, modules, freeform
> **Comments:**
> **username_1**: "Is this a terrible idea?" ... Maybe not _terrible_, but relying on a third-party plugin for such critical core functionality is something I'd definitely avoid. As you're finding out, it makes everything a bit more complicated than it needs to be, and you end up tying yourself in knots and coding workarounds on top of workarounds. Not to mention it stymies your ability to upgrade Craft and increases the burden of testing that everything still works.  
> **username_2**: Surely you could have ported the styles/validation over to the custom forms in the time it's taken you to fiddle with this  I admire your perseverance and genuinely interested to see where you get with this!  
> **username_0**: I was able to take this a bit further, handing off the request to the `actionLogin()` method, and if unsuccessful, I could add errors to the response. However, I wasn't able to actually get a successful login; I'm clearly trying to hijack things and the `actionLogin()` method wasn't intended to be used this way. Oh well, it was a fun exercise. The route I'll end up taking is using the Craft/Yii validation framework to [extend validation](https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors) rules as I need. I appreciate all the input.  

---

## 2 answers

### Answer by username_1 on 2020-08-07. Score: 3 | Accepted answer

Based on your goal, I don't recommend taking this approach for the technical debt reasons in my comment, however your actual question is still a valid one. You can call any controller's method from a plugin/module like this:
Craft::$app->runAction('users/save-user');


---

### Answer by username_3 on 2020-08-07. Score: 2

Is this a terrible idea? Is there a better way to accomplish this?

One thing that makes a great designer/developer is knowing when to say no. It could be done but it's probably not worth it. If you need to ask the question, sometimes you just need to validate your own thoughts.
At the expense of not answering your other questions, I'd say Freeform is great for non-technical, end users who need to create custom forms that may need to change on a whim at some point.
However, I'm just curious as to why you're trying to use it to "reinvent the wheel" for the login/registration fields when Craft itself already has the proper validation and access control checking built in for those?
The great thing about Craft is the CSS is up to you, so I'm not really sure what you're really gaining by doing this?
If you're trying to get the styling to look right, something like Bootstrap,  Foundation or Tailwind might be useful.
Your login form is just 2 fields, user and password:

    {{ csrfInput() }}
    {{ actionInput('users/login') }}

    Username or email
    

    Password
    

    
        
        Remember me
    

    

    {% if errorMessage is defined %}
        {{ errorMessage }}
    {% endif %}


Forget your password?

User profile is similar. It's just HTML with with some CMS specific stuff.
It may look complicated when you first start but it's really not when you start to break it down.
One other thought I had is you're trying to give CMS authors/editors a way to throw those forms anywhere on the site, similar to how Freeform (or WordPress/Gravity forms) might do it.
That's probably more of a different answer, but one way to pull that off would be with a custom field such as a dropdown and some Twig templates to see which form the user selected. Then embed the appropriate form using Twig {% include %} for example.
TLDR
I didn't even touch on Entry forms or User Profile but these are just as straightforward to code and setup. And you can always reuse code on other projects including any custom validators you setup.
I guess my point is if you're going to down the path of a custom module to hook into Freeform anyway, why not just cut out the middleman?

> **Comments:**
> **username_0**: Thanks for the thoughtful answer. If you simply consider the login form, then yes Freeform seems overkill. But I have other forms I need to create on the frontend (for editing entries, user profile, registration, etc.) and I have custom user profile fields that I want to validate server side. And Freeform has the ability to build that validation easily in the backend and display it consistently on the frontend. From what I can tell, I'll have to build custom validation logic in order to have Craft do the validation on the server side. But, you're probably right: not worth contorting Freeform.  
> **username_0**: I'm still totally going to try using `Craft::$app->runAction('users/sogin')` to see what happens!  
> **username_3**: I've updated my answer with links to Craft's docs on Entries and User Profile. I guess my point is if you're going to down the path of a custom module to hook into Freeform anyway, why not just cut out the middleman?  

---

