jai
jai

Reputation: 29

Date validation for custom field - SuiteCRM Version 7.10.4 Sugar Version 6.5.25 (Build 344)

I have two fields in my module called: start_date_c & end_date_c with date datatype

These fields are not mandatory fields however when i enter data into the end_date_c field I would like to make it is not less than start_date_c

I have tried the following:

but as i am new to suiteCRM, i am not able to find positive response

Upvotes: 1

Views: 1246

Answers (3)

jai
jai

Reputation: 29

worked for me.
I added the following code to the fields in modules/custom_module/vardefs.php

 'audited' => true,
 'enable_range_search' => true,

and added the following to the start field

'validation' => 
      array (
        'type' => 'isbefore',
        'compareto' => 'enddate',
        'blank' => true,
      ),

Upvotes: 0

mrbarletta
mrbarletta

Reputation: 922

You will need 2 things

  1. Edit the file editviewdefs.php in the module you want to add the logic. This field will be autogenerated when you add the first custom field to the edit view.
  2. Create your custom JS logic to define when the field is valid.

This logic here will add a validation callback for your

 addToValidateCallback(
        'EditView', // Form Name
        'end_date_c', // field name
        'datetime', // Field type
        false, // Is required
        "End date cannot be earlier than start date", // Message
        function() { 
          //WRITE YOUR JS VALIDATION HERE, return true when is valid
        }); 

In the editviewdefs.php find the field definition and use the displayParams to make suite/sugar add the JS for you.

array (
  'name' => 'end_date_c',
 'displayParams' =>
             array (
              'updateCallback' => 'FUNCTIONNAME',
          ),
),

The last step ain't needed if you already have a global custom JS (like style.js file for a custom theme).

EDIT: javascript DisplaParams will not work, so added the updateCallback option.

Now this validation works in 2 ways.

  1. The updateCallback will be fired onChange
  2. The addtoValidateCallback will be fired on Save.

This will give you enough flexibility to validate the form.

Upvotes: 2

Abdur Rehman
Abdur Rehman

Reputation: 3293

Simple and one linear, try following in any JS file (added in module edit view):

addToValidateDateBefore('EditView', 'start_date_c', 'date', false,'Date Start', 'end_date_c' );

Upvotes: 0

Related Questions