This is in reference to this question. I have a form MakenewForm.php. My action:
class posterActions extends autoPosterActions
{
public function executeMakenew(sfWebRequest $request)
{
$this->form = new MakenewPosterForm();
if($request->isMethod(sfRequest::POST))
{
$this->form->bind($request->getParameter($this->form->getName()));
}
$this->poster= $this->form->getObject();
}
}
in my template:
<div id="sf_admin_content">
<?php include_partial('poster/form', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper, 'makenew'=>true)) ?>
</div>
I'm using admin-generator. and I have overridden _form.php, just changed submit action to /poster/makenew i.e. the same controller.
My MakenewForm.php has a Textarea widget.
Now I open /poster/makenew and submit it with the value of the textarea \r\n\r\n Line1\r\nLine2\r\n\r\nLine4
The form submits to the same action so I see the same page but not the value of the textarea is trimmed one linefeed from top i.e. \r\n Line1\r\nLine2\r\n\r\nLine4
Where exactly is the problem? I'm guessing $form->bind() but they say symfony has been tested rigorously so it might be my fault as well.
My form:
<?php
class MakenewPosterForm extends PosterForm
{
public static $choices= array('BRCA'=>"BRCA", 'BSA'=>'BSA');
public function configure()
{
$keys= (array_keys(self::$choices));
$default = $keys[0];
unset($this['id'], $this['created_at'], $this['updated_at'], $this['slug'],$this['approved'], $this['filename']);
$this->widgetSchema['preset']= new sfWidgetFormChoice(array('default'=>'BRCA','choices'=>self::$choices));
$this->validatorSchema['preset']=new sfValidatorChoice(array('choices'=>array_keys(self::$choices)));
$this->widgetSchema['text']= new sfWidgetFormTextarea();
$this->validatorSchema['text']= new sfValidatorString(array('required'=>false));
$this->widgetSchema->setHelp('name','Enter some keywords to describe your poster.');
$this->widgetSchema['places_list']->setOption('expanded', true);
$this->widgetSchema['user_id'] = new sfWidgetFormInputHidden(array(), array("value"=>"anonymous"));
$this->validatorSchema['user_id']=new sfValidatorString(array("required"=>false));
$this->widgetSchema['user_id']->setOption('is_hidden',true);
$this->setDefault('name',$default);
$this->setDefault('preset',$default);
$this->widgetSchema->setNameFormat('makenewposter[%s]');
}
}
and my makenewSuccess.php:
<?php use_helper('I18N', 'Date') ?>
<?php include_partial('poster/assets') ?>
<div id="sf_admin_container">
<h1><?php echo __('New Poster', array(), 'messages') ?></h1>
<?php include_partial('poster/flashes') ?>
<div id="sf_admin_header">
<?php include_partial('poster/form_header', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration)) ?>
</div>
<div id="sf_admin_bar">
<?php
$args=array('preset'=>
($form->getValue('preset'))? $form->getValue('preset'): $form->getDefault('preset'),
'text'=>
($form->getValue('text'))? $form->getValue('text'): $form->getDefault('text')
)
?>
<img src="<?php echo url_for('#poster_preview').'?'.http_build_query($args); ?>" width= <?php echo sfConfig::get('poster_preview_width',800)?>/>
</div>
<div id="sf_admin_content">
<?php include_partial('poster/form', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper, 'makenew'=>true)) ?>
</div>
<div id="sf_admin_footer">
<?php include_partial('poster/form_footer', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration)) ?>
</div>
</div>
_form template is pretty much same as the default one. I'm posting it anyway.
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<div class="sf_admin_form">
<?php echo $form->renderFormTag( url_for('#poster'.(isset($makenew)?'_makenew':'_create')), array('method' => 'POST') ); ?>
<?php echo $form->renderHiddenFields(false) ?>
<?php if ($form->hasGlobalErrors()): ?>
<?php echo $form->renderGlobalErrors() ?>
<?php endif; ?>
<?php foreach ($configuration->getFormFields($form, $form->isNew() ? 'new' : 'edit') as $fieldset => $fields): ?>
<?php include_partial('poster/form_fieldset', array('poster' => $poster, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>
<?php endforeach; ?><?php include_partial('poster/form_actions', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?></form></div>
Related
I am trying to use select2 querying the database because there are 30000 records this is really the only efficient way I could think of.
My problem is if I just submit this form the pole_id doesn't update.
Can someone help either with jQuery getting the id or telling me why the select box doesn't update the pole_id field in the db?
<?php
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'pole_id'); ?>
<?php //echo $form->textField($model,'pole_id',array('id'=>'pole_id')); ?>
<?php
$list = CHtml::listData(Poles::model()->findAll(array('order' => 'pole_number')), 'id', 'pole_number');
//echo $form->dropDownList($model, 'pole_id', $list, array('class'=>"js-example-basic-multiple", 'name'=>'pole_id', 'multiple'=>'multiple'));
//
echo CHtml::hiddenField('selectbox_pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#selectbox_pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="javascript">
$("#myselect").on("change", function (e) {
var id = $("#myselect").select2("data")[0].id;
// Now you can work with the selected value, i.e.:
//$("#pole_id").val(id);
alert(id);
});
</script>
You can just use CActiveForm::hiddenField() to generate this hidden input:
<div class="row">
<?php echo $form->labelEx($model, 'pole_id'); ?>
<?php
echo $form->hiddenField($model, 'pole_id', ['id'=>'pole_id']);
$this->widget('ext.select2.ESelect2', [
'id' => 'myselect',
'selector' => '#' . CHtml::activeId($model, 'pole_id'),
'options' => [
'allowClear' => true,
'placeholder' => 'Select a Pole',
'minimumInputLength' => 3,
'ajax' => [
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type' => 'GET',
'dataType' => 'json',
'quietMillis' => 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results' => 'js:function(data,page) {
var more = (page * 10) < data.total; return {results: data, more:more };
}',
],
],
]);
?>
<?php echo $form->error($model, 'pole_id'); ?>
</div>
It will correctly handle existing data and no special actions are required in controller to handle this input - $model->attributes = $_POST['Jpapoles'] will load pole_id as any other attribute.
But this extension should also work with models direly, so you can just use:
$this->widget('ext.select2.ESelect2', [
'model' => $model,
'attribute' => 'pole_id',
'options' => [
// ...
],
]);
The you don't need to create hidden filed yourself.
Two parts:
one I decided to var_dump in the controller action:
Which resulted in me discovering that hidden field with id: selectbox_pole_id is where the posted value was.
So, I renamed that id to match the db id pole_id and added the following line before save: $model->pole_id = $_POST['pole_id'];
public function actionCreate()
{
$model=new Jpapoles;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Jpapoles']))
{
$model->attributes=$_POST['Jpapoles'];
$model->pole_id = $_POST['pole_id'];
if($model->save())
//$this->redirect(array('jpas/view','id'=>$_GET['jpano']));
var_dump($_REQUEST);
//echo $_POST['pole_id'];
}
$this->renderPartial('create',array(
'model'=>$model
),false,true);
}
/* #var $this JpapolesController */
/* #var $model Jpapoles */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'jpapoles-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'pole_id'); ?>
<?php
echo CHtml::hiddenField('pole_id', '', array('class' => 'span5'));
$this->widget('ext.select2.ESelect2',array(
'id'=>'myselect',
'selector' => '#pole_id',
'model'=>$model,
'attribute'=>'pole_id',
'options' => array(
'allowClear'=>true,
'placeholder'=>'Select a Pole',
'minimumInputLength' => 3,
'ajax' => array(
'url' => Yii::app()->createUrl('jpapoles/poles'),
'type'=>'GET',
'dataType' => 'json',
'quietMillis'=> 100,
'data' => 'js: function(text,page) {
return {
q: text,
page_limit: 10,
page: page,
};
}',
'results'=>'js:function(data,page) { var more = (page * 10) < data.total; return {results: data, more:more };
}
',
),
),
));
?>
<?php echo $form->error($model,'pole_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php //echo $form->textField($model,'member_id'); ?>
<?php
$list = CHtml::listData(Members::model()->findAll(array('order' => 'abriviation')), 'id', 'abriviation');
echo $form->dropDownList($model, 'member_id', $list);
?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'jpa_id'); ?>
<?php echo $form->hiddenField($model,'jpa_id', array('value'=>$_GET['jpano'])); ?>
<?php echo $form->error($model,'jpa_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<script type="text/javascript">
// Or for single select elements:
$("#select").select2({
initSelection : function (element, callback) {
var data = {id: element.val(), text: element.val()};
callback(data);
}
});
</script>
I am getting this error in CakePHP from my forgot_password method which is in UsersController.
public function forgot_password() {
$this->layout = 'signin';
if (!empty($this->data)) {
$user = $this->User->findByUsername($this->data['User']['username']);
if (empty($user)) {
$this->Session->setflash('Sorry, the username entered was not found.');
$this->redirect('/users/forgot_password');
}else{
$user = $this->__generatePasswordToken($user);
if ($this->User->save($user) && $this->__sendForgotPasswordEmail($user['User']['id'])) {
$this->Session->setflash('Password reset instructions have been sent to your email address.
You have 24 hours to complete the request.');
$this->redirect('/users/login');
}
}
}
}
Here is the forgot_password.ctp file
<header class="panel-heading text-center">
<strong>Forget Password</strong>
</header>
<h3>Enter Your Username</h3>
<?php
echo $this->Form->create('User', array('action' => 'forgot_password', 'id' => 'web-form', 'class'=>'panel-body wrapper-lg'));
echo $this->Form->input('username', array('label' => 'Username', 'between'=>'<br />', 'type'=>'text', 'div' => 'form-group','class' => 'form-control input-lg'));
?>
<div class="form-group">
<?php echo $this->Form->submit('Send Password Reset Instructions', array('class' => 'btn btn-primary btn-techuz', 'id' => 'submit')); ?>
</div>
<?php echo $this->Form->end(); ?>
It's not necessary to specify the form action if it's same as the view. In case you'd like to redirect to a different action, try this:
<?php
echo $this->Form->create('User', array(
'url' => array(
'controller' => 'users','action' => 'forgot_password'
),
'id' => 'web-form',
'class' =>'panel-body wrapper-lg'
)
); ?>
If you follow the convention and define this "url" index as an array of controller and index, you're bound to be safe and deprived of errors.
Peace! xD
Got the solution, no need to mention 'action' => 'forgot_password' in forgot_password.ctp file until it is redirect to another method.
I have a category,And I need to create a subcategory of that.
When i tried to create the subcategory i have a field called category Name which should preloaded with the name of category which i wish to create subcategory.
how can i achieve this..
My controller
public function actionCreate($id)
{
$model=new SubCategory;
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SubCategory']))
{
$model->attributes=$_POST['SubCategory'];
if($model->save())
$this->redirect(array('create','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
And the form
<?php
/* #var $this SubCategoryController */
/* #var $model SubCategory */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sub-category-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<?php
$category_id = $_GET['id'];
$category = Category::model()->findByAttributes(array('id' => $category_id));
// echo '<pre>';print_r($category);'</pre>';
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->textField($model,'category_id',
array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'category_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'subcategory_name'); ?>
<?php echo $form->textField($model,'subcategory_name',array('class'=>'form-control','style'=>'width:300px;')); ?>
<?php echo $form->error($model,'subcategory_name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class' => 'btn btn-default')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<div style="width: 97%; margin: auto;" >
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'fav-category-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
// 'id',
//'favcategory',
array('header'=>'#','htmlOptions'=>array('width'=>'60px'),
'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
//'id',
//'category_id',
array(
'name' => 'category_id',
'value' => '$data->category->categoryname',
//'filter'=> CHtml::listData(Category::model()->findAll(array('order'=>'categoryname')), 'categoryid', 'categoryname')
),
'subcategory_name',
array(
'class' => 'CButtonColumn',
'htmlOptions'=>array('width'=>'110px'),
'template' => '{update}{delete}',
'buttons' => array(
'update'=>array(
'imageUrl' =>false,
'label' => '',
'options' => array('title'=>'update','class'=>'btn btn-info btn-xs fa fa-pencil-square-o'),
),
'delete'=>array(
'imageUrl' =>false,
'label' => 'delete',
'options' => array('title'=>'view','class'=>'btn btn-danger btn-xs'),
),
)
),
),
'itemsCssClass'=>'table table-striped table-bordered table-hover',
'pagerCssClass'=>'pagination',
'pager'=>array( 'header' => '','lastPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span><span class="glyphicon glyphicon-chevron-right"></span>','firstPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span><span class="glyphicon glyphicon-chevron-left"></span>','prevPageLabel'=>'<span class="glyphicon glyphicon-chevron-left"></span>','nextPageLabel'=>'<span class="glyphicon glyphicon-chevron-right"></span>','header' => '','cssFile' => Yii::app()->baseUrl . '/css/pager.css','htmlOptions'=>array('class'=>'pagination'),'selectedPageCssClass'=>'active'),
)); ?>
</div>
I can see category model $category getting loaded in the form.
$category = Category::model()->findByAttributes(array('id' => $category_id));
And if id in category table is primary key, then I suggest, just for simplicity:
$category = Category::model()->findByPk($category_id);
Now I am guessing you may want that in textField which will be disabled.
So use textField() of CActiveForm in the form, something like:
$form->textField($category, 'name', array('disabled' => 'disabled'));
textField docs
Note: The second parameter must be the attribute name, I have assumed name.
Also I don't see the use of the following two lines in actionCreate(). I mean, the variable $mode and $mode_1 are not used anywhere in that action.
$mode = Category::model()->findAll();
$mode_1=$this->loadModel1($id);
I am new to Yii. I have done a normal criteria search, and rendering them in the Grid view in Yii. If I click on the second page after searching/filtering, it again gives me the whole set of records in the Grid view.
My View:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'nimsoft-alerts-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<h1>Missing Hosts List</h1>
<?php //echo $form->errorSummary($model); ?>
<div style="float:left;">
<div class="row">
<?php echo $form->labelEx($model,'host_start_date'); ?>
<?php
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker', array(
'attribute' => 'host_start_date',
'language' => '',
'model' => $model,
'options' => array(
'mode' => 'focus',
'dateFormat' => 'yy-mm-dd',
//'minDate'=>'0',
'showAnim' => 'slideDown',
),
'htmlOptions' => array(
'style'=>'height:20px;',
'value' => $model->host_start_date,
),
));
?>
<?php echo $form->error($model,'host_start_date'); ?>
</div>
</div>
<div style="float:left;"> </div>
<div style="float:left;">
<div class="row">
<?php echo $form->labelEx($model,'host_end_date'); ?>
<?php
Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');
$this->widget('CJuiDateTimePicker', array(
'attribute' => 'host_end_date',
'language' => '',
'model' => $model,
'options' => array(
'mode' => 'focus',
'dateFormat' => 'yy-mm-dd',
//'minDate'=>'0',
'showAnim' => 'slideDown',
),
'htmlOptions' => array(
'style'=>'height:20px;',
'value' => $model->host_end_date,
),
));
?>
<?php echo $form->error($model,'host_end_date'); ?>
</div>
</div>
<div class="row buttons">
<?php echo CHtml::button('Search',array('submit' => array('Site/index')));?>
<?php echo CHtml::button('Search and Export',array('submit' => array('Site/Export')));?>
</div>
<?php $this->endWidget(); ?>
<!--<div class="row buttons">
<a href="<?php //echo $this->createUrl('Site/Index',array('id'=>$cust_id,'isXLSDownload'=>1));?>" title="Export For All Customers" class="btn btn-primary circle_ok" style="text-decoration: none; color:#FF3333;" ><b>Export All</b></a>
</div>-->
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enableSorting' => false,
'columns'=>array(
array( // display 'create_time' using an expression
'name'=>'alert_device',
'value'=>'$data->alert_device',
),
array( // display 'create_time' using an expression
'name'=>'alert_event_time',
'value'=>'$data->alert_event_time',
),
array( // display 'create_time' using an expression
'name'=>'alert_datetime',
'value'=>'$data->alert_datetime',
),
),
//'itemView'=>'_view',
));
?>
</div>
My Action:
public function actionIndex()
{
//$this->layout=false;
$model=new NimsoftAlerts;
if(isset($_POST['NimsoftAlerts']))
{
$model->attributes=$_POST['NimsoftAlerts'];
if($model->validate())
{
$pagination=1;
$criteria = new CDbCriteria();
$criteria->condition = "alert_datetime >= '$model->host_start_date' and alert_datetime <= '$model->host_end_date' and alert_itsm_ack_status IS NULL";
$details = NimsoftAlerts::model()->findAll($criteria);
$dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
'criteria' => $criteria,));
}
else $dataProvider=new CActiveDataProvider('NimsoftAlerts');
// if(isset($pagination))
// {
// $dataProvider=new CActiveDataProvider('NimsoftAlerts',array(
// 'criteria' => $criteria,));
// }
}
else
{ $dataProvider=new CActiveDataProvider('NimsoftAlerts'); }
if($_REQUEST['isXLSDownload']=='1')
{
$xlsName='Missing_Host_Details_'.date('YmdHis').'.xls';
$sheetName='Missing Host Details';
$headerTxt='Host Details';
$arrTh=array(
'alert_device'=>array('label'=>'Alert Device'),
'alert_event_time'=>array('label'=>'Alert Event Time'),
'alert_datetime'=>array('label'=>'Alert Datetime'),
);
$this->generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
//GlobalFuncs::generateCXLS($xlsName,$sheetName,$criteria,$model,$headerTxt,$arrTh);
}
$nimsoftAlerts = new NimsoftAlerts;
$viewNimsoftTktSts = $nimsoftAlerts->dispNimsoftTktSts();
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->render('index',array(
'viewNimsoftTktSts'=>$viewNimsoftTktSts,
'dataProvider'=>$dataProvider,
'model'=>$model,
));
}
On clicking the pagination buttons, this is what gets called -
$dataProvider=new CActiveDataProvider('NimsoftAlerts');
and not
if(isset($_POST['NimsoftAlerts'])) { [......] }.
So try this -
$model=new NimsoftAlerts;
$model->unsetAttributes(); // clear any default values
//Removed the content from here.
if(isset($_POST['NimsoftAlerts']))
{
$model->attributes=$_POST['NimsoftAlerts'];
if($model->validate())
{
[.....]
}
else {
$dataProvider = $model->search();
}
}
else {
$dataProvider = $model->search();
}
The $model->search() will add the filters and the pagination would include the filtered values received from $_GET[].
I hope it helps.
It seems that the selected dates from your custom search form does not get send when the pagination is used and so all records are fetched. So you have to send them manually.
I have implemented the same scenario in my sample project. Try to resemble it in your project.
In your view file -
//Your form has datepicker, this form has a textbox. On submit the values are posted to apply the filter.
<form id="filter_form" method="POST">
<input name="Skill[name]" id="Skill_name" value="<?php echo $model->name; ?>" />
<input type="submit" name="submit_btn" id="submit_btn" value="Submit" />
</form>
<script>
$(document).ready(function() {
$('.page a').click(function(ev) {
ev.preventDefault();
$.fn.yiiGridView.update('skill-grid', {data: $('#filter_form').serialize()}); //This will include the values from the form (dates in your case)
return false;
});
});
</script>
In your controller-action -
$model = new Skill;
$model->unsetAttributes();
if(isset($_REQUEST['Skill'])) {
$model->attributes = $_REQUEST['Skill'];
}
$dataProvider = $model->search();
$this->render('admin', array(
'dataProvider' => $dataProvider,
'model' => $model,
));`
Important Note: Replace the 'Skill' Model name with 'NimsoftAlerts'.
I hope it helps.
I write few files to form submit. But form validators is not working for choices(payment status).
This is my form.
class bookingAddForm extends sfForm
{
protected static $paymentStatus = array(
0 => 'Select Payment Status',
'PAID' => 'Paid',
'PARTIALLY_PAID' => 'Partially Paid',
'NOT_PAID' => 'Not Paid',
);
public function configure()
{
$this->widgetSchema['check_in'] = new sfWidgetFormInputText();
$this->widgetSchema['check_out'] = new sfWidgetFormInputText();
$this->widgetSchema['payment_status'] = new sfWidgetFormSelect(array('choices' => self::$paymentStatus));
unset($paymentStatus[0]);
$this->validatorSchema['check_in'] = new sfValidatorDate(array(
'min' => strtotime('today'),
'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~'
),
array(
'required' => 'From date is required',
'bad_format' => '"%value%" does not match the date format (DD/MM/YYYY).',
'min' => 'Invalid Date',
)
);
$this->validatorSchema['check_out'] = new sfValidatorDate(array(
'min' => strtotime('today'),
'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~'
),
array(
'required' => 'To date is required',
'bad_format' => '"%value%" does not match the date format (DD/MM/YYYY).',
'min' => 'Invalid Date'
)
);
$this->validatorSchema['payment_status'] = new sfValidatorChoice(array('choices' => array_keys(self::$paymentStatus)));
$this->widgetSchema->setNameFormat('reservation[%s]');
}
}
This is my action.
public function executeAddReservation(sfWebRequest $request)
{
$chaletId = $request->getParameter('id');
$this->chalet = skiChaletTable::getInstance()->getChalet($chaletId);
$this->form = new bookingAddForm();
if ($request->getMethod() == sfRequest::POST) {
$this->form->bind($request->getParameter($this->form->getName()));
if ($this->form->isValid()) {
die('submitted');
}
}
}
This is my view
<div class="form">
<form id="owner-add-reservation" action="<?php print url_for('owner_add_reservation', array('id' => $chalet->getId())); ?>" method="post">
<?php echo $form->renderHiddenFields(); ?>
<div class="row">
<div class="col">
<?php echo $form['check_in']->renderLabel(); ?><br/>
<?php echo $form['check_in']->render(); ?>
<img style="vertical-align:middle;" src="/images/icon_checkin_checkout.jpg">
<span id="<?php echo 'form_error_' . $form->getName() . '_check_in' ?>" class="form_error">
<?php echo $form['check_in']->getError(); ?>
</span>
</div>
<div class="col">
<?php echo $form['check_out']->renderLabel(); ?><br/>
<?php echo $form['check_out']->render(); ?>
<img style="vertical-align:middle;" src="/images/icon_checkin_checkout.jpg">
<span id="<?php echo 'form_error_' . $form->getName() . '_check_out' ?>" class="form_error">
<?php echo $form['check_out']->getError(); ?>
</span>
</div>
</div>
<div class="row">
<div class="col">
<?php echo $form['payment_status']->renderLabel(); ?><br/>
<?php echo $form['payment_status']->render(); ?>
<span id="<?php echo 'form_error_' . $form->getName() . '_payment_status' ?>" class="form_error">
<?php echo $form['payment_status']->getError(); ?>
</span>
</div>
</div>
<div class="row">
<div class="col float-right">
<input type="submit" value="Add Reservation" class="button_black"/>
<div class="col">
</div>
</form>
<div class="clearfix"></div>
</div>
Validation is works for check_in and check_out. But it is nor working for payement_status. Please help me.
This line of code in your form's configure() method
unset($paymentStatus[0]);
has no effect, because you're referencing the forms static variable self::$paymentStatus in the validator's options. You should change your sfValidatorChoice so that its options are
array('choices' => array_keys($paymentStatus))
instead of
array('choices' => array_keys(self::$paymentStatus))
Or what might be easier is to do this:
protected static $paymentStatus = array(
'PAID' => 'Paid',
'PARTIALLY_PAID' => 'Partially Paid',
'NOT_PAID' => 'Not Paid',
);
public function configure()
{
//....
$this->widgetSchema['payment_status'] = new sfWidgetFormSelect(array('choices' => array_merge(array('' => 'Select a payment status'), self::$paymentStatus)));
//...
}