Thursday, August 27, 2009

Data Validation in #cakePhp, #open Source, #php

Cake goes a long way in taking the monotony out of form input validation. Everyone hates coding up endless forms and their validation routines. CakePHP makes it easier and faster.

To take advantage of the validation features, you'll need to use Cake's FormHelper in your views. The FormHelper is available by default to all views at $form.

Here's our add view:

Plain Text View
create('Post');echo $form->input('title');echo $form->input('body', array('rows' => '3'));echo $form->end('Save Post');?>
Here, we use the FormHelper to generate the opening tag for an HTML form. Here's the HTML that $form->create() generates:

Plain Text View

If create() is called with no parameters supplied, it assumes you are building a form that submits to the current controller's add() action (or edit() action when id is included in the form data), via POST.

The $form->input() method is used to create form elements of the same name. The first parameter tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options - in this case, the number of rows for the textarea. There's a bit of introspection and automagic here: input() will output different form elements based on the model field specified.

The $form->end() call generates a submit button and ends the form. If a string is supplied as the first parameter to end(), the FormHelper outputs a submit button named accordingly along with the closing form tag. Again, refer to Chapter "Built-in Helpers" for more on helpers.

Now let's go back and update our /app/views/posts/index.ctp view to include a new "Add Post" link. Before the , add the following line:

Plain Text View
link('Add Post',array('controller' => 'posts', 'action' => 'add'))?>
You may be wondering: how do I tell CakePHP about my validation requirements? Validation rules are defined in the model. Let's look back at our Post model and make a few adjustments:

Plain Text View
array( 'rule' => 'notEmpty' ), 'body' => array( 'rule' => 'notEmpty' ) );}?>
The $validate array tells CakePHP how to validate your data when the save() method is called. Here, I've specified that both the body and title fields must not be empty. CakePHP's validation engine is strong, with a number of pre-built rules (credit card numbers, email addresses, etc.) and flexibility for adding your own validation rules. For more information on that setup, check the Data Validation chapter.

Now that you have your validation rules in place, use the app to try to add a post with an empty title or body to see how it works. Since we've used the input() method of the FormHelper to create our form elements, our validation error messages will be shown automatically.



Thursday, August 20, 2009

Html link in plugin using #cakephp, #php, #opensource, # programming

when you have to make link for plugin controller action while you are not working in plugin. I means makeing link to plugin from view is pretty simple
like
i have app/plugin/Testplugin/testplugin_conrollers
and i am at this url
http://localhot/projectname/user/index

So in view/users/index.ctp i will make url link to testplugin index function
link('Configure', array('plugin' =>'Testplugin', 'controller' => 'testplugin', 'action' => 'index'));?>

Monday, August 17, 2009

captcha in cakephp # php, # cakephp, #opensource

download libraray
http://www.captcha.ru/kcaptcha.zip
unzip and past it in vendor
app/vendor/place here
-----------------------------------------------------
code controller model and view as normal
------------------------------------------------------
class KCaptchaComponent extends Object
{
function startup(&$controller)
{
$this->controller = $controller;
}

function render()
{
vendor('kcaptcha/kcaptcha');
$kcaptcha = new KCAPTCHA();
$this->controller->Session->write('captcha', $kcaptcha->getKeyString());
}
}
?>

save this as [web]/app/controllers/components/kcaptcha.php

Model of users
class User extends AppModel
{
var $name = 'User';

/**
* validation rules
*/
var $validate = array (
'user_login' => array(
'exists' => array(
'rule' => array( 'checkUnique', 'user_login' ),
'message' => 'The Username you entered has been taken.'
),
'minLength' => array(
'rule' => array('minLength', 3),
'message' => 'Username must at least be 3 character long.'
)
),
'user_pass' => array(
'mingLength' => array(
'rule' => array('minLength', '6'),
'message' => 'Mimimum 6 characters long'
)
),
'user_name' => array(
'minLength' => array(
'rule' => array('minLength', 3),
'message' => 'Username must at least be 3 character long.'
)
),
'user_email' => array (
'email' => array(
'rule' => 'email',
'message' => 'Please supply a valid email address.'
),
'exists' => array(
'rule' => array( 'checkUnique', 'user_email' ),
'message' => 'The email you entered has been registered.'
)
)
);

/**
* Validate if the data is unique.
*
* @param $data The data to be compared.
* @param $fieldName The field name to check.
* @return true If the field name unique. False otherwise.
*/
function checkUnique( $data, $fieldName ) {
$valid = false;
if(isset($fieldName) && $this->hasField($fieldName)) {
$valid = $this->isUnique(array($fieldName => $data));
}
return $valid;
}
}
?>


Controller
class UsersController extends AppController
{
var $name = 'Users';
var $components = array('Auth', 'KCaptcha');

function beforeFilter() {
$this->Auth->fields = array('username' => 'user_login', 'password' => 'user_pass');
$this->Auth->allow('register','kcaptcha' );
}


/**
* This method handle the user registration process.
* It will first of all, get the user basic information.
* After user submit the information, a hash key will be generated and
* stored in the database. An email will then send to user and pending
* for user activation
*/
function forget() {


if (!empty($this->data))
{
$username = $this->data['User']['username'];
if( strtolower($this->data['User']['captcha']) == strtolower( $this->Session->read('captcha'))) {

if ($user = $this->User->find(array('or' => array('username' => $username, 'email' => $username)), 'id, username, password, email, first_name, last_name'))
{

extract($user['User']);
$reset_code = Security::hash($username.$id.$password);
$this->set('reset_code', $reset_code);
$this->User->id = $id;
if($this->User->saveField('password_reset', $reset_code) &&
$this->_sendMailTemplate($email, $this->forgetPassword, $this->forgetSubject, 'forget')) {
$this->Session->setFlash('A password reset request has been sent. Check you email address: '.$username);
} else {
$this->Session->setFlash('Error resetting password. Contact Administrator');
}
$this->redirect('/users/login');
}
else {
$this->Session->setFlash('User does not exist.');
}
unset( $this->data['User']['captcha']);
}
else{
$this->Session->setFlash('captcha verification failed');
}
}
}
function kcaptcha()
{
$this->KCaptcha->render();
}
}
?>

views/users/forget.ctp
flash('auth'); ?>
flash(); ?>

create('User', array('action' => 'forget'));?>

Forget Password
echo $form->input('username', array('label' => 'Username or Email Address'));
echo $form->input('captcha', array( 'label' => 'Please type the text presented in the image : ',
'before' => 'user image html tag here="'. $form->url('/users/kcaptcha').'" '));
?>

end('Submit');?>

Saturday, August 8, 2009

Baitullah Mehsud is alive, still holding command of Taliban fighters: Hakimullah Mehsud

Tehrik-e-Taliban Pakistan, a coalition of various Taliban groups, has refuted the reports about killing of Taliban supremo Baitullah Mehsud in a drone strike. Hakimullah Mehsud, an influential Taliban commander while refuting the reports has said the Taliban will release a video of Baitullah Mehsud. He said Baitullah Mehsud was alive and still holding the command of Taliban fighters. He promised to release a recorded video of Mehsud within two to three days.


URL
http://www.nation.com.pk/pakistan-news-newspaper-daily-english-online/Politics/08-Aug-2009/Baitullah-Mehsud-is-alive-still-holding-command-of-Taliban-fighters-Hakimullah-Mehsud

Thursday, August 6, 2009

USE OF IF CONDITION IN FIND QUERY IN CONDITION ARRAYIN #CAKEPHP, #BAKERY ALL THING, #MVC

$condition = array();

if($this->data['Comment']['module_name'] == 'news' && $this->data['Comment']['news'] == 'all')
$condition['Comment.content_type'] = $this->data['Comment']['module_name'];
if($this->data['Comment']['module_name'] == 'opinions' && $this->data['Comment']['opinions'] == 'all')
$condition['Comment.content_type'] = $this->data['Comment']['module_name'];
if($this->data['Comment']['module_name'] == 'classifieds' && $this->data['Comment']['classifieds'] == 'all')
$condition['Comment.content_type'] = $this->data['Comment']['module_name'];



than
use find query
$result = $this->Comment->find('all', array('recursive'=> -1,'fields'=>array('*'),'conditions' => array('Comment.created >' => $fromDate,'Comment.created <' => $toDate, $condition)));