I have problem with drupal 7 and form ajax callback.
I have simple form with form select item and when i choose something ajax callback show item with form radios. Its actually works, but I get only radios item title and description but no options to choose.
function my_test_form($form, &$form_state) {
$form['places'] = array(
'#type' => 'select',
'#title' => t('Select twitter trends location'),
'#options' => array('1' => 'item 1', '2' => 'item 2'),
'#default_value' => 1,
'#description' => t('Select description'),
'#ajax' => array(
'callback' => 'my_test_form_callback',
'wrapper' => '.form-item.form-type-select.form-item-places',
'method' => 'append',
'effect' => 'fade',
),
);
return $form;
}
function my_test_form_callback($form, &$form_state) {
$form['trends'] = array(
'#type' => 'radios',
'#title' => t('Select'),
'#options' => array(0 => t('Closed'), 1 => t('Active')),
'#default_value' => 0,
'#description' => t('Radios description.'),
);
return $form['trends'];
}
result of print $form['trends'] -> dpm(drupal_render($form['trends']));
<div class="form-item form-type-radios">
<label>Select trend </label>
<div class="form-radios"></div>
<div class="description">Radios description.</div>
</div>
I would be happy for every advice.
Thanks a lot.
The callback receives a built form. It's only responsible for returning the right part of the form.
function my_test_form_callback($form, &$form_state) {
return $form['trends'];
}
my_test_form is responsible for building the form for the first call and all the following AJAX calls. Simply put your condition inside this function.
Related
I have used bootstrap modal popup in my site. By clicking the button pop will open. It is working on the following code.
HTML:
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
But the button come from array.
<?php
$args['test'] = array(
array(
'type' => 'submit',
'id' => ' text',
'label' => 'Launch demo modal',
'desc' => site_url('test'),
'class' => 'span3',
'default' => '',
'options' => ''),
);
?>
How can I pass "data-toggle="modal" data-target="#myModal" in array.
Nothing different, just add those attributes in the array like this:
array(
'type' => 'submit',
'id' => ' musicfileupload',
'label' => 'Launch demo modal',
'desc' => site_url('test'),
'class' => 'span3',
'default' => '',
'options' => '',
'data-toggle' => 'modal', // <--
'data-target' => '#myModal' // <--
);
Update: Following is a working example:
$data = array(
'type' => 'submit',
'id' => ' musicfileupload',
'label' => 'Launch demo modal',
'desc' => site_url('test'),
'class' => 'span3',
'default' => '',
'options' => '',
'data-toggle' => 'modal', // <--
'data-target' => '#myModal' // <--
);
echo form_button($data);
I am building a simple invoice module using angular js and cake php.
The items fields are repeated using ng - repeat in my view as seen below
<div ng:controller="ItemsCtrl" ng:app>
<div class="row-fluid items" >
<hr>
<ul class="invoice_items" ng:init="invoice={items:[{serial:'',details:'',qty:0,unit:'',rate:0,discount:0,amount:0}],pf:0}">
<li ng:repeat="item in invoice.items">
<div class="clear"></div>
<div id="items_row">
<div class="field span1">
<?php
echo $this->TwitterBootstrap->input("Number", array(
"input" => $this->Form->text("Item.{{\$index}}.serial" , array('class' => 'serial span1' ,'placeholder' => 'S.No' , 'ng-model' => 'item.serial' , 'value' => '{{ $index + 1 }}' , 'readonly' => 'readonly' ))
)); ?>
</div>
Closing the appropriate tags in the end
I have the following code in my model for validation -
public $validate = array(
'id' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'serial' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'details' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'quantity' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'naturalnumber' => array(
'rule' => array('naturalnumber'),
'message' => 'Please enter a valid quantity'
),
),
'rate' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'numeric' => array(
'rule' => array('numeric'),
),
),
'discount' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'amount' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'numeric' => array(
'rule' => array('numeric'),
),
),
);
The issue is that the fields which are outside of the ng-repeat directive get validated as required , but since the fields inside ng-repeat get initialized on each page load, cakephp validation isnt applied to them .
Do you guys see any work around to this ? May be my entire architecture approach is wrong?
I think you should change the way you are solving the problem.
Use CakePHP to create a REST API and AngularJS for the frontend. It will be much easier (and better if you want to migrate your stack in the future, i.e. Dart or NodeJS).
You might want to read: https://github.com/hantsy/angularjs-cakephp-sample
Hope it helps.
I have already read the forum about make custom cgridview.. I want to make filter use datetimepicker.
Datetimepicker:
I can include already the datetimepicker, but when I choose the date, it became error. :(..
My date datatype on database is 'timestamp'
This is the code
<?php
$this->breadcrumbs=array(
'Events'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Event','url'=>array('index')),
array('label'=>'Create Event','url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('event-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Events</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button btn')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'event-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'afterAjaxUpdate'=>"function(){jQuery('#event_date_search').datepicker({'dateFormat': 'yy-mm-dd'})}",
'columns'=>array(
array(
'type' => 'raw',
'name'=> 'event_image',
'value' => 'CHtml::image("'.Yii::app()->request->baseUrl.'/uploads/event/$data->event_image", "event_image" ,array("width"=>100))',
'filter'=> false,
),
'event_name',
array(
'name' => 'event_date',
'type' => 'raw',
'filter'=>$this->widget('zii.widgets.jui.CJuiDatepicker', array(
'model'=>$model,
'attribute'=>'event_date',
'htmlOptions' => array(
'id' => 'event_date_search'
),
'options' => array(
'dateFormat' => 'yy-mm-dd'
)
), true)
),
array(
'name'=>'published',
'filter'=>CHtml::dropDownList('Event[published]', '',
array(
''=>'',
'1'=>'Published',
'0'=>'Not Published',
)
),
'value' =>'($data->published==1)?"Published":"Not Published"',
),
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
?>
Thank you very much...
You can change search() function of model class. It can be like this:
public function search() {
....
$criteria->addCondition('DATE_FORMAT(time_field, \'%Y-%m-%d\') = ' . $this->time_field);
....
}
but it can be need to translate input value of $this->time_field.
i had the exact same problem yesterday and for some unknown reason by moving the
'dateFormat' => 'yy-mm-dd'
from option to htmloptions fixed my problem. to be honest i have it in both htmloptions and options and don't have any problems so far
'htmlOptions' => array(
'id' => 'event_date_search',
'dateFormat' => 'yy-mm-dd'
),
'options' => array(
'dateFormat' => 'yy-mm-dd'
),
hope this helps
Could you replace your code
'options' => array(
'dateFormat' => 'yy-mm-dd'
)
with
'defaultOptions' => array(
'showOn' => 'focus',
'dateFormat' => 'yy-mm-dd',
'showOtherMonths' => true,
'selectOtherMonths' => true,
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
)
Hope this help
For detail check this link http://www.yiiframework.com/wiki/318/using-cjuidatepicker-for-cgridview-filter/
I created custom module and now from admin side on edit form i added extra field select type.
I want to change comments with onchange function for this specific field.See below my code.
$eventElem = $fieldset->addField('banner_type', 'select', array(
'label' => Mage::helper('multibanners')->__('Banner Style'),
'required' => false,
'onchange' => 'checkSelectedItem(this.value)',
'name' => 'banner_type',
'values' => array(
array(
'value' => 'Banner 1',
'label' => 'AnySlider',
),
array(
'value' => 'Banner 2',
'label' => 'Content Slider',
),
));
$eventElem->setAfterElementHtml("<script type=\"text/javascript\">function checkSelectedItem(selectElement){}</script>");
This is my code i alert the value and i got my value but it cannot show it in comments area .Did someone one know how to fix it ?
Thanks
This will update the comment (onchange) with the current selected option
$fieldset->addField('banner_type', 'select', array(
'label' => Mage::helper('multibanners')->__('Banner Style'),
'required' => false,
'onchange' => 'checkSelectedItem(this.value)',
'name' => 'banner_type',
'values' => array(
array(
'value' => 'Banner 1',
'label' => 'AnySlider',
),
array(
'value' => 'Banner 2',
'label' => 'Content Slider',
),
)
))->setAfterElementHtml("<small id='banner_type_comment'>Comments</small>
<script type=\"text/javascript\">
function checkSelectedItem(selectElement){
$('banner_type_comment').update($('banner_type')[$('banner_type').selectedIndex].text);
}
</script>");
Iam new in cakephp ,I need to validate a form.
This is the code:
Controller:
<?php
class TasksController extends AppController {
var $name = 'Tasks';
var $helpers = array('Html','Form','Session');
public function index(){
}
function add_task()
{
if(!empty($this->data)) {
//print_r($this->data);
$this->Task->set($this->data);
if ($this->Task->validates()) {
// it validated logic
//echo "ttt";
} else {
// didn't validate logic
echo $errors = $this->Task->validationErrors;
}
}
}
}
?>
Model:
<?php
class Task extends AppModel
{
public var $name = 'Task';
var $useDbConfig = 'travanco_erp';
public var $useTable = 'tbl_tasks'; // This model uses a database table 'exmp'
public var $validate = array(
'task_title_mm' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'The title field is required'
),
'task_description_mm' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'The description field is required'
),
'task_from_mm' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'The from date field is required'
),
'task_to_mm' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'The to date field is required'
)
);
}
?>
This is the view:
<div class="employeeForm" style="width:64%; padding:10px 30%;">
<?php echo $this->Form->create('test', array('class'=>'form'));?>
<fieldset style="width:36em; padding:0px 0px;">
<div style="width:475px; font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#333333; font-weight:bold; margin-left:20px; margin-top:10px;">Add Task</div>
<br/>
<?php
/*echo $this->Form->input('task_ids_mm', array( 'div'=>'frm_filed_new',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Task ID',
));*/
echo $this->Form->input('task_title_mm', array( 'div'=>'frm_filed_new',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Title',
));
echo $this->Form->input('task_description_mm', array( 'type' => 'textarea',
'cols'=>60,
'rows' => 5,
'div'=>'frm_filed_new',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Description',
));
echo $this->Form->input('task_from_mm', array( 'div'=>'frm_filed_new','id'=>'task_from_mm','value'=>'',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'From',
));
echo $this->Form->input('task_to_mm', array( 'div'=>'frm_filed_new','id'=>'task_to_mm','value'=>'',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'To',
));
?>
<br/>
<?php echo $this->Form->button('Submit', array('type'=>'submit','escape'=>true)); ?>
</fieldset>
<?php echo $this->Form->end(); ?>
</div>
The validation not working.
What is the error in my code?
How can i solve this?
EDIT:
It is the mistake of misconfiguration of databse.php file.Now its corrected .And the print_r($errors) displays the errors.But that not displayed in the view page , i mean near the textboxes.
This is that error array:
Array ( [task_title_mm] => Array ( [0] => The title field is required ) [task_description_mm] => Array ( [0] => The description field is required ) [task_from_mm] => Array ( [0] => The from date field is required ) [task_to_mm] => Array ( [0] => The to date field is required ) )
How can i put it in near the text box?
CakePHP is designed to automatically validate model and display validation errors. Auto validation runs on model save. In your case:
$this->Task->save($this->request->data);
above will trigger validation. There is no need to run: $this->Task->validates() - If you do so, you also have to take care of displaying validation error by your own. So I think you simply should try:
<?php
class TasksController extends AppController {
var $name = 'Tasks';
var $helpers = array('Html','Form','Session');
function add_task()
{
if ($this->request->is('post')) {
// If the form data can be validated and saved...
if ($this->Task->save($this->request->data)) {
//saved and validated
}
}
}
}
?>
one thing i noticed in your code that you are writing in your model
public var $validate=array();
instead try
public $validate= array() or var $validate=array();
Validation should work after words.
Thanks :)
Try this:
if ($this->Task->validates()) {
// it validated logic
//echo "ttt";
} else {
$this->validateErrors($this->Task);
$this->render();
}