We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50497
    • 30 Posts
    So I'd like to check for an entry by a user in a table before allowing a second entry. If the user already has an entry I do not want them to have the ability to submit a second entry.

    I tried creating a custom validator which I think is probably the appropriate way of doing this, but didn't have much luck. I was able to create a hook that seems to prevent the form from submitting, but I can't figure out how to output an error message that I can capture on the page.

    Here is my snippet:
    <?php
    $path = MODX_CORE_PATH . 'components/memberdata/';
    $result = $modx->addPackage('memberdata',$path.'model/','mp_');
    $output = '';
    $userid = $modx->user->get('id');
    $company = $modx->getObject('companyProfiles',array('FK_member_id' => $userid));
    if ($company) {
    return false;
    }
    else
    {
    return true;
    }


    And my Formit call if needed:
    [[!FormIt? 
    &hooks=`spam,checkDupCompanyUser,formit2db` 
    &preHooks=`db2formit`
    &prefix=`mp_`
    &packagename=`memberdata`
    &tablename=`company_profiles`
    &classname=`companyProfiles`
    &validate=`
        nospam:blank,
        companyName:required,
        FK_companytype_id:required:minLength=`1`,
        FK_state_id:required:minLength=`1`,
    `
    &companyName.vTextRequired=`Please enter a Company Name.`
    &FK_companytype_id.vTextMinLength=`Please select a Company Type.`
    &FK_companytype_id.vTextRequired=`Company Type is a required field.`
    &FK_state_id.vTextMinLength=`Please select a State.`
    &FK_state_id.vTextRequired=`State is a required field.`
    &vTextRequired=`Please enter a value for this field.`
    ]]


    Any help as always is greatly appreciated.
      • 13428 ☆ A M B ☆
      • 1,031 Posts
      With the following code you could avoid to update any existing entry:
      …
      if ($company) {
          $hook->addError('company', 'Whatevermessage');
      }
      return hook->hasErrors();
      

      The errormessage is displayed in the field [[!+fi.company.error]] and in [[!+errors]] a bulk list of all errors.
        • 50497
        • 30 Posts
        Jako,
        Thank you for your reply. Unfortunately, I'm getting an error.

        $hook->addError('company', 'Error message');


        Throws the following error on the page.

        Fatal error: Call to a member function addError() on a non-object
          • 13428 ☆ A M B ☆
          • 1,031 Posts
          Did you use my code above in your snippet code?
            • 50497
            • 30 Posts
            I used the following:

            $path = MODX_CORE_PATH . 'components/memberdata/';
            $result = $modx->addPackage('memberdata',$path.'model/','mp_');
            $output = '';
            $userid = $modx->user->get('id');
            $company = $modx->newQuery('companyProfiles',array(
               'FK_member_id' => $userid,
            ));
            if ($company) {
                $hook->addError('company', 'Whatevermessage');
                return $hook->hasErrors();
            }
            else
            {
            return true;
            }


            I believe on your code above you left the $ off of $hook->hasErrors(); so I added it, but I get the following:

            Fatal error: Call to a member function addError() on a non-object in ..... on line 10
              • 4172
              • 5,888 Posts
              what happens, when you try it on a clean resource with a clean template for debugging.
              Very simple form and very simple formit-call, only one simple hook with:

                  
              $hook->addError('company', 'Whatevermessage');
              return $hook->hasErrors();
              




                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 50497
                • 30 Posts
                Let me make sure I understand your question Bruno. You want to see what happens with a hook that only contains the following code?
                    
                $hook->addError('company', 'Whatevermessage');
                return $hook->hasErrors();
                

                  • 4172
                  • 5,888 Posts
                  yes, because this error seems strange to me:

                  Fatal error: Call to a member function addError()

                  This would mean, that the $hook - var isn't a hook-object at this time.
                  We want to find the reason, why.

                  First step. Check, if it is the case also with a very simple setup.
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 50497
                    • 30 Posts
                    Ok, that took a minute. When I initially changed to your snippet from above it was still continuing to throw the same error. I realized it was a cache issue at that point. I cleared the cache and ran the snippet with no issue. I had to delete the previous snippet because it just kept caching and throwing an error.

                    Once I got the snippet above working I added my previous snippet:
                    <?php
                    $path = MODX_CORE_PATH . 'components/memberdata/';
                    $result = $modx->addPackage('memberdata',$path.'model/','mp_');
                    $output = '';
                    $userid = $modx->user->get('id');
                    $company = $modx->newQuery('companyProfiles',array(
                       'FK_member_id' => $userid,
                    ));
                    if ($company) {
                        $hook->addError('company', 'Whatevermessage');
                        return $hook->hasErrors();
                    }
                    else
                    {
                    return true;
                    }


                    Based on this code the following should provide the error message "Whatevermessage" when I add [[!+fi.company.error]]. However, the form won't submit due to the hook returning false, but I don't see the message when [[!+fi.company.error]] is added to the page.
                      • 50497
                      • 30 Posts
                      It actually seemed to be a cache issue. It's working now.