Where to put redirect if certain fields are not filled

11 views
Skip to first unread message

in...@yuzi.nl

unread,
May 22, 2021, 9:59:50 AMMay 22
to Joomla! General Development
I am making a component which has a profile page. Now when a user reaches the homepage, I want to make sure the fields are filled, so where do I put the redirect code to redirect to profile page?

Do I add that into the view.html.php file? I thought putting it into the controller.php file but I don't know what method I have to use? I am thinking about puttng it in the view.html.php file because there is the display() method. But is there a better way (I think it's best to put it into the controller of that page)

Mathew Lenning

unread,
May 23, 2021, 7:01:45 AMMay 23
to Joomla! General Development
If you want to make them fill in the details after they have created a user account then I would put the redirect in a plugin and use this method

/**
* We set the authentication cookie only after login is successfully finished.
* We set a new cookie either for a user with no cookies or one
* where the user used a cookie to authenticate.
*
* @param array $options Array holding options
*
* @return boolean True on success
*
* @since 3.2
*/
public function onUserAfterLogin($options)
{
      if(!JFactory::getApplication()->isClient('site'))
      {
           return true;
      }

 // Do all your checks here and redirect if they haven't completed the required fields.
}

If you need them to do this before you create their user account then, I would make the fields required and return an error when they try to submit it without completing it. 

Darren Forster

unread,
May 23, 2021, 8:36:25 AMMay 23
to joomla-de...@googlegroups.com
If you want to check if fields are filled in you might be better using jQuery and JavaScript for that.

For example you could have a button that looks like a submit button that call a JavaScript function on click like

<button type="button" onclick="submitForm(this) ;" class="btn btn-primary" data-form="formId">Submit</button>
<input type="hidden" id="submitGo" value="true" />

(Note - the submitGo has deliberately only got an id attribute and no name attribute as it's only used by the JavaScript checking process)

and then a JavaScript function like
<script>
function submitForm( item )
{
form =jQuery(item).data("form")
jQuery("#"+form+" input").each (
function()
{
  if ( jQuery(this).val () = "" )
  {
     jQuery("#submitGo").val("false") ;
     return false ;
  }
}
) ;
/*create similar functions for select/textarea if in the form*/

if ( jQuery("#submitGo").val() != "false" )
 jQuery ("#"+form).submit() ;
}

This way your not constantly submitting the data getting the server to check it and constantly reloading the page - the basic checks are being done at the client end (also important to remember when using jQuery in Joomla you have to use full jQuery not $) and if you do add JavaScript add it with joomlas addScript functions so it can put the script into the header properly.

Also if you have Bootstrap 3 loaded BS3 has its own "required" functions that just need certain classes adding to the form/inputs you want as required.  Search Bootstrap 3 form required for more info on that one.



On Sat, 22 May 2021, 14:59 in...@yuzi.nl, <in...@yuzi.nl> wrote:
I am making a component which has a profile page. Now when a user reaches the homepage, I want to make sure the fields are filled, so where do I put the redirect code to redirect to profile page?

Do I add that into the view.html.php file? I thought putting it into the controller.php file but I don't know what method I have to use? I am thinking about puttng it in the view.html.php file because there is the display() method. But is there a better way (I think it's best to put it into the controller of that page)

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/6731917d-7b08-4ae5-a59e-f6e43f2bc718n%40googlegroups.com.

Mathew Lenning

unread,
May 23, 2021, 8:53:43 AMMay 23
to Joomla! General Development
Joomla actually has built-in client side validation. https://docs.joomla.org/Form_validation
So it might be easier to use it rather than a custom function.

I agree CS validation is better for the user experience. But IMHO it should always be "in addition to"  rather than "a replacement for" server side validation. 

in...@yuzi.nl

unread,
May 24, 2021, 12:04:33 PMMay 24
to Joomla! General Development
Hm I think I worded the question wrong. I let Joomla handle the form validation. It's more or less that I have a custom component and when I reach a certain screen I want certain fields to be filled. I moved it all around and made some custom methods to achieve it

Op zondag 23 mei 2021 om 14:53:43 UTC+2 schreef Mathew Lenning:
Reply all
Reply to author
Forward
0 new messages