vTiger Custom Field Validation beforesave

ⅰ亾dé卋堺 提交于 2019-12-12 06:14:06

问题


I have created a custom module on vTiger 6.5.

I have made an event handler for the module but I am wondering how I could perform some sort of validation on this field. So fat I have done this but I am unable to get it work, I have tested the handler just echoing a sting and it works fine.

Please see my code below. Thanks!

    <?php
/*+***********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 *************************************************************************************/
# getModuleName : Returns the module name of the entity.
# getId : Returns id of the entity, this will return null if the id has not been saved yet.
# getData : Returns the fields of the entity as an array where the field name is the key and the fields value is the value.
# isNew : Returns true if new record is being created, false otherwise. 
# 'vtiger.entity.beforesave.modifiable' : Setting values : $data->set('simple_field', 'value'); 

class isaHandler extends VTEventHandler { 
    function handleEvent($eventName, $entityData) {
        global $adb;
        $moduleName = $entityData->getModuleName(); 
        if($moduleName=='isa'){     
            if($eventName == 'vtiger.entity.beforesave.modifiable') {}
            if($eventName == 'vtiger.entity.beforesave') {
                if('currentamount' > 20000){
                    echo "Please go back and enter less than 20000";
                exit;
                }

            }
            if($eventName == 'vtiger.entity.beforesave.final') {}
            if($eventName == 'vtiger.entity.aftersave') {
            }
        }   
    }

}
?>

回答1:


After doing some searching around and looking at other peoples event handlers I managed to solve this by changing:

if($eventName == 'vtiger.entity.beforesave') {
                if('currentamount' > 20000){
                    echo "Please go back and enter less than 20000";
                exit;
                }

to

if($eventName == 'vtiger.entity.beforesave') {
                $price = $entityData->get('currentamount');
                if($price > 20000){
                    echo "Please go back and enter less than 20000";
                exit;
                }

Now I want to see if I can display the message and then give a link to go back to the entity module with all the fields still full.




回答2:


In my opinion you should use the recordPreSave function. It's allow you to display an information/error message before to save data on the database

Here is an example:

In your Edit.js:

donCache : {},
    checkDon : function(details) {
        // alert("checkOverlap");
        var aDeferred = jQuery.Deferred();

        var params = {
            'module' : 'Affectations',
            'action' : "checkAffectAmount",
            'mode': 'CtrlAffectAmount',
            'recordId' : details.don,
            'montant' : details.montant
        }
        AppConnector.request(params).then(
            function(data) {
                if (data.success == true) {
                    // console.log(data.result);
                    var statut = data.result.statut;
                    var reste = data.result.reste;

                    if(statut == "error"){
                        aDeferred.reject(data);
                    }else {
                        aDeferred.resolve(data);
                    }
                }
            },
            function(error,err){
                aDeferred.reject();
            }
        );
        return aDeferred.promise();
    },

    registerRecordPreSaveEvent : function(form) {
        var thisInstance = this;
        if (typeof form == 'undefined') {
            form = this.getForm();
        }
        form.on(Vtiger_Edit_Js.recordPreSave, function(e, data) {
            var check = false;
            var recordId = jQuery('input[name="record"]').val();

            if (!recordId || recordId) {

                var montant_affectation = jQuery("input[name='affectations_montant']").val();
                var don_id = jQuery("input[name='affectations_potentialid']").val();

                var params = {};
                if (!(check in thisInstance.donCache)) {
                    thisInstance.checkDon({
                        'montant' : montant_affectation,
                        'don': don_id
                    }).then(
                        function(data){
                            thisInstance.donCache[check] = data['success'];
                            form.submit();
                        },
                        function(data, err){
                            thisInstance.donCache[check] = data['success'];
                            var reste = data.result.reste;
                            var msg = app.vtranslate("<strong>Attention!</strong> La somme des affectations est supérieure  de ")+ reste +app.vtranslate(" Euros au montant du don");
                            Vtiger_Helper_Js.showPnotify(msg);
                            delete thisInstance.donCache[check];
                        }
                    );
                } else {
                    delete thisInstance.donCache[check];
                    return true;
                }
                e.preventDefault();
            }
        })
    },

The PHP part in modules/isa/actions:

<?php

/***********************************
** DEBUT ALTAIR - JPR 15/06/2016 ***
***********************************/

class Affectations_checkAffectAmount_Action extends Vtiger_Action_Controller {

    function __construct() {
        $this->exposeMethod('CtrlAffectAmount');
    }

    public function checkPermission(Vtiger_Request $request) {
        $moduleName = $request->getModule();
        $moduleModel = Vtiger_Module_Model::getInstance($moduleName);

        $userPrivilegesModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
        $permission = $userPrivilegesModel->hasModulePermission($moduleModel->getId());

        if(!$permission) {
            throw new AppException('LBL_PERMISSION_DENIED');
        }
    }

    public function process(Vtiger_Request $request) {
        $mode = $request->getMode();
        if(!empty($mode) && $this->isMethodExposed($mode)) {
            $this->invokeExposedMethod($mode, $request);
            return;
        }
    }

    function CtrlAffectAmount(Vtiger_Request $request){
        global $adb,$log;

        $log->debug("ALTAIR CtrlAffectAmount OK");

        $recordId = $request->get('recordId');
        $montant = $request->get('montant');

        // $query = $adb->pquery("SELECT SUM(unit_price) AS sommeaffectation FROM vtiger_products INNER JOIN vtiger_crmentity ON vtiger_products.productid = vtiger_crmentity.crmid WHERE don_affecte = ? AND vtiger_crmentity.deleted=0",array($recordId));
        $query = $adb->pquery("SELECT SUM(affectations_montant) AS sommeaffectation FROM vtiger_affectations INNER JOIN vtiger_crmentity ON vtiger_affectations.affectationsid = vtiger_crmentity.crmid WHERE vtiger_affectations.affectations_potentialid = ? AND vtiger_crmentity.deleted=0",array($recordId));
        $sommeAffectation = $adb->query_result($query,0,"sommeaffectation");

        $query = $adb->pquery("SELECT amount FROM vtiger_potential INNER JOIN vtiger_crmentity ON vtiger_potential.potentialid = vtiger_crmentity.crmid WHERE potentialid = ? AND vtiger_crmentity.deleted = 0", array($recordId));
        $montantDon = $adb->query_result($query,0,"amount");

        if ( ($sommeAffectation + $montant) == $montantDon) {
            $statut = "ok";
            $reste = "";
        } else if( ($sommeAffectation + $montant) > $montantDon) {
            $statut = "error";
            $reste = ($sommeAffectation + $montant) - $montantDon;

        }

        $value = array('statut'=>$statut, 'reste'=>$reste);

        $response = new Vtiger_Response();
        $response->setEmitType(Vtiger_Response::$EMIT_JSON);
        $response->setResult($value);
        $response->emit();
    }
}



回答3:


Oh. you have 1 invalid error here. please change:

if($eventName == 'vtiger.entity.beforesave') {
    $currentAmount = $entityData->get('currentamount');
    if($currentAmount > 20000){
        echo "Please go back and enter less than 20000";
        exit;
    }
}


来源:https://stackoverflow.com/questions/43144675/vtiger-custom-field-validation-beforesave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!