Customer Portal

Get Involved. Join the Conversation.

Topic

    David Soto
    Simple CP Hook numeric Validation not workingAnswered
    Topic posted October 27, 2015 by David SotoBronze Medal: 1,250+ Points 
    183 Views, 5 Comments
    Title:
    Simple CP Hook numeric Validation not working
    Content:

    Hi guys,

    I am new using CP Hooks, I want to validate Customer "login" field to be numeric only on pre_contact_create. I have reviewed other posts & examples and did my own, but it is not working. Here is my code:

    Hooks.php:

    $rnHooks['pre_contact_create'] = array(
             'class' => 'Validacion_Contacto',
             'function' => 'ValidarContacto',
             'filepath' => ''
         );

     Validacion_Contacto.php:

    <?php
        
        namespace Custom\Models;
        use \RightNow\Connect\v1_2 as RNCPHP;
        
    class Validacion_Contacto extends \RightNow\Models\Base
    {
        function __construct()
        {
            parent::__construct();
           
        }
       
        public function ValidarContacto($contact){
           
           $login = $contact['data']->login->value;
            
            if(!is_numeric($login)) {
                echo "Error: El Nro de Documento debe contener sólo números";
            } 
           
        }
    }

     

    Probably it is a nomenclature error... Could you please help me what I am doing wrong?

    Thanks a lot
    David 

    Best Comment

    Jeremy Watson

    David,

    Rather than echo'ing the error message, I think you will get the functionality you want if you actually return that error message (return "Error: El Nro de Documento debe contener sólo números";).  That should cause the model to consider the request to be in a failure state.

    James

    Comment

     

    • Barrilito van Dijk

      Hi David,

      What is the precise use case here? Can you explain in detail what you are trying to do? The hook you made is a pre_contact_create, meaning if you have an /app/ask form on the customer portal which would create a contact if the email adres of the form does not exist it would call the pre_contact_create hook first. In your case, you talk about checking a loggin field?  Where does it fit in the picture?  Could you explain what you want to do? Are you working in the console or in the end user pages, and if so in which page. Please describe the full use case so that people undestand what it is you are trying to accomplish. Thanks David.

    • David Soto

      Hi BvD

      Thanks for the answer.

      The business case is: Our organization wants to "force" a new client that wants to create an account in the Customer Portal, to have their username (standard text field) only with numbers. Clients can't access the /app/ask page without their account created. They are redirected to app/utils/create_account   and in that page, we want to validate that the "username" field ("login" column in Contact Table) to be only numeric. If that specific field is not numeric, the CP should prompt an error message. 

      I read that the easiest approach to this CP validation is with hooks, but my code isn't working.

       

      If any more clarification is needed, I'd be happy to solve it.
      Thanks in advice
      David

       

    • Jeremy Watson

      David,

      Rather than echo'ing the error message, I think you will get the functionality you want if you actually return that error message (return "Error: El Nro de Documento debe contener sólo números";).  That should cause the model to consider the request to be in a failure state.

      James

    • David Soto

      Hi James,

      Thanks for your answer, I managed to solve this, there were two mistakes:

      1) Putting "echo" instead of return, as your advice

      2) The correct grammar in order to capture the login was:  $login = $contact["data"]->login;

      It is now working fine. 


      Thanks, best regards
      David

       

    • Barrilito van Dijk

      Hi David.

      Good to hear you got it working!