Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-07T14:00:39.253Z
Answers.LastActivityDate: 2020-08-07T14:00:39.253Z
Answers.UserDisplayName: James Smith
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-07T22:03:30.753Z
Answers.Text: 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.
Answers.UserDisplayName: Garrett
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-07T22:04:43.323Z
Answers.Text: I'm still totally going to try using `Craft::$app->runAction('users/sogin')` to see what happens!
Answers.UserDisplayName: Garrett
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-08T19:50:51.557Z
Answers.Text: 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?
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-07T16:30:36.010Z
Answers.LastActivityDate: 2020-08-08T19:49:41.743Z
Answers.LastEditDate: 2020-08-08T19:49:41.743Z
Answers.UserDisplayName: RitterKnight
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-07T07:52:36.527Z
Comments.Text: "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.
Comments.UserDisplayName: James Smith
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-07T08:55:02.267Z
Comments.Text: 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!
Comments.UserDisplayName: dmatthams
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-08T01:12:03.897Z
Comments.Text: 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.
Comments.UserDisplayName: Garrett
Tags: <plugin-freeform><controller><modules><freeform>
Title: Can you call a controller method from within a plugin/module in Craft 3?
ThreadText: # 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](https://getbootstrap.com/docs/4.0/examples/sign-in/), [Foundation](https://get.foundation/building-blocks/blocks/form-login.html) or [Tailwind](https://tailwindcss.com/components/forms/) might be useful.
  
  Your [login form](https://craftcms.com/docs/3.x/dev/examples/login-form.html) is just 2 fields, user and password:
  
  ```
  <form method="post" accept-charset="UTF-8">
      {{ csrfInput() }}
      {{ actionInput('users/login') }}
  
      <h3><label for="loginName">Username or email</label></h3>
      <input id="loginName" type="text" name="loginName"
          value="{{ craft.app.user.rememberedUsername }}">
  
      <h3><label for="password">Password</label></h3>
      <input id="password" type="password" name="password">
  
      <label>
          <input type="checkbox" name="rememberMe" value="1">
          Remember me
      </label>
  
      <input type="submit" value="Login">
  
      {% if errorMessage is defined %}
          <p>{{ errorMessage }}</p>
      {% endif %}
  </form>
  
  <p><a href="{{ url('forgotpassword') }}">Forget your password?</a></p>
  
  ```
  
  [User profile](https://craftcms.com/docs/3.x/dev/examples/user-profile-form.html) 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](https://craftcms.com/docs/3.x/dropdown-fields.html) 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](https://craftcms.com/docs/3.x/dev/examples/entry-form.html) or [User Profile](https://craftcms.com/docs/3.x/dev/examples/user-profile-form.html) but these are just as straightforward to code and setup. And you can always reuse code on other projects including any [custom validators](https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors) 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?  
  
  ---
  
  