OCS: Validation

  • 2715
  • 0

摘要:OCS: Validation

OCS Validation

Code modification in this version

  1. Remove Methods in ocs2\lib\pkp\classes\submission\Submission.inc.php:
    • getBusinessCostTotal
    • getEquipmentCostTotal
    • getStaffCostTotal
    • getTotalCostEdu
    • getTotalCostSelf
    • getTotalCost
    • setBusinessCostTotal
    • setEquipmentCostTotal
    • setStaffCostTotal
    • setTotalCostEdu
    • setTotalCostSelf
    • setTotalCost
  2. Remove the values in the returned array from getLocaleFieldNames method in ocs2\classes\paper\PaperDAO.inc.php
    • 'staffCostTotal'
    • 'businessCostTotal'
    • 'equipmentCostTotal'
    • 'totalCostEdu'
    • 'totalCostSelf'
    • 'totalCost'
  3. Remove the specific variable strings in ocs2\classes\author\form\submit\AuthorSubmitStep3Form.inc.php
    1. Variable Strings
      • staffCostTotal
      • businessCostTotal
      • equipmentCostTotal
      • totalCostEdu
      • totalCostSelf
      • totalCost
    2. Removing conditions
      • the corresponding field checkers of Form Validator in the constructor
      • paper object settings with default values in initData method
      • user-submitted data assignments in readInputData method
      • names of fields for which data should be localized in getLocaleFieldNames method
      • Save and update changes to paper object in execute method
  4. Renew HTML and Javascript in ocs2\templates\author\submit\step3.tpl

Javascript code for cost fields validation:

// NeiL Cho, 2012/10/14
$(document).ready(function() {
    var staffCostEdu = $('#staffCost_Edu'), staffCostSelf = $('#staffCost_Self'), staffCostTotal = $('#staffCostTotal');
    var businessCostEdu = $('#businessCost_Edu'), businessCostSelf = $('#businessCost_Self'), businessCostTotal = $('#businessCostTotal');
    var equipmentCostEdu = $('#equipmentCost_Edu'), equipmentCostSelf = $('#equipmentCost_Self'), equipmentCostTotal = $('#equipmentCostTotal');
    var totalCostEdu = $('#totalCostEdu'), totalCostSelf = $('#totalCostSelf'), totalCost = $('#totalCost');

    var ErrInputs = {};
    var totalCostSelfIntVal = 0, totalCostIntVal = 0;

    var CostHooks = {
        'staffCost': function() {
            staffCostTotal.text(getCost(staffCostEdu) + getCost(staffCostSelf));
        },
        'businessCost': function() {
            businessCostTotal.text(getCost(businessCostEdu) + getCost(businessCostSelf));
        },
        'equipmentCost': function() {
            equipmentCostTotal.text(getCost(equipmentCostEdu) + getCost(equipmentCostSelf));
        }           
    };

    var ObjectHooks = {
        'Edu': function() {
            totalCostEdu.text(getCost(staffCostEdu) + getCost(businessCostEdu) + getCost(equipmentCostEdu));
        },
        'Self': function() {
            totalCostSelf.text(getCost(staffCostSelf) + getCost(businessCostSelf) + getCost(equipmentCostSelf));
        }
    };

    var SumHook = function() {
        totalCostSelfIntVal = getCostByString(totalCostSelf.attr('id'), totalCostSelf.text());
        totalCostIntVal = totalCostSelfIntVal + getCostByString(totalCostEdu.attr('id'), totalCostEdu.text());
        totalCost.text(totalCostIntVal);
    };

    var getCostByString = function(id, vStr) {
        return ErrInputs[id] ? 0 : (function(){
            var v = parseInt(vStr);
            return v ? v : 0;
        })();
    };

    var getCost = function(jqObj) {
        return getCostByString(jqObj.attr('id'), jqObj.val());          
    };

    $('#costtitle').change(function(evt) {
        var target = evt.target;
        var jsTar = $(target);
        if (target && target.nodeName == 'INPUT' && target.id) {
            var tVal = parseInt(jsTar.val());
            if (isNaN(tVal)) {
                jsTar.css('color', 'red');
                ErrInputs[target.id] = jsTar;
                return;
            }
            else if (ErrInputs[target.id]) {
                jsTar.removeAttr('style');
                delete ErrInputs[target.id];
            }

            jsTar.val(tVal);
            var idx = target.id.split('_');
            var ch = CostHooks[idx[0]];
            if (ch) ch();           
            var oh = ObjectHooks[idx[1]];
            if (oh) oh();
            SumHook();
        }           
    });                 

    $('#Step3Form').submit(function() {         
        if ($.isEmptyObject(ErrInputs)) {
            if( (totalCostSelfIntVal / totalCostIntVal) >= 0.2) alert('ddd');
        }           
        return false;
    });
});

HTML for cost fields, notice the name convention of the fields:

<tr valign="top">
    <td width="20%" class="label">{fieldLabel name="staffCost" required="true" key="paper.staffCost"}</td>
    <td width="25%" class="value"><input type="text" class="textField" name="staffCostEdu[{$formLocale|escape}]" id="staffCost_Edu" value="{$staffCostEdu[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="value"><input type="text" class="textField" name="staffCostSelf[{$formLocale|escape}]" id="staffCost_Self" value="{$staffCostSelf[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="label"><label class="textField" name="staffCostTotal" id="staffCostTotal">0</label></td> 
</tr>

<tr valign="top">
    <td width="20%" class="label">{fieldLabel name="businessCost" required="true" key="paper.businessCost"}</td>
    <td width="25%" class="value"><input type="text" class="textField" name="businessCostEdu[{$formLocale|escape}]" id="businessCost_Edu" value="{$businessCostEdu[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="value"><input type="text" class="textField" name="businessCostSelf[{$formLocale|escape}]" id="businessCost_Self" value="{$businessCostSelf[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="label"><label class="textField" name="businessCostTotal" id="businessCostTotal">0</label></td>   
</tr>

<tr valign="top">
    <td width="20%" class="label">{fieldLabel name="equipmentCost" required="true" key="paper.equipmentCost"}</td>
    <td width="25%" class="value"><input type="text" class="textField" name="equipmentCostEdu[{$formLocale|escape}]" id="equipmentCost_Edu" value="{$equipmentCostEdu[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="value"><input type="text" class="textField" name="equipmentCostSelf[{$formLocale|escape}]" id="equipmentCost_Self" value="{$equipmentCostSelf[$formLocale]|escape}" size="20" maxlength="40" /></td>
    <td width="25%" class="label"><label class="textField" name="equipmentCostTotal" id="equipmentCostTotal">0</label></td> 
</tr>

<tr valign="top">
    <td width="20%" class="label">{fieldLabel name="total" required="true" key="paper.total"}</td>
    <td width="25%" class="label"><label class="textField" name="totalCostEdu" id="totalCostEdu">0</label></td>
    <td width="25%" class="label"><label class="textField" name="totalCostSelf" id="totalCostSelf">0</label></td>
    <td width="25%" class="label"><label class="textField" name="totalCost" id="totalCost">0</label></td>
</tr>