Showing posts with label cakephp. Show all posts
Showing posts with label cakephp. Show all posts

Cakephp default values

Monday, July 23, 2012


I know many of framework users are tired with default value option for forms.Here is method you can use for cakephp.

create('Post', array(
'url' => '/posts/index',
'type' => 'file',
));

echo $form->input('text', array(
'type' =>'text',
'value' => 'default value',
));
echo $form->input('textarea', array(
'type' =>'textarea',
'value' => 'default value',
));

$options = array(
'1' => 'Option 1',
'2' => 'Option 2',
'3' => 'Option 3',
);
echo $form->input('select', array(
'type' =>'select',
'options' => $options,
'selected' => 2,
'empty' => 'Please Select'
));


echo $form->input('multiple_select', array(
'type' =>'select',
'options' => $options,
'selected' => 2,
'empty' => 'Please Select Multiple',
'multiple' => true
));


echo $form->input('checkbox', array(
'type' =>'select',
'multiple'=> 'checkbox',        
'options' => $options,
'selected' => 2,
));

echo $form->input('radio', array(
'type' =>'radio',
'options' => $options,
'legend' => 'Radio',
'default' => '2'

));
?>

CheckBoxes
       
input('checkbox2', array(
'type'  => 'checkbox',
'value' =>'1',
'checked' => 'checked',
'label' => 'Choice 1'
));


echo $form->input('checkbox2', array(
'type'  => 'checkbox',
'value' =>'2',
'label' => 'Choice 2'
));
?>

input('file', array(
'type'  => 'file',

'label' => 'Browse File'
));

echo $form->input('date', array(
'type'  => 'date',
'label' => 'Date',
'minYear' => 1900,
'maxYear' => 2010,
# default order m/d/y
'selected' => array(
'month' => '03',
'day' =>  '09',
'year' => '1977'
 ),

));

echo $form->input('time', array(
'type'  => 'time',
'label' => 'Time',
'selected' => array(
'hour' =>  '12',
'min' => '15',
'meridian' => 'pm'
 ),
));      


echo $form->input('datetime', array(
'type'  => 'datetime',
'label' => 'Date Time',
'minYear' => 1900,
'maxYear' => 2010,
# default order m/d/y
'selected' => array(
'month' => '03',
'day' =>  '09',
'year' => '1977',      
'hour' =>  '13',
'min' => '10',
'meridian' => 'am'
),
));
echo $form->submit('Submit');
echo $form->end();      
?>

Reference and thanks:azrilnazli.blogspot

Continue Reading...

CakePHP: Date field with default values



it's a bit of a hack and quite ugly, but since the empty option doesn't appear to support multiple values it's probably the easiest solution - unless you want to rewrite the whole dateTime() function. str_replace unfortunately doesn't allow limiting the number of replacements, which is why we have to resort to preg_replace.

Cakephp code:

echo $form->input('date', array(
'type'  => 'date',
'label' => 'Date',
'empty' => TRUE,
'minYear' => 2000,
'dateFormat' => 'DMY',
'maxYear' => date('Y'),
'minYear' => date('Y') - 10
# default order m/d/y
));

PHP Way out:
$placeholder = '[RandomStringWhichDoesNotAppearInTheMarkup]';
$out = $this->Form->input('date', array('label' => 'Date of Birth'
                                            , 'dateFormat' => 'DMY'
                                            , 'empty' => $placeholder
                                            , 'minYear' => date('Y') - 110
                                            , 'maxYear' => date('Y') - 0));

$escapedPlaceholder = preg_quote($placeholder, '/');
$out = preg_replace("/$escapedPlaceholder/", 'DATE', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'MONTH', $out, 1);
$out = preg_replace("/$escapedPlaceholder/", 'YEAR', $out, 1);

echo $out;
Jquery way:
        // var valid=true;

        jQuery(document).ready( function() {

            $("#dateDay option:first").text('DAY');
            $("#dateMonth option:first").text('MONTH');
            $("#dateYear option:first").text('YEAR');


        });






Continue Reading...

CK Editor Height and Width

Wednesday, December 21, 2011

You can not specify width and hieght in your input and expect work fine with CKEDITOR.Beacause it wont adjust height and width of CKEDitor

To specify height and width of of a CKEDiTOR

field use this code in your css

.cke_contents{
height: 400px !important;
width: 550px !important;
}
Continue Reading...

cakephp validation using jquery validator

Friday, December 9, 2011

In my project client was asked to do client side validation.I decided to use jquery validator in my Cakephp project.I tried find out "how to do" with my project..I got to read jquery validator plugin DOC and tried some demo version.After many fat burning session i decided to post in my blog so that starter like me can go through you it and find a solution.

Steps
--------
1.download jquery validator plugin from this link.

2.Copy jquery.validate.js and jquery.js in cake/app/webroot/js folder.

3.Use this code in your view to load script inline in your view

$options = array('inline'=>'inline');
echo $javascript->link('validator/jquery.validate.js',null,$options);

4.Try this was in your view

$(function(){
$('#admin_register').validate({
debug: false,
errorClass: "authError",
errorElement: "span",
rules: {
"data[User][email]": {
required: true,
email: true
},
"data[User][telephone]": {
number: true,
minlength: 10
}
},
highlight: function(element, errorClass) {
$(element).removeClass(errorClass);
}
});
});

5.Your form is going to be something like this.

echo $form->create('User', array('action' => 'admin_register','inputDefaults'=> array('label'=>false),'type' => 'file','id'=>'admin_register'));


POINTS TO GIVE SOME ATTENTION
-------------------------------------------------------------

$('#admin_register').validate({ ----------its using form element's ID .

"data[User][telephone]": {-----------------Give form input field's name attribute as mentioned here.(I made mistake in this section, that is i used data[User][telephone] instead of "data[User][telephone]" )
Continue Reading...

Fancy box close after login

Saturday, November 26, 2011

One of my friend used fancybox for login.And he could not close fancy box after login.So i tried to help him and guess what i used this code to its completion.

parent.$.fancybox.close(); parent.location.reload(true);exit();
Continue Reading...