Results 1 to 5 of 5

Thread: Custom validation based on role

  1. #1
    Snook is offline Sugar Community Member
    Join Date
    Jan 2007
    Posts
    52

    Exclamation Custom validation based on role

    Hi there,

    I'm working on Version 4.5.1f

    I need to validate a field depending on the user's role. If the user is part on one specific role, this field can contain only one value.

    For this, I have add an empty input field which contain the user's role (which is called "other"). (EditView.html)
    HTML Code:
    <input id="user_role" type="hidden" name="user_role" value="{USER_ROLE}">
    Which is filled by EditView.php:
    PHP Code:
    $user_role $focus->_getUserRole($current_user->id);
    $xtpl->assign("USER_ROLE"$user_role); 
    The _getUserRole() function is define in Account.php:
    PHP Code:
    function _getUserRole($user_id) {
            
    $query "SELECT r.name FROM acl_roles r, acl_roles_users ru WHERE r.id = ru.role_id AND ru.user_id='$user_id' ";
            
    $result $this->db->query($query,true," Error filling in additional detail fields: ");
            
    $row $this->db->fetchByAssoc($result);
            
    $user_role $row['name'];    
            return 
    $user_role;
        } 
    Then I have add a jQuery listener function on the Save button of the Account form, which check the value of the field I would like to check:
    PHP Code:
    function validateForm() {
        $(
    "input.saveAccount").click(function () {
            
    // Check the user role
            
    if ($("#user_role").val() == "other") {
                
    // Check the custom validation
                
    if ($("#city_c").val() != "Boston") {
                    
    alert("You cannot enter a city else then Boston.");
                }
            }
        });

    So now everything is working fine except that I don't know how to stop the script that save the account ?
    When the user click on the save button, an alert message appear, and when the user click on the OK button, I would like to keep the user on the form to allow him to change this specific field and not to save the new account entry.

    Thanks for your suggestions.
    :: Keep Open Spirit ::

  2. #2
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: Custom validation based on role

    Hi, Damien Snoeck

    Your code looks interesting, but it seems to be non upgrade safe.

    You can validate that field using the default sugar validation.

    Take a look at the function addToValidateComparison in the script include/javascript/sugar_3.js

    You can modify the EditView.php to add a validation to that field based on user role.

    In the modules/Users/User.php there is a function which returns true if the user is related to a role and false otherwise: check_role_membership.

    Let me know if you need additional information.

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  3. #3
    Snook is offline Sugar Community Member
    Join Date
    Jan 2007
    Posts
    52

    Default Re: Custom validation based on role

    Thanks andopes,

    Your suggestion looks better than my option.
    The first step to check the user's role is passed fine, but I have trouble using the function addToValidateComparison().
    I think I need to call the javascript.php function but don't know what kind of data are required in this function. I have try something like this:

    PHP Code:
    require_once('modules/Users/User.php');
    require_once(
    'include/javascript/javascript.php');
    $check_role = new User();
    $javascript2 = new javascript();
    if (
    $check_role->check_role_membership('Other'$user_id '')) {
        
    //addToValidateComparison($field, $type, $displayName, $required, $prefix='',$compareTo)
        
    $javascript2->addToValidateComparison('localite_c''varchar''Localité'true$prefix='','Boston');

    Thanks for your assistance.
    :: Keep Open Spirit ::

  4. #4
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: Custom validation based on role

    Hi, Damien

    In the addToValidateComparison(), the javascript check if the field has the same value of compareTo.
    The other fields looks great.

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  5. #5
    Snook is offline Sugar Community Member
    Join Date
    Jan 2007
    Posts
    52

    Default Re: Custom validation based on role

    Muito obrigado andopes for your help.

    After many try and inspections with firebug and firephp (thanks to this two awesome addons), I finally found how to use the function addToValidateComparison(). But it's still buggy. Here is how to use it:

    In Accounts/EditVIew.php, after: $javascript = new javascript(); and the calls to functions of the javascript's object I add:
    PHP Code:
    require_once('modules/Users/User.php');
    $check_role = new User();
    if (
    $check_role->check_role_membership('Other'$user_id '')) {
        
    //addToValidateComparison($field, $type, $displayName, $required, $prefix='',$compareTo)
        
    $javascript->addToValidateComparison('city_c''varchar''The city must be Boston''true'$prefix='','city_ref_boston');

    So, true must be between ' ', and the $compareTo must refer to an other field for the comparison ... why not ! So I refer to this field with 'city_ref_boston' and added an empty input field in my form in Accounts/EditView.html:
    HTML Code:
    <td width="30%" valign="top" class="dataField">
      <span sugar='slot124b'>
        <input type='hidden' name='city_ref_boston' id='city_ref_boston' size='35' maxlength='50' value='Boston'>&nbsp;
      </span sugar='slot'>
    </td>
    In this case, if the user enter each time a value else than "Boston", the error message will appear each time and the form won't be save (what I expect, so that's fine in this case).

    But, if the user let the field that need to be checked (city_c) empty, he would have the error message bellow the field. If he try a 2nd time to save the form with an empty value or a value else than "Boston" the form will be saved !!! What I didn't expect it will do.

    Any idea how to resolve that ?

    Thanks a lot for suggestions.
    Damien
    :: Keep Open Spirit ::

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. upgraded to 4.5.1e, received "Metadata for table tracker does not exist"
    By sfgeorge in forum Installation and Upgrade Help
    Replies: 0
    Last Post: 2007-09-03, 02:24 PM
  2. Asterisk Patch 1.1.0 Crash on logon
    By skyracer in forum Help
    Replies: 6
    Last Post: 2006-07-08, 06:30 AM
  3. Replies: 7
    Last Post: 2006-06-06, 07:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •