Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
935
Firm Validation on column using REGEX
posted

I am using Angular 11 and 2020-Dec IG build.

My grid has a column of Name (string) and a column of Type (dropdown of static string-type choices). The validation of Name is based on Type and a regex that looks like this: /^PLAN\_[0-9]{4}\_[0-9]{2}[a-zA-Z0-9\_]{0,39}$/

"PLAN" is one of many prefixes that align with specific types and are not interchangeable.

What I need is a dynamically appearing warning about the Name not properly validating and prevention of submission by either click or enter keypress. I intend to have a warning snackbar floating in the upper right corner of the grid (zindex = 999), a warning span in the igxRowEditText area, and the "done" button in the igxRowEditActions area disabled until the Name matches the correct format.

I am finding that (onCellEdit) is not working for me, neither is (keypress) or (input) on my template for the column.

HTML template for Name and igxRowEditText:

        <ng-template igxRowEditText>
            <span class="error" *ngIf="badName">{{badNameWarning}}</span>
        </ng-template>

        <igx-column [pinned]="true" field="name" header="Scenario Name" [dataType]="'string'" [resizable]="true" width="246px" [sortable]="true">
            <ng-template igxCellEditor let-cell="cell">
                <igx-input-group>
                    <input igxInput [igxFocus]="true" name="ScenarioName" [(ngModel)]="cell.editValue" (keypress)="validateName(cell.editValue, cell.scenarioType)">
                </igx-input-group>
            </ng-template>
        </igx-column>

Handling in my Component:

  //validation
  badName:boolean = false;
  badNameWarning:string = '';

validateName(scenName:string, type:string) {
    let prefix:string = '';
    this.badName = false;
    if (type == 'Commit' && !/^PLAN\_[0-9]{4}\_[0-9]{2}[a-zA-Z0-9\_]{0,39}$/.test(scenName)) { this.badName = true, prefix = 'PLAN'; }
    ...else if (type == ...types
        this.badNameWarning = `${type} Name should be: ${prefix}_YYYY_WW(optional [a-z,A-Z,0-9,-,_] to 50 total)`;
 };

Parents
  • 2660
    Offline posted
    Hello Chris,

     

    Thank you for the provided details and code-snippets.

     

    I have been looking into your question and created a sample to demonstrate how this can be achieved. You can find it here

     

    For the sake of simplicity and easier testing, the validated column in the sample is called “Value”, its data type is “number” and the validation requires that the value is greater than “100” and matches the following regex: /^\d+$/.

     

    You have started on the right track by defining an editor template for the “Name” column to be able to add validation logic. As you will see in the sample, the dynamic validation can be executed in the ngModelChange event and by subscribing to an observable of the input’s values, from where the validation messages are opened. Additionally, the validation logic needs to be executed in the cellEdit event, which is fired before committing the value and is cancelable.

     

    Regarding displaying the warnings and disabling the “Done” button, the sample demonstrates the following approaches:The “floating” snackbar:

    1) While the Ignite UI for Angular suite provides built-in Snackbar and Toast components, I suppose they do not fit your particular requirement about positioning the warning in the upper-right corner of the grid. Nevertheless, here is an additional sample applying an IgxToast, positioned at the top of the page.

    So, to have more control over the warning’s position, my suggestion is to use the IgxTooltip. It can accept an OverlaySettings object as a parameter of its open method. With the help of the positionStrategy property and its PositionSettings object, the tooltip can be placed accordingly. The target of the OverlaySettings can be directly specified as the grid's nativeElement, or as done in the sample -  as a point (using the grid’s html element ClientRect and specifying a bit of an offset, so that the tooltip is slightly distanced from the grid’s bounds).

    2) The warning span’s implementation is same as yours.

    3) Disabling the igxRowEditActions’  “Done” button by binding its “disabled” property to the “badName” variable. 

     

    Please, test the suggested sample on your side and let me know if it correctly addresses your requirements.

     

    Sincerely,
    Bozhidara Pachilova
    Associate Software Developer
Reply Children