Sunday 24 May 2020

Angular Reactive Form Validation with Custom Validators

Hey Guys, 

I worked on Angular Form group and Form controls validation recently.  we can validate the whole form controls at one go with angular cross field validation technique. 

Fundamentals & Reference :

Angular Reactive Forms:
https://angular.io/guide/reactive-forms

Validation  with custom validators for whole form:

https://angular.io/guide/form-validation#cross-field-validation


All we have to do is supply the certain options to new FormGroup() constructor. 

So, today we talk about two such properties of FormGroup 

1) Validators:

   This property is used to pass custom validator function. we create the validation functions as a directive which implements   ValidatorFn interface  of angular . It takes an Angular control object as an argument and returns either null if the form is valid, or ValidationErrors otherwise.

Note: Data cloned from the official angular documentaion . 
    Example : 


Form Group : 

const heroForm = new FormGroup({ 'name': new FormControl(), 'alterEgo': new FormControl(), 'power': new FormControl() });

To add a validator function . give option like below:

const heroForm = new FormGroup({ 'name': new FormControl(), 'alterEgo': new FormControl(), 'power': new FormControl() }, { validators: identityRevealedValidator });
Here identityRevealedValidator is a cross field validator function writte in a separate directive like below

Directive:

export const identityRevealedValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => { const name = control.get('name'); const alterEgo = control.get('alterEgo'); return name && alterEgo && name.value === alterEgo.value ? { 'identityRevealed': true } : null;

};Template goes like this to show errors:


<div *ngIf="heroForm.errors?.identityRevealed && (heroForm.touched || heroForm.dirty)" class="cross-validation-error-message alert alert-danger">
Name cannot match alter ego. </div>

Now Lets talk about another property of FormGroup
2) updateOn
 > Reports the update strategy of the AbstractControl (meaning the event on which the control updates itself). Possible values: 'change' | 'blur' | 'submit' Default value: 'change'
> Can be used for formControl / formGroup
submit:
> updates the formGroup values on submit instead each blur / change event
blur:
> on blur , the values get updated for formGroup / formControl based on given context.

change:
> On control change the values get updated.


In our above example, if we want to update formGroup values on submit then
const heroForm = new FormGroup({
'name': new FormControl(), 'alterEgo': new FormControl(), 'power': new FormControl() }, { validators: identityRevealedValidator , updateOn: 'submit' });

// the above will set updateOn:'submit/blur/change' for all controls as they are grouped under one formGroup.







Friday 24 April 2020

pyodbc -mysql connection in MAC OS [solved]

Hey Guys ,

I would like write this post after my personal exploration of making mysql + pyodbc connection successful in MAC OS.

To kick start with :


  1. I don't have mysql odbc drivers in my system.  so we have to download them.                  
    1.  We need to use unixODBC on the Mac because the default driver manager is not compatible with ODBC drivers for some databases. so we need to do this at first                    brew install unixodbc
    2. Download the mysql odbc connecter from this link https://dev.mysql.com/downloads/connector/odbc/ 
    3. Once we have them installed via dmg pacakge . we have them sit in your system. 
    4. Now we need to register these drivers . so that our system can connect. 
    5. Run the command  odbcinst -j in the terminal . you will see list of variables here which shows the data sources path detail files. 
    6. so, plz edit and save odbcinst.ini and odbci.int with below content.  

[MySQL ODBC 8.0 ANSI Driver]
Driver=/<pathin your system> /mysql-connector-odbc-8.0.19-macos10.15-x86-64bit/lib/libmyodbc8a.so
UsageCount=1


Connecting to DB using the odbc driver:

connection_string = (
    'DRIVER=MySQL ODBC 8.0 ANSI Driver;'    'SERVER=localhost;'    'PORT=3306;'    'DATABASE= dbname;'    'UID=root;'    'PWD=<your pwd>;'    'charset=utf8;')
 pyodbc.connect(connection_string);


sqlalchemy error:


  • suppose if we are using sqlalchemy to connect to db using mysql:pyodbc then it throws the below error 

cursor.execute(statement, parameters) TypeError: The first argument to execute must be a string or unicode query.

Fix for above error :   



supports_unicode_statements = True

https://github.com/sqlalchemy/sqlalchemy/issues/4869  : Removing the __init__ function helped me to get rid of the above error