Yii2 Color Picker in Form - forms

Is there any built in class or an extinction that makes the user pick color from something like this?

You can simply render a color input like this with out using any plugins.
<?= $form->field($model, 'color', [
'template' => "{input}"
])->input('color',['class'=>"input_class"]) ?>

You can use kartik\widgets\ColorInput widget. first install kartik\widgets\ColorInput from this link
e.g. (Updated)
use kartik\widgets\ColorInput; or kartik\color\ColorInput;(for previous ver.)
<?php $form = ActiveForm::begin([
'id' => 'form',
]); ?>
<?= $form->field($model, 'color_code')->widget(ColorInput::classname(), ['options' => ['placeholder' => 'Select Color...'],]); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-primary btn-create' : 'btn btn-info btn-create']) ?>
</div>
<?php ActiveForm::end(); ?>

Related

Cakephp Email format validation before submit the form

I need to validate the Email format before submitting the form in Cakephp.
I was provided an invalid email (i.e. testuser1 instead of testuser1#domain.com). The request was submitted, then got error. If email is invalid, request should not be submitted.
Very curious about what I missed in my code and also referred related questions in forum but that was not work for me. H
Code is:
//for email input
<div class="form-group">
<?= $this->Form->label("email", __('Email')); ?>
<?= $this->Form->text("email", [
'required' => true,
'label' => false,
'id' => "email",]);
?>
</div>
<div class="btn-group btn-group-spaced form-group" role="group" aria-label="Actions">
<?= $this->Form->button(__('Submit'), ['class' => 'btn orange btn-default']) ?>
<?= $this->Form->button(__('Clear'), ['class' => 'btn orange hollow btn-default ucase','id' => 'show-prof','type' => 'reset']) ?>
</div>
Try:
//for email input
<div class="form-group">
<?= $this->Form->label("email", __('Email')); ?>
<?= $this->Form->input("email", [
'required' => true,
'label' => false,
'id' => "email",
'type' => "email"
]);
?>
</div>
<div class="btn-group btn-group-spaced form-group" role="group" aria-label="Actions">
<?= $this->Form->button(__('Submit'), ['class' => 'btn orange btn-default']) ?>
<?= $this->Form->button(__('Clear'), ['class' => 'btn orange hollow btn-default ucase','id' => 'show-prof','type' => 'reset']) ?>
</div>
The 'type' => "email" will add a type attribute with the value email to the input.
Read: CakePHP Form helper.
Also, you can visit http://www.formvalidator.net/ for more validations
I am answering for my own question.
One small change needs to be done.
That is, instead of Form->text use Form->email to validate the email before submitting the form.
Code is like:
<?= $this->Form->email("email", [
'required' => true,
'label' => false,
'id' => "email",]);
?>

yii2 loading pjax form dynamically

Problem: I want to update GridView in pjax style but it redirect to the form creation page.
What the code below does:
Having an index page with GridView to display data list and open a form in a modal window to create new record. The form code is added into the modal dynamically.
When click on "Create Country" button on index page, it calls country/create to get the HTML code of the form and insert it into the modal, then it shows the modal window.
When click on the "Create" button on the form, it submit the form to country/create. This will return the HTML code of the index page, and I want it to update the GridView part, but it does not.
The code:
Controller CountryController.php
class CountryController extends Controller
{
public function actionIndex()
{
return $this->renderIndex();
}
public function actionCreate()
{
$model = new Country();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->renderIndex();
} else {
return $this->renderAjax('_form', [
'model' => $model,
]);
}
}
private function renderIndex()
{
$searchModel = new CountrySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
View index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\web\View;
$this->title = Yii::t('app', 'Countries');
$this->params['breadcrumbs'][] = $this->title;
yii\bootstrap\Modal::begin(['id' => 'modal']);
yii\bootstrap\Modal::end();
?>
<div class="country-index">
<h1><?= Html::encode($this->title) ?></h1>
<div>Current Time: <?= date('Y/m/d H:i:s') ?></div>
<p><?= Html::a(Yii::t('app', 'Create Country'),
['create'],
['class' => 'btn btn-success show-modal']) ?>
</p>
<?php Pjax::begin(['id' => 'pjax-grid']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
<?php
$this->registerJs("$(function() {
$('.show-modal').click(function(e) {
e.preventDefault();
$('#modal').modal('show').find('.modal-body')
.load($(this).attr('href'));
});
});", View::POS_READY, '.show-modal');
?>
View _form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\View;
?>
<?php
$this->registerJs(
'$("document").ready(function(){
$("#pjax-create").on("pjax:end", function() {
$.pjax.reload({container:"#pjax-grid"}); //Reload GridView
});
});'
, View::POS_READY, 'pjax-create-end');
?>
<div class="country-form">
<?php yii\widgets\Pjax::begin(['id' => 'pjax-create']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => TRUE]]); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yii\widgets\Pjax::end() ?>
</div>
Use Html::button instead Html::a and simple overwrite the content of the modal:
yii\bootstrap\Modal::begin(['id' => 'modal']);
echo '<div id="modal-content"></div>';
yii\bootstrap\Modal::end();
echo Html::button('Create Country', [
'onClick' => 'createCountry("' . Url::to([create]) . '")',
'class' => 'btn btn-primary'
]);
$script = <<< JS
function createCountry(url) {
$('#modal').modal('show').find('#modal-content').load(url);
}
JS
$this->registerJs($script, View::POS_END);

Selected an option in edit view cakephp 3.0

I am generating list within a form like the following code. But for updating view how can select the particular option according to the user. Suppose for this user's role is Customer. Then how can select this(Customer) option in CakePHP 3.
<?= $this->Form->create($user) ?>
<?php
echo $this->Form->input('fullname');
echo $this->Form->input('contact_no');
?>
<div class="form-group">
<?php echo $this->Form->select(
'role',
['Customer', 'Staff', 'User'],
['title' => 'Select user type']
); ?>
</div>
<?= $this->Form->end() ?>
Here is what I did : (According that I have 2 tables : Users which belongsTo Roles)
In my UsersController, i added this following line :
$this->set('roles', $this->Users->Roles->find('list')->order('name'));
Then, you'll need to edit your view from
<div class="form-group">
<?php echo $this->Form->select(
'role',
['Customer', 'Staff', 'User'],
['title' => 'Select user type']
); ?>
</div>
to
<div class="form-group">
<?= $this->Form->select(
'role_id',
$roles,
['id' => 'role-id', 'required' => true]
);
?>
</div>

put submit button in Tabs::widget in yii2

I have a Tabs::widget that all settings are located in different tabs in a ActiveForm and admin can set config in each tab and once submit.(multiple forms in one widget )
in setting view :
<?php $form = ActiveForm::begin(); ?>
<?php
echo \yii\jui\Tabs::widget([
'headerOptions' => ['class' => 'tabs'],
'itemOptions' => ['tag' => 'div'],
'items' => [
[
'label' => 'serverSetting',
'content' => $this->render('serverSetting', ['model' => $model, 'form' => $form]),
'active' => true
],
[
'label' => 'emailSetting',
'content' => $this->render('emailSetting', ['model' => $model, 'form' => $form]),
],
[
'label' => 'smsSetting',
'content' => $this->render('smsSetting', ['model' => $model, 'form' => $form]),
],
],
]);
?>
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary', 'name' => ''])) ?>
</div>
<?php ActiveForm::end(); ?>
in view of one of the tabs (smsServer view):
<?php
use yii\helpers\Html;
use app\components\ActiveForm;
?>
<div class="user-form">
<?= $form->field($model, 'login')->textInput(['placeholder' => 'host']) ?>
<?= $form->field($model, 'password1')->textInput(['placeholder' => 'username']) ?>
<?= $form->field($model, 'wsdl')->textInput(['placeholder' => 'password']) ?>
<?= $form->field($model, 'from1')->textInput(['placeholder' => 'port']) ?>
</div>
in controller :
public function actionSetting()
{
$model = new Setting();
$model->setAttributes(Yii::$app->params, false);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
file_put_contents(Yii::getAlias('#app/config') . DIRECTORY_SEPARATOR . 'extra_params.php', base64_encode(serialize($model->attributes)));
}
return $this->render('setting', [
'model' => $model,
]);
}
My form and submit button do not work. where is my problem?
Seems you don't have an ActiveForm::end()
.......
<div class="btnForm">
<?= Html::submitButton(Yii::t('app', 'ثبت', ['class' => 'btn btn-primary'])) ?>
</div>
<?php ActiveForm::end(); ?>
and for debugging don't suppress the name of button
So I found my answer. My validation was false.
in rule():
[['login', 'wsdl', 'password','from1'], 'required',],
But I post form without filled all fields.
I changed to
[['login',], 'required',],
[['login', 'wsdl', 'password',from1'], 'safe',],

Yii2 saving form with multiple models

Hi I am very close to finish a project but got stuck saving a from with multiple models.
I have a grid that calls a controllers action that calls a form.
public function actionToday() {
$ID = $_GET["0"];
$modelCustomers = Customers::find()->where(['ID' => $ID])->one();;
$today = date("Y-m-d");
$beforeToday = 'DropinDate>'.$today;
$modelAttendance = Attendance::find()->where(['CustomersID' => $ID])->andwhere(['DropinDate' => $today])->one();
return $this->render('//attendance/_form-today-attendance', ['modelCustomers' => $modelCustomers, 'model' => $modelAttendance]);
}
In the form i have 3 main fields to update or in case is not record i need to create a new record.
This is the _form-today-attendance
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
?>
<?php $form = ActiveForm::begin(); ?>
<h3>Your Personal Details</h3>
<?= $form->field($modelCustomers, 'Name')->textInput(['readonly' => true]) ?>
<?= $form->field($model, 'DropinDate')->textInput(['readonly' => true]) ?>
<div class="attendance-form">
<?= $form->field($model, 'Dropin')->checkbox() ?>
<?= $form->field($model, 'Doctor')->checkbox() ?>
<?= $form->field($model, 'Lawyer')->checkbox() ?>
<?= $form->field($model, 'Observation')->textInput(['maxlength' => 250]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
When I debug i cant get anything happend in the Attendence model or the Customers model.
Any ideas?
Thanks a lot,
Eduardo
Try this function and check what you get.
public function actionToday()
{
$ID = $_GET["0"];
$modelCustomers = Customers::find()
->where(['ID' => $ID])
->one();;
$today = date("Y-m-d");
$beforeToday = 'DropinDate>' . $today;
$modelAttendance = Attendance::find()
->where(['CustomersID' => $ID])
->andwhere(['DropinDate' => $today])
->one();
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
//do something with $data
}
return $this->render('//attendance/_form-today-attendance', [
'modelCustomers' => $modelCustomers,
'model' => $modelAttendance]);
}
There will be something in array, you can assign it to model instances.