Monday, 29 December 2014

Yii2: Validate value without model (Ad Hoc Validation)

Sometimes you need to do ad hoc validation for values that are not bound to any model.
If you only need to perform one type of validation (e.g. validating email addresses), you may call the [[yii\validators\Validator::validate()|validate()]] method of the desired validator, like the following:
$email = '';
$validator = new yii\validators\EmailValidator();

if ($validator->validate($email, $error)) {
    echo 'Email is valid.';
} else {
    echo $error;
Note: Not all validators support this type of validation. An example is the unique core validator which is designed to work with a model only.
If you need to perform multiple validations against several values, you can use [[yii\base\DynamicModel]] which supports declaring both attributes and rules on the fly. Its usage is like the following:
public function actionSearch($name, $email)
    $model = DynamicModel::validateData(compact('name', 'email'), [
        [['name', 'email'], 'string', 'max' => 128],
        ['email', 'email'],

    if ($model->hasErrors()) {
        // validation fails
    } else {
        // validation succeeds
The [[yii\base\DynamicModel::validateData()]] method creates an instance of DynamicModel, defines the attributes using the given data (name and email in this example), and then calls [[yii\base\Model::validate()]] with the given rules.
Alternatively, you may use the following more "classic" syntax to perform ad hoc data validation:
public function actionSearch($name, $email)
    $model = new DynamicModel(compact('name', 'email'));
    $model->addRule(['name', 'email'], 'string', ['max' => 128])
        ->addRule('email', 'email')

    if ($model->hasErrors()) {
        // validation fails
    } else {
        // validation succeeds
After validation, you can check if the validation succeeded or not by calling the [[yii\base\DynamicModel::hasErrors()|hasErrors()]] method, and then get the validation errors from the [[yii\base\DynamicModel::errors|errors]] property, like you do with a normal model. You may also access the dynamic attributes defined through the model instance, e.g., $model->name and $model->email.
Read more :

Monday, 24 November 2014

Yii2: createUrl method

Old Yii1 to create an url we write:


Now with Yii2 we have to write


Yii2: Error invalid configuration with modules

Invalid Configuration – yii\base\InvalidConfigException
The configuration for the "modules" component must contain a "class" element.
Check your config file.  don't put "modules" in component section.
Correct config:

Readmore about module : 

Sunday, 26 October 2014

[MySql] Reset MySQL root password on Linux

1. Login to server as root/su

2. Stop the MySQL service
service mysqld stop
* Note : Be carefully, all your application will be temporarily unable to use mysql.
3. Start MySQL Safe mode with skip grant tables option
mysqld_safe --skip-grant-tables & 
(press ctrl+C to exit, if required)

4. Start the MySQL service
service mysqld start

5. Log into the MySQL server without any password
mysql -u root -p mysql

6. Reset the password for ‘root’ user
UPDATE user SET password=PASSWORD('new-password') where user='root';

7. Flush privileges
flush privileges;

8. Stop MySQL Safe mode
killall mysqld

9. Start the MySQL service again
service mysqld start

10. Try to Log-in with the new password
mysql -u root -p 
<enter new password when prompted>

Friday, 19 September 2014

MySQL: Allowed to remote connect to MySQL server

MySQL Error : Host '' is not allowed to connect to this MySQL server
How to fix it ?

STEP 1: Change mysql config :

vi /etc/mysql/my.cnf
Comment out following lines or edit to your client ip :
#bind-address           =
Restart mysql server:
service mysql restart
STEP 2: Change GRANT privilege :

Login MySQL using command line .
Then run a command like below to grant access for user. Replace 'username' and 'password' with your username and password.
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
* '%' mean you can remote access from any ip address, You can replace it with a specified ip address.

Then flush MySQL :

If it's still not working. Let's check your server firewall (iptables ...) and client firewall (windows firewall, antivirus software)

PHP: Create big unique ID like Facebook or MongoDB

Most of web application using a relational database management system (MSQL, SQL ..etc) to store data as records. We need an unique id to use as a key Primary.
How to do it ?
--> 1,2,3,4,5..... #anyone can count the number of your records and more

* PHP uniqid :
--> 541cf7f7d685e, 541cf7f7d685e, 541cf7f7d685e .....  #somewhat similar?

* Or use a custom function like this :

function gen_id() {
    return sprintf( '%04x%04x%04x%04x%04x%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
Result : 6800cd499f25434aac48c0bb51f46307, caa02c4831194690ad8458c3a4e07a0c, 7488a4c459d64f8dbacd2b79f44ed4bf ......

- differences
- professional (like facebook, google, MongoDB ...)
high performance
- unrestricted

Reference : PHP uuid lib

Tuesday, 1 April 2014

Linux: Delete all contents of file by only a command in vi editor

To clear the contents of a file is opening. Let's try folow the following command.


Other basic useful command :
* Create a file
vi filename
* To exit vi and save changes:
         ZZ   or  :wq
* To exit vi without saving changes:
* To edit file content :
* Deleting Text :

*xdelete single character under cursor
 Nxdelete N characters, starting with character under cursor
 dwdelete the single word beginning with character under cursor
 dNwdelete N words beginning with character under cursor;
  e.g., d5w deletes 5 words
 Ddelete the remainder of the line, starting with current cursor position
*dddelete entire current line
 Ndd or dNddelete N lines, beginning with the current line;
  e.g., 5dd deletes 5 lines

* Searching Text:

 /stringsearch forward for occurrence of string in text
 ?stringsearch backward for occurrence of string in text
 nmove to next occurrence of search string
 Nmove to next occurrence of search string in opposite direction
* Screen Manipulation:

 ^fmove forward one screen
 ^bmove backward one screen
 ^dmove down (forward) one half screen
 ^umove up (back) one half screen
 ^lredraws the screen
 ^rredraws the screen, removing deleted lines

Excel: How to remove VBA password from excel file

If you want to remove VBA password protect excel file without password, please follow the following guidelines.

Step by step :
  1. Download a HEX editor (HxD Hex Editor)
  2. Open xls/xlsx file and Save As xls. Do this even if the file is xls, just to be sure
  3. Open xls file with a hex editor
  4. Search for DPB
  5. Replace DPB to DPx (Note: the editor overrides, not inserts. So pay attention)
  6. Save file
  7. Open file in Excel
  8. Click "Yes" if you get any message box. There will be a few
  9. Click Alt + F11
  10. Click Tools -> [Project Name] Properties
  11. Click Protection
  12. Enter a new password twice
  13. Save
  14. Close and open the file again
  15. Type your new password to get to the required information.