Pick up a date with native codeigniter instead of HTML - codeigniter

I'm using CodeIgniter and the MVC architecture to build a form on ExpressionEngine.
My problem is that CodeIgniter doesn't have the input date that I can use with HTML. My code is something like:
$data['cycle_begin'] = form_input('cycle_begin');
$data['cycle_month'] = form_input('cycle_month');
if(isset($_POST['submit']))
{
ee()->db->insert('exp_credit_tracker_assoc',
array(
'cycle_begin' => $_POST['cycle_begin'],
'cycle_months' => $_POST['cycle_month']
)
);
}
The view is something in the line of:
<div class="form-group">
<label for="cycle_type">cycle_type</label><?= $cycle_type ?>
</div>
<div class="form-group">
<label for="cycle_begin">cycle_begin</label><?= $cycle_begin ?>
</div>
<div class="form-group">
<label for="cycle_month">cycle_month</label><?= $cycle_month ?>
</div>
Is there any form_date which I could use or my only option is to change the input in my view file and grab the value in some way in my control file?

No there is no any form_date input type which you could use.
you can do some thing like this :
$data['cycle_begin'] = form_input('date_name',['type' =>'date']);
You can modify form_input as date input like this
<?php
$data = array(
'name' => 'mydate',
'id' => 'mydate',
'value' => '03-04-2012',
'type' => 'date',
'format' =>'m-d-Y'
);
echo form_input($data);
?>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

Related

Input image in Yii2 Dynamic Form always null

I want to make dynamic input that save an image to my website.
I use yii2-dynamicform and Kartik input file extension. But, it always save it as null.
Thank you for your help
Ps : ... is other part of my code that not relevant with this question. :)
In controller :
<?php
namespace frontend\controllers;
use Yii;
use common\models\Election;
use common\models\ElectionSearch;
use common\models\Model;
use common\models\Kandidat;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;
use yii\web\Session;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
class ElectionController extends Controller
{
...
public function actionCreate()
{
$model = new Election();
$modelsKandidat = [new Kandidat];
if ($model->load(Yii::$app->request->post())){
$model->save();
$modelsKandidat = Model::createMultiple(Kandidat::classname());
Model::loadMultiple($modelsKandidat, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsKandidat) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsKandidat as $modelKandidat) {
$modelKandidat->id_election = $model->id_election;
if($modelKandidat->file = UploadedFile::getInstance($modelKandidat,'file'))
{
$imageName = date('dmyhis_').$modelKandidat->id_election;
$modelKandidat->file->saveAs('../../common/file/fotokandidat/'.$imageName.'.'.$modelKandidat->file->extension);
$modelKandidat->foto = $imageName.'.'.$modelKandidat->file->extension;
}
if (! ($flag = $modelKandidat->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id_election]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
} else {
return $this->render('create', [
'model' => $model,
'modelsKandidat' => $modelsKandidat,
]);
}
}
...
}
In _form :
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;
use yii\helpers\ArrayHelper;
use dosamigos\datepicker\DatePicker;
use wbraganca\dynamicform\DynamicFormWidget;
?>
<div class="election-form">
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data', 'id' => 'dynamic-form']]); ?>
...
<!-- mulai input kandidat !-->
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i>Vote</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 4, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsKandidat[0],
'formId' => 'dynamic-form',
'formFields' => [
'nama',
'deskripsi',
'riwayat',
'file',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsKandidat as $i => $modelsKandidat): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Kandidat</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelsKandidat->isNewRecord) {
echo Html::activeHiddenInput($modelsKandidat, "[{$i}]id_kandidat");
}
?>
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelsKandidat, "[{$i}]nama")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelsKandidat, "[{$i}]deskripsi")->textarea(['rows' => 6]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelsKandidat, "[{$i}]riwayat")->textarea(['rows' => 6]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelsKandidat, "[{$i}]file")->fileInput() ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<!-- selesai input kandidat !-->
...
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
It's from #shoara/#zahraj answer :
I think you forgot to specify index for your file `
foreach ($modelsKandidat as $keyindex=>$modelKandidat)
{
$modelKandidat->id_election = $model->id_election;
if ($modelKandidat->file = UploadedFile::getInstance($modelKandidat, "[{$keyindex}]file")) {
.......
}
}
see demo controller and form part,imagine $modelCatalogOption in demo is your $model = new Election(); and $modelsOptionValue is your $modelsKandidat = [new Kandidat]; if you try and check your values in your controller I'm sure you can solve this problem easily. Pay attention to actionCreate

how to visit or go to another page using button in codeigniter

Good day sir/maam!,
please help me to understand more about how MVC works. i am new in codeigniter and i cant really understand how the controller works when using a button. please teach me about how to navigate using button.. pls need your attention here.. :)
i have sample code here (login)
in my HTML
<?php echo validation_errors() ?>
<?php echo form_open(current_url()) ?>
<fieldset>
<div class="pull-right">
<label class="input login-input">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input placeholder = "Email Address" class = "form-control" value = "<?php echo set_value('txt_email') ?>" name = "txt_email" type="email"></input>
</div>
</label>
<label class="input login-input">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input placeholder = "Password" class = "form-control" name = "txt_password" type="password"></input>
</div>
</label>
<div class = "button-inline">
<button type="submit" class="login-button btn">Log in</button>
</div>
<hr class = "form group">
<label> You do not have an accout yet? Please click <a>here</a> to register</label>
<br>
<h6>Note: This website is for personal user only..</h6>
</div>
</fieldset>
<?php echo form_close() ?>
in my Controller
public function login()
{
$rules = array(
array('field' => 'txt_email',
'label' => 'Email',
'rules' => 'required|valid_email|callback_check_login|trim',
),
array('field' => 'txt_password',
'label' => 'Password',
'rules' => 'required|trim',
)
);
$this->form_validation->set_message('callback_check_login', 'invalid Email or Password');
if ($this->is_validated($rules)){
$this->render_client('homepage');
}
else{
$this->render_login('login', $this->data);
}
}
public function check_login(){
$where = array(
'email' => $this->input->post('txt_email'),
'password' => $this->input->post('txt_password')
);
return($this->database_model->select('tb_user', $where)) ? true : false;
}
and when i clicked the button(login), this url show
-> "http://localhost/myfirstwebsite/auth/login?txt_email=sample%40gmail.com&txt_password=sample123"
please i need your assistance! thanks!
If i understand you correct you want to call your method "login" when you press your login button. In your view replace:
<?php echo form_open(current_url()) ?>
with
<?php echo form_open('your_controller_name/login') ?>
In your confing.php you should set up your base_url:
$config['base_url'] = 'http://localhost/myfirstwebsite';
If you don't use mod_rewrite in your config.php your index_page shoud be set:
$config['index_page'] = 'index.php';
So when you click login button your form will point to your base URL plus the "your_controller_name/login" URI segments, something like this:
http://localhost/myfirstwebsite/index.php/your_controller_name/login

Form is not getting submitted when render partial in Yii

I am partially rendering a form in a view in which user can add subaccounts(profiles). The view in which the form is called is another form where all subaccounts are listed as radiolist.
Below is my view.
<div class="inputs">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'selectUser-form',
'enableAjaxValidation' => false,
)); ?>
<?php echo CHtml::activeRadioButtonList($model,'id', $model->getSubAccounts(),array('prompt'=>'Please Select'));?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Book Now' : 'Save'); ?>
<?php echo CHtml::Button('Cancel',array('submit'=>array('cancel','id'=>$model->a_id)));?>
<?php $this->endWidget(); ?>
<div id="data"></div>
<?php echo CHtml::ajaxButton ("Add Users",
CController::createUrl('/user/user/addSubAccount'),
array('update' => '#data'));
?>
Below is my addSubAccount action.
public function actionAddSubAccount()
{
$profile=new YumProfile;
if (isset($_POST['YumProfile']))
{
$profile->attributes = $_POST['YumProfile'];
$profile->user_id=Yii::app()->user->id;
if($profile->save())
$this->redirect(array('/home/create'));}
if(!Yii::app()->request->isAjaxRequest){
$this->render('subaccount_form', array('profile'=>$profile));}
else{
$this->renderPartial('subaccount_form', array('profile'=>$profile));
}
}
Below is subaccount_form.
<?php $this->title = Yum::t('SubAccounts');?>
<div class="wide form">
<?php $activeform = $this->beginWidget('CActiveForm', array(
'id'=>'subaccount-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnChange' => true,
'validateOnSubmit' => true,
),
));
?>
<div class="row"> <?php
echo $activeform->labelEx($profile,'Firstname');
echo $activeform->textField($profile,'firstname');
echo $activeform->error($profile,'firstname');
?> </div>
<div class="row"> <?php
echo $activeform->labelEx($profile,'Lastname');
echo $activeform->textField($profile,'lastname');
echo $activeform->error($profile,'lastname');
?> </div>
<div class="row"> <?php
echo $activeform->labelEx($profile,'Age');
echo $activeform->textField($profile,'age');
echo $activeform->error($profile,'age');
?> </div>
<div class="row submit">
<?php echo CHtml::submitButton(Yum::t('Add')); ?>
</div>
<?php $this->endWidget(); ?>
My form is rendering. But it's not getting submitted.What am i doing wrong?
After submitting,I need the view to be refreshed and to display the newly added user as an option in the radio list.
EDIT:
I tried like adding the following in the CActiveForm Widget array:
'action' => array( '/user/user/addsubAccount' ),
But still no result.Instead it is saving my data two times when i go through my direct way,meaning render method. :-(
It is because
'enableAjaxValidation'=>true,
Ajax validation is set to true in your form. Set its value to false
'enableAjaxValidation'=>FALSE, and then your form will submit :)
and if you want it to be true only then you should uncomment
$this->performAjaxValidation($model);
in your controller's action
Update 1
if(!Yii::app()->request->isAjaxRequest){
$this->render('subaccount_form', array('profile'=>$profile));}
else{
//change this line
$this->renderPartial('subaccount_form', array('profile'=>$profile),FALSE,TRUE);
}
This link might help you
on renderPartial the action attribute of the form is set to the current page instead of being set to the actual update or create url
My Solution
$form=$this->beginWidget('CActiveForm', array(
'id'=>'country-form',
'action' => Yii::app()->createUrl(is_null(Yii::app()->request->getParam('id'))?'/country/create':'/country/update/'.Yii::app()->request->getParam('id')),

CakePHP Ajax Update action

i would like to know if we have something to do to keep style in a update input with a Ajax->link?
I have this code.
<?= $this->Ajax->link('Update fields', array('controller' => 'residences', 'action' => 'edit', $residence['Residence']['id']),
array('update' => 'finance_dep_nourriture_commentaire')) ?>
and my div
<?= $this->Ajax->div('finance_dep_nourriture_commentaire') ?>
<?= $this->Form->input('Residence.finance_dep_nourriture_commentaire', array('size' => '10', 'alt' => 'cash')) ?>$/l'unité
<?= $this->Ajax->divEnd('finance_dep_nourriture_commentaire') ?>
my field have this code before the update
<div id="finance_dep_nourriture_commentaire">
<input id="ResidenceFinanceDepNourritureCommentaire" type="text" value="1600.00" alt="cash" size="10" name="data[Residence][finance_dep_nourriture_commentaire]" style="text-align: right;">mask=Object { attr="alt", mask="99.999999999999", type="reverse", more...}events=Object { keydown=[1], keypress=[1], keyup=[1], more...}handle=function()
$/l'unité
after the update
<div id="finance_dep_nourriture_commentaire">
<input id="ResidenceFinanceDepNourritureCommentaire" type="text" value="2000.00" maxlength="13" alt="cash" size="10" name="data[Residence][finance_dep_nourriture_commentaire]">
$/l'unité
how i can keep the mask, style, etc?
thanks.

Form_validation isn’t using rules from config file (CodeIgniter 2)

My form validation isn’t using the rules in a config file in CodeIgniter 2. I’m accessing the form at /admin/people/add and /admin/people/edit/id and submitting to /admin/people/save. When I submit the add form it just reloads add without reporting any validation errors (my form views will display validation errors if $this->form_validation->_error_array is not empty; this is working on my login form). When I submit the edit form I get a 404 error at /admin/people/save even though that URI works when I initially go to the edit page. I'm loading the form validation library in the constructor.
application/config/form_validation.php:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'people/save' => array(
array(
'field' => 'first_name',
'label' => 'first name',
'rules' => 'trim|required'
)
) // people/save
);
/* End of file form_validation.php */
/* Location: /application/config/form_validation.php */
application/controllers/admin/people.php:
public function add() {
$fields = $this->db->list_fields('people');
/* set_defaults makes an object with null values for the fields in the database */
$person = $this->form_validation->set_defaults($fields);
$data = array(
'action' => 'add',
'person' => $person,
'button_text' => 'Add Person'
);
$data['page_type'] = 'form';
$this->layouts->set_title('Add a Person');
$this->layouts->view('people/add_edit_person_form',$data);
} // add
public function save(){
if($this->form_validation->run('people/save') == FALSE){
if(is_numeric($this->input->post('person_id'))){
$this->edit();
} else {
$this->add();
}
} else {
redirect('people'); // test to see if it's passing validation
}
} // save
application/views/add_edit_person_form.php:
<?php
$attributes = array(
'class' => 'block',
'id' => 'add_edit_person'
);
echo form_open('admin/people/save');
?>
<div class="required<?php echo form_error('first_name')?' error':'';?>">
<label for="first_name">First name:</label>
<input name="first_name" id="first_name" type="text" value="<?php echo set_value('first_name',$person->first_name); ?>" size="75" maxlength="255" />
</div>
<input name="person_id" id="person_id" type="hidden" value="<?php echo set_value('id',$person->id); ?>" />
<button><?php echo $button_text; ?></button>
</form>
After you've defined your $config array in application/config/form_validation.php, you'll need to call the following function in order to set the rules:
$this->form_validation->set_rules($config);
Reference: Setting Rules Using an Array

Resources