ViewEdit Validation

Hello,
i’m trying to add some custom validation.

If at least one of email,phone_work,phone_mobile is not
filled then marks as required.

So i wrote my view.edit.php extension class like so:

class LeadsViewEdit extends ViewEdit
{
     public function __construct() {
        parent::ViewEdit();
        $this->useForSubpanel = true; 
        $this->useModuleQuickCreateTemplate = true; 
    }
    
    function display(){
        
        // Add custom validation
        global $mod_strings;
         $jsscript = <<<EOQ
                   <script>
                       $('#SAVE_HEADER,#SAVE_FOOTER').click(function() {
                            makerequired(); // onchange call function to mark the field required
                       });
                     function makerequired()
                     {
                        var status_email1 = $('#dbgit_email1_c').val(); // get current value of the field
                        var status_phonework = $('#phone_work').val(); // get current value of the field
                        var status_phonemobile = $('#phone_mobile').val(); // get current value of the field
                         if((status_email1 == '') && (status_phonework == '') && (status_phonemobile == '')){ // check if it matches the condition: if true,
                                addToValidate('EditView','dbgit_email1_c','varchar',true,'{$mod_strings['LBL_DBGIT_EMAIL1_C']}');    // mark  field required
                                $('#dbgit_email1_c_label').html('{$mod_strings['LBL_DBGIT_EMAIL1_C']}: <font color="red">*</font>'); // with red * sign next to label
                                addToValidate('EditView','phone_work','varchar',true,'{$mod_strings['LBL_OFFICE_PHONE']}');    // mark field required
                                $('#phone_work').html('{$mod_strings['LBL_OFFICE_PHONE']}: <font color="red">*</font>'); // with red * sign next to label
                                addToValidate('EditView','phone_mobile','varchar',true,'{$mod_strings['LBL_MOBILE_PHONE']}');    // mark  field required
                                $('#phone_mobile').html('{$mod_strings['LBL_MOBILE_PHONE']}: <font color="red">*</font>'); // with red * sign next to label
                            }
                            else{
                                removeFromValidate('EditView','dbgit_email1_c');                        // else remove the validtion applied
                                $('#dbgit_email1_c_label').html('{$mod_strings['LBL_DBGIT_EMAIL1_C']}: '); // and give the normal label back
                                removeFromValidate('EditView','phone_work');                        // else remove the validtion applied
                                $('#phone_work_label').html('{$mod_strings['LBL_OFFICE_PHONE']}: '); // and give the normal label back
                                removeFromValidate('EditView','phone_mobile');                        // else remove the validtion applied
                                $('#phone_mobile_label').html('{$mod_strings['LBL_MOBILE_PHONE']}: '); // and give the normal label back
                            }
                    }
                    makerequired(); //Call at onload while editing
                </script>
EOQ;

        
        
        //Call the parent display function
        parent::display();
        echo $jsscript; 
    }

}

This works quite good.

The problem is that when for example email is filled…all other fields still remains required.

After clicking 3/4 times to Save button, the Leads(in my case) is saved successfull.

How can i avoid this behavior?

Thank you

How can we set validation on mobile field for specific digits.?

2 Likes

Have the same concern <3

We have the same concern