PHP - Yii Framework tutorials, Extension, Component Devlopment: February 2015

Thursday, 12 February 2015

Yii simple ajax form validation

Here's how Yii implement AJAX validation in CActiveForm :


1)
Code for Form declaration generally found in _form.php

<? php $form = $this->beginWidget('CActiveForm', array(
'id'=>'sandy-form',
'enableAjaxValidation'=>true //switch on Ajax Validation on the client side
)); ?>
<? php echo $form->errorSummary($model); ?>

<div class="form-group">
<? php echo $form->labelEx($model,'title'); ?>
<? php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<? php echo $form->error($model,'title'); ?>
</div>
<? php $this->endWidget(); ?>

2) put your validation code in model file found in models folder.

Model File

public function rules()
{
return array(
array('title', 'required'),
array('title', 'length', 'max'=>255),

  );
}


3) Also put code corresponding controller file.
 
public function actionCreate()
{
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}

I hope you understand well.

Notes: Validation fires every time you tab out of a (changed) html form field. 
From the javascript/jquery point of view it works with blur function.


Wednesday, 11 February 2015

Yii 1.X Framework Migration Tips

Yii Framework Migrations - based on tool(1.1.14) 

Straight Serial

1) Modify config.php under protected/config. Also change in main.php same directory.

2) Come to Command Prompt. (Type "cmd" in run menu)

3) Go to the project directory . e.g. :D\wamp\www\phpproject\protected

Type "php.exe yiic migrate create san"

where, "san" is the migrate php file

It will ask you to confirm to create migration or not.

Press Y to confirm.

New migration created successfully. The file would save in protected/migrate directory. 

Executing the migration.
 Go to the project directory . e.g. :D\wamp\www\phpproject\protected

Type "php.exe yiic migrate".

Migration file will restore your db in localhost server. 

Yii remove index.php from URL and SEO URL tips


This one is using for removing index.php and also remove site portion from URL.

I am trying to remove index.php from the URL string for so many times. Finally i got some cases in which i want to share with you guys.


Verify mod_rewrite is enabled in your server you can check it to load php.ini file.

Just copied following code and paste into urlManager in main.php
Here's what I put under components file main.php:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
//'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'/'=>'site/index',
'blog'=>'post/index',
'<view:(about)>'=>'site/page',
'contact'=>'site/contact',
'login'=>'site/login',
),
//'urlSuffix'=>'.html',
),

Also,
You can make your own rule to your web page need:
Here, i have changed site/login to login and site/contact to contact.
Also , post/index to blog as you can see in the rules array.

Hope this is one helps you in many ways.




How to set Yii description tag and Meta keywords

Here I have put some stuff on how to set yii description tag and meta tag in head tag.

You can use as follows:

<?php 

//Setting yii Description

Yii::app()->clientScript->registerMetaTag('Welcome to Yii Description -  Yii frameworrk', 'description', null, array('lang' => 'en'));

// Setting yii set keywords

Yii::app()->clientScript->registerMetaTag('yii, php framework', 'keywords', null, array('lang' => 'en'));

?>


All you need to register is meta as simple we do in normal web page head struture.