Zend_paginator doesn't change perPage value, when there're some GET parameters - zend-framework

It works perfectly, when I am on first page of the results - I can display the number of results I need.
But when I'm on another page, the number of results cannot be changed. Why is that?
And when I'm for example on 3 page, and I'm displaying 12 per page and I try to change it to 60, on the end of link I have:
perPage/12/submitPagin/Zastosuj/page/3?perPage=60&submitPagin=Zastosuj
Any ideas, anyone? :)
public listAction(){
(...)
$params = $this->_parseSearchParams( $this->getRequest()->getParams() );
$searchForm = new ListIndexForm( );
$searchForm->setAction( '' );
$searchForm->setDefaults( $params );
$this->view->searchForm = $searchForm;
$this->view->params = $params;
$auth = Zend_Auth::getInstance();
$sql = 'SELECT * FROM Images order by created desc LIMIT 500';
$result = $db->fetchAll($sql);
$this->view->images = $result;
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setCurrentPageNumber( $this->getRequest()->getParam( 'page', 1 ) );
$paginator->setItemCountPerPage( $params[ 'perPage' ] );
$this->view->paginator = $paginator;
}
my function to parse params:
private function _parseSearchParams ( $params )
{
if ( ! isset( $params[ 'perPage' ] ) ) {
$params[ 'perPage' ] = 24;
}
foreach ( $params as $key => $value ) {
if ( is_null( $value ) or $value == '' ) {
unset( $params[ $key ] );
continue;
}
switch ( $key ) {
case 'tags':
case 'im':
case 'module':
case 'submitupdate':
case 'controller':
case 'action':
case 'submit':
unset( $params[ $key ] );
continue;
break;
}
}
return $params;
}
My form to change the number of the results per page:
class ListIndexForm extends Zend_Form {
public function __construct($options = null) {
parent::__construct ( $options );
$this->setMethod ( 'GET' );
$perPageValues = array(
6 => 6,
12 => 12,
18 => 18,
24 => 24,
30 => 30,
60 => 60,
900 => 90,
150 => 150,
300 => 300,
600 => 600,
);
$this->addElement ( 'Select', 'perPage',
array (
'filters' => array(
'Digits'
),
'label' => 'Obrazków na stronę',
'MultiOptions' => $perPageValues,
'value' => 24,
)
);
$this->addElement ( 'Submit', 'submitPagin',
array (
'label' => 'Zastosuj',
)
);
}
}
view list.phtml:
(...)
<?=$this->searchForm;?>
<?foreach ($this->paginator as $row):?>
<?=$row['id']?>
<?enforeach;?>
<?= $this->paginationControl($this->paginator, 'Sliding', '../helpers/searchpagination.phtml', array( 'urlParams' => $this->params) ); ?>
searchpagination.phtml helper:
<?php
$urlParams = isset($this->urlParams) ? $this->urlParams : array();
?>
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url( array_merge( $urlParams, array('page' => $this->previous) ) ); ?>">
< Nowsze
</a> |
<?php else: ?>
<span class="disabled">< Nowsze </span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?= $this->url( array_merge( $urlParams, array('page' => $page))); ?>">
<?= $page; ?>
</a> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url( array_merge( $urlParams, array('page' => $this->next)) ); ?>">
Starsze >
</a>
<?php else: ?>
<span class="disabled">Starsze ></span>
<?php endif; ?>
</div>
<?php endif; ?>

You appear to be specifying the perPage parameter twice. (Zend Framework sees both as GET parameters)
perPage/12/submitPagin/Zastosuj/page/3?perPage=60&submitPagin=Zastosuj
What happens if you change the first one to 60? I realise this may cause problems for your URL structure.

Related

Showing custom fields of custom post type in a date ordered list on a page

I'm struggling with this. I add multiple custom field values: in this case event dates with their title and location to all my projects (custom post type) and want to display them in a table together on a page called Calendar. I tried to query the posts but then the custom fields will show as groups together, and not as a date ordered list.
The event titles match the post title in this case.
My question is: what would be the best solution? Creating all the event dates as custom fields within the posts and then show them all together on a single page called calendar, or create them all in a calendar page and then filter those on title to show only the relevant ones in a single post that match the page title?
The event name is taken from a post object, which seemed the best solution for the scenario where all events are created on the same page, but other wise I might use just the post title.
Hope there is someone to help!
My code for the calendar (the part with upcoming dates):
<div class="Rtable upcoming Rtable--collapse">
<?php $outs = array(); if( have_rows('events') ): ?>
<?php while ( have_rows('events') ) : the_row(); ob_start();
// Get sub field values.
$date = get_sub_field('date');
$location = get_sub_field('location');
$website = get_sub_field('website');
$title = get_sub_field('title');
$premiere = get_sub_field('premiere');
$event = get_sub_field('date', false, false);
$today = strtotime(date('Ymd'));
$upcoming = strtotime($event);
$datefin = new DateTime($event); ?>
<?php if ($upcoming >=$today): ?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php if( get_sub_field('date') ): ?><?php the_sub_field('date'); ?><?php endif; ?>
</div>
<div class="Rtable-cell table-project-title"><?php if( get_sub_field('premiere') == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif; ?>
<?php if( $title ): ?><?php foreach( $title as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?><?php the_title(); ?> <?php endforeach; ?> <?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?> <?php endif; ?>
</div>
<div class="Rtable-cell"><?php if( get_sub_field('location') ): ?><?php the_sub_field('location'); ?><?php endif; ?></div>
<div class="Rtable-cell Rtable-cell--foot"><?php if( get_sub_field('website') ): ?><a class="button" href="<?php echo esc_url( $website ); ?>" target="_blank">Tickets</a> <?php endif; ?></div>
<?php endif; ?>
<?php $outs[] = ob_get_clean(); endwhile; ?>
<?php
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>
</div><!-- UPCOMING END -->
I found the solution, to query by the custom field date. Result here:
<?php
/**
* Template Name: Calendar
*/
get_header();
?>
<?php get_header();
?>
<div class="content-header yellow">
<h1><?php the_title();
?></h1>
</div>
<!-- Kalender -->
<div class="yellow content">
<div class="Rtable upcoming Rtable--collapse">
<?php
// Create an empty array for storage
$post_data = array();
// Set arguments for your query
$args = array(
'post_type' => 'projects',
'posts_per_page' => -1
);
// Do our query with any arguments from above
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Setup our repeater
if ( have_rows( 'events' ) ):
while ( have_rows( 'events' ) ) : the_row();
// Optional - Only get posts that match a true false within our repeater
$event = get_sub_field( 'date', false, false );
$today = strtotime( date( 'Ymd' ) );
$upcoming = strtotime( $event );
$datefin = new DateTime( $event );
if ( $upcoming >= $today ) {
// Build our array with the data we need using key => value
$post_data[] = array(
'url' => get_the_permalink(),
'title' => get_the_title(),
'date' => get_sub_field( 'date' ),
'location' => get_sub_field( 'location' ),
'website' => get_sub_field( 'website' ),
);
}
endwhile;
endif;
}
} ?>
<?php // Custom function to use in our sort to list the items by date
function subval_sort( $a, $b ) {
if ( $a['date'] == $b['date'] )
return 0;
return $a['date'] < $b['date'] ? -1 : 1;
}
// Sort our multidimensional array by sub array value
usort( $post_data, 'subval_sort' );
// We can now work our data normally through easy to access methods
foreach ( $post_data as $post ) {
?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php $format_in = 'm/d/Y';
// the format your value is saved in ( set in the field options )
$format_out = 'd/m/Y';
// the format you want to end up with
$date = DateTime::createFromFormat( $format_in, $post['date'] );
echo $date->format( $format_out ) . ' ' ;
?>
</div>
<div class="Rtable-cell table-project-title"><?php if ( get_sub_field( 'premiere' ) == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif;
?>
<a href="<?php echo $post['url']; ?> ">
<?php
echo $post['title'] . ' ' ;
?>
</a>
</div>
<div class="Rtable-cell">
<?php echo $post['location'] . ' ' ;
?>
</div>
<div class="Rtable-cell Rtable-cell--foot">
<a class="button" href="<?php
echo $post['website']; ?> ">
tickets
</a>
</div>
<?php }
?>
</div>
</div>
<?php get_footer();
?>

Yii 2 Call to a member function saveAs() on string

new to Yii and I am getting this error on a Send Email page. The string in question is the path to the attachment and the attachment name and i am presuming that the saveAs function would expect a string. Any ideas what i am missing?
The form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model app\models\emails */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="emails-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'reciever_name')->textInput(['maxlength' => 50]) ?>
<?= $form->field($model, 'receiver_email')->textInput(['maxlength' => 200]) ?>
<?= $form->field($model, 'subject')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'attachment')->fileInput(['maxlength' => 255]) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
and the controller:
public function actionCreate()
{
$model = new emails();
if ($model->load(Yii::$app->request->post())) {
// upload the attachment
$model->attachment = UploadedFile::getInstance($model, 'attachment');
if($model->attachment)
{
parent::init();
$time = time();
//$model->attachment->saveAs('attachments/'.$time.'.'.$model->attachment->extension);
//$model->attachment = 'attachments/'.$time.'.'.$model->attachment->extension;
}
if($model->attachment)
{
$value = Yii::$app->mailer->compose()
->setFrom(['my_email#gmail.com' => 'Paul'])
->setTo ($model->receiver_email)
->setSubject ($model->subject)
->setHtmlBody ($model->content);
foreach ($model->attachment as $file) {
//$filename = 'attachments/'.$time.'.'.$model->attachment->extension;
$filename = 'attachments/file.jpg';
//var_dump($filename);die();
$file->saveAs($filename);
$value->attach('attachments/file.jpg');
//$value->attach('attachments/'.$time.'.'.$model->attachment->extension);
}
$value->send();
}else{
$value = Yii::$app->mailer->compose()
->setFrom(['my_email#gmail.com' => 'Paul'])
->setTo($model->receiver_email)
->setSubject($model->subject)
->setHtmlBody($model->content)
->send();
}
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
I have tried absolute paths as well as dymanic paths, but all has the same output, i am stuck
Save as() function require the actual path. So that is issue. Please make sure your path should be correct and accessible.

Adding token to the url of the Update action Yii2

I am wondering where can I add the token parameter to the url in the backend? What my url looks now at this moment is www.domain.com/admin/brand/update?id=26 and I simply want to add one token in the end to prevent caching like this www.domain.com/admin/brand/update?id=26&token=23782173812. But now at this moment I realized that I don't know where is that url constructed. This is my controller action:
public function actionUpdate($id)
{
$model = $this->findModel($id, false);
$settings = new Settings;
if ($model->load(Yii::$app->request->post())) {
$languages = Lang::find()->all();
foreach ($languages as $language) {
if ($language->default != 1) {
$title = 'title_' . $language->url;
$varTitle = Yii::$app->OutData->sanitize($model->$title);
$model->$title = $varTitle;
$description = 'description_' . $language->url;
$varDescription = Yii::$app->OutData->sanitize($model->$description);
$model->$description = $varDescription;
} else {
$model->title = Yii::$app->OutData->sanitize($model->title);
$model->description = Yii::$app->OutData->sanitize($model->description);
}
}
$model->link = Yii::$app->OutData->sanitize($model->link);
if ($model->save()) {
$model = $this->findModel($model->id, true);
foreach ($languages as $language) {
if ($language->default != 1) {
$urlName = 'url_' . $language->url;
$model->$urlName = MakeURL::parseUrlGoogle($model->$urlName, $model->id);
} else {
$model->url = MakeURL::parseUrlGoogle($model->url, $model->id);
}
}
$model->update();
$model->setImageFolder();
$model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
if (isset($model->imageFiles) and count($model->imageFiles) > 0) {
foreach ($model->imageFiles as $key => $file) {
$file->saveAs(Yii::getAlias('#frontend/web') . '/brands_images/'.$model->getImageFolderName().'/brand_' . $model->id . '_' . ($key + 1) . '.' . $file->extension);
$pic = Yii::getAlias('#frontend/web') . '/brands_images/'.$model->getImageFolderName().'/brand_' . $model->id . '_' . ($key + 1) . '.' . $file->extension;
/* $settings->compress($pic, $pic, 90);*/
}
}
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
And my form if it is needed:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\modules\storeadmin\models\Lang;
use frontend\components\OutData;
use kartik\file\FileInput;
/* #var $this yii\web\View */
/* #var $model backend\models\News */
/* #var $form yii\widgets\ActiveForm */
$_SESSION['KCFINDER'] = array(
'disabled' => false,
'uploadURL' => "/media",
'uploadDir' => Yii::getAlias('#frontend/web') . "/media/",
);
?>
<div class="news-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?php $languages = Lang::find()->all(); ?>
<ul class="nav nav-tabs">
<?php
foreach ($languages as $key => $language) {
if ($language->default == 1) {
echo '<li class="active"><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>';
} else {
echo '<li><a data-toggle="tab" href="#' . $key . '">' . $language->name . '</a></li>';
}
}
?>
</ul>
<div class="tab-content">
<?php
foreach ($languages as $key => $language) {
if ($language->default != 1) {
echo '<div id="' . $key . '" class="tab-pane fade">';
echo $form->field($model, 'title_' . $language->url)->textInput(['maxlength' => true]);
$description = 'description_' . $language->url;
$model->$description = OutData::showTXT($model->$description);
echo $form->field($model, 'description_' . $language->url)->textarea(['rows' => 6]);
echo $form->field($model, 'url_' . $language->url)->textInput(['maxlength' => true]);
echo "<script>
CKEDITOR.replace( 'News[description_$language->url]' );
</script>";
} else {
echo '<div id="' . $key . '" class="tab-pane fade in active">';
echo $form->field($model, 'title')->textInput(['maxlength' => true]);
$model->description = OutData::showTXT($model->description);
echo $form->field($model, 'description')->textarea(['rows' => 6]);
echo $form->field($model, 'url')->textInput(['maxlength' => true]);
echo "<script>
CKEDITOR.replace( 'News[description]' );
</script>";
}
echo '</div>';
}
?>
<div class="row">
<div class="col-sm-12">
<?= $form->field($model, 'link')->textInput() ?>
</div>
</div>
</div>
<hr>
<h3><strong>Размер: 220w : 100h</strong></h3>
<hr>
<?php
$newsImages = array();
$count = 0;
for ($i = 1; $i < Yii::$app->params['numberNewsPhotos'] + 1; $i++) {
if ($model->hasImg($i)) {
$count++;
$newsImages[] = Html::img($model->getImg($i), ['class'=>'file-preview-image', 'alt'=>$model->title, 'title'=>$model->title]).
'<span class="glyphicons glyphicons-bin"></span>';
}
}
?>
<?=
$form->field($model, 'imageFiles[]')->widget(FileInput::classname(), [
'options' => [
'multiple' => true,
'disabled' => ((Yii::$app->params['numberBrandsPhotos'] - $count) == 0) ? true : false,
],
'pluginOptions' => [
'initialPreview' => $newsImages,
'allowedFileExtensions'=> Yii::$app->params['allowedNewsFileExtensions'],
'maxFileCount' => Yii::$app->params['numberBrandsPhotos'] - $count,
],
]);
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'sort')->textInput() ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'active')->dropDownList(['0' => Yii::t('app', 'Не'), '1' => Yii::t('app', 'Да')]) ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'app.Create') : Yii::t('app', 'app.Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<script>
$('#w0').on('beforeValidate', function (event, messages, deferreds) {
for (var instanceName in CKEDITOR.instances) {
CKEDITOR.instances[instanceName].updateElement();
}
return true;
});
function deleteBrandsImage(brands_id, key) {
var r = confirm("Сигурни ли сте че искате да изтриете изображението");
if (r == true) {
$.ajax({
url: '/storeadmin/brands/deleteimage',
method: "POST",
data: {
brands_id: brands_id,
key: key,
},
success: function (data) {
location.reload();
}
});
}
}
</script>

Yii2 dynamic form constraint violation on unique index in database table

I am creating a form with dynamic fields in which 2 fields have unique indexes in the database table. 1st 'Landline' and 2nd 'Address'. If there is no duplicate value in any of the dynamically added fields then the form submits without any error, but if I enter Address or Landline which matches any previously added field then shows constraint violation instead of showing error message even when I have already added rules in the model.
For example: I have 1 record with address = 'address', landline = 123 and I create a new record with the address = 'address' or landline = 123 then there is no error but when I submit it shows:
Exception (Integrity constraint violation) 'yii\db\IntegrityException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '15-address' for key 'unique_doctors_id__address'
If I remove the indexes from the table then data saves successfully.
I have tried to submit this normally and via ajax but same problem every time, my main interest is submitting the form via ajax.
I already have googled for this issue, but couldn't find any solution, please help.
model/DoctorClinics
class DoctorClinics extends \yii\db\ActiveRecord
{
public function rules()
{
return [
[['name', 'incharge', 'landline', 'address'], 'required', 'message' => "'{attribute}' can not be empty."],
[['name', 'incharge', 'address', 'landline', 'landmark'], 'string', 'max' => 255],
['status', 'required', 'message' => "'{attribute}' can not be unselected"],
['status', 'in', 'range' => Common::get_array('range_active_inactive'), 'message' => "'{attribute}' has an invalid value"],
['status', 'string', 'max' => 1],
[['doctors_id', 'landline'], 'unique', 'targetAttribute' => ['doctors_id', 'landline'], 'message' => 'The combination of Doctors and Landline has already been taken.'],
[['doctors_id', 'address'], 'unique', 'targetAttribute' => ['doctors_id', 'address'], 'message' => 'The combination of Doctors ID and Address has already been taken.'],
[['token'], 'string', 'max' => 50],
[['token'], 'unique'],
[['doctors_id'], 'exist', 'skipOnError' => true, 'targetClass' => Doctors::className(), 'targetAttribute' => ['doctors_id' => 'id']],
[['doctors_id', 'created_at', 'updated_at'], 'integer'],
];
}
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels))
{
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post))
{
foreach ($post as $i => $item)
{
if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']]))
{
$models[] = $multipleModels[$item['id']];
}
else
{
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
}
}
controller
class DoctorsController extends Controller
{
...
public function actionCreate()
{
# models
$modelDoctors = new Doctors();
$modelDoctorClinics = [new DoctorClinics];
# scenario
$modelDoctors->scenario = Doctors::SCENARIO_CREATE;
$transaction = Yii::$app->db->beginTransaction();
# checking post method
if(($arrayPost = \Yii::$app->request->post()) != null)
{
$modelDoctorClinics = DoctorClinics::createMultiple(DoctorClinics::classname());
// $modelDoctorClinics->scenario = Doctors::SCENARIO_CREATE;
DoctorClinics::loadMultiple($modelDoctorClinics, Yii::$app->request->post());
# loading posted data to model
$modelDoctors->load($arrayPost);
# setting data
$modelDoctors->token = Common::generate_token();
$modelDoctors->added_by = \Yii::$app->user->identity->id;
$modelDoctors->auth_key = \Yii::$app->security->generateRandomString();
$modelDoctors->password_hash = \Yii::$app->security->generatePasswordHash(Common::DEFAULT_PASSWORD);
$modelDoctors->created_at = time();
# validate all models
$valid = $modelDoctors->validate();
$valid = DoctorClinics::validateMultiple($modelDoctorClinics) && $valid;
if($valid)
{
try
{
if ($flag = $modelDoctors->save(false))
{
foreach ($modelDoctorClinics as $modelDoctorClinic)
{
$modelDoctorClinic->token = Common::generate_token();
$modelDoctorClinic->doctors_id = $modelDoctors->id;
$flag = $modelDoctorClinic->save(false) && $flag;
if(!$flag)
{
$transaction->rollBack();
break;
}
}
}
if ($flag)
{
$transaction->commit();
Yii::$app->session->setFlash('success', 'Doctor\'s details saved successfully');
# setting response format
\Yii::$app->response->format = Response::FORMAT_JSON;
return true;
}
}
catch (Exception $e)
{
$transaction->rollBack();
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelDoctorClinics),
ActiveForm::validate($modelDoctors)
);
}
}
else
{
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelDoctorClinics),
ActiveForm::validate($modelDoctors)
);
}
}
else
{
# returning data
return $this->render('create', [
'model_doctors' => $modelDoctors,
'model_doctor_clinics' => (empty($modelDoctorClinics)) ? [new DoctorClinics] : $modelDoctorClinics
]);
}
}
public function actionValidations($scenario)
{
# fetching posted data
$arrayPost = \Yii::$app->request->post();
# models
if(empty($scenario) || !in_array($scenario, [Doctors::SCENARIO_CREATE, Doctors::SCENARIO_UPDATE]))
{
$modelDoctors = new Doctors(['scenario' => Doctors::SCENARIO_CREATE]);
}
else
{
if($scenario == Doctors::SCENARIO_UPDATE)
{
$modelDoctors = Doctors::find()
->where(['token' => $arrayPost['Doctors']['token']])
->one();
}
else
{
$modelDoctors = new Doctors();
}
# scenario
$modelDoctors->scenario = $scenario;
}
if(!empty($arrayPost) && \Yii::$app->request->isAjax)
{
# setting response format
\Yii::$app->response->format = Response::FORMAT_JSON;
# loading posted data to model
$modelDoctors->load($arrayPost);
return ActiveForm::validate($modelDoctors);
}
}
...
}
form
<?php
$form = ActiveForm::begin([
"enableAjaxValidation" => true,
"validateOnSubmit" => true,
'validationUrl' => \Yii::$app->urlManager->createUrl('doctors-validation/' . (($model_doctors->isNewRecord) ? 'create' : 'update')),
'options' => [
'id' => $model_doctors->formName(),
'class' => 'forms'
]
]);
?>
<div class="boxBody">
<div class="row form-page-image">
<div class="image mb10">
<div class="col-sm-12 thumbnail">
<?php
if(!empty($model_doctors->image) && file_exists(\Yii::getAlias('#uploads') . "/{$model_doctors->image}"))
{
echo Html::img(\Yii::$app->urlManagerFrontend->createUrl('/thumbnails') . '/' . $model_doctors->image, [
'alt' => $this->title,
'class' => 'js-thumbnail'
]);
}
else
{
echo Html::img(\Yii::$app->urlManager->createUrl('/images') . '/' . Yii::getAlias('#staff-no-image'), [
'alt' => $this->title,
'class' => 'js-thumbnail'
]);
}
?>
<div class="button-section">
<?php
echo Html::a('<i class="'. Common::ICON_IMAGE .'"></i> ' . Yii::t('app', 'Picture'), [
'images/upload-avatar',
'token' => $model_doctors->token
], [
'class' => "js-popup buttons tiny " . Common::LINK_IMAGE
]);
?>
</div>
</div>
</div>
<div class="image-form">
<div class="col-sm-12">
<div class="row">
<div class="col-xs-12 col-sm-6">
<?php
echo $form
->field($model_doctors, 'first_name')
->textInput([
'autofocus' => true,
'maxlength' => true
])
?>
</div>
<div class="col-xs-12 col-sm-6">
<?php
echo $form
->field($model_doctors, 'last_name')
->textInput([
'maxlength' => true
])
?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6">
<?php
echo $form
->field($model_doctors, 'email')
->textInput([
'maxlength' => true
])
?>
</div>
<div class="col-xs-12 col-sm-6 selectBox">
<?php
echo $form
->field($model_doctors, 'status')
->dropDownList(Common::get_array('active_inactive'), [
'prompt' => '- Status -'
])
?>
</div>
</div>
</div>
</div>
</div>
<div class="section">
<h3 class="heading">Residence Details</h3>
<div class="res-details">
<div class="col-sm-9">
<?php
echo $form
->field($model_doctors, 'residence_address')
->textArea(['maxlength' => true]);
?>
</div>
<div class="col-sm-3">
<?php
echo $form
->field($model_doctors, 'residence_telephone')
->textInput(['maxlength' => true]);
?>
</div>
<div class="col-sm-3">
<?php
echo $form
->field($model_doctors, 'mobile')
->textInput(['maxlength' => true])
?>
</div>
</div>
</div>
<div class="section">
<?php
DynamicFormWidget::begin([
'widgetContainer' => 'jsDoctorsClinics', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.clinics-container', // required: css class selector
'widgetItem' => '.js-clinic-clonable', // required: css class
// 'insertPosition' => 'top',
'limit' => 5, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.clinic-add-item', // css class
'deleteButton' => '.clinic-remove-item', // css class
'model' => $model_doctor_clinics[0],
'formId' => $model_doctors->formName(),
'formFields' => [
'name',
'incharge',
'address',
'landline',
'landmark',
'status'
]
]);
?>
<h3 class="heading">Clinics (max. 5)</h3>
<div class="doctor-clinics">
<div class="clinics-container">
<?php
foreach ($model_doctor_clinics as $i => $clinic)
{
?><div class="col-sm-12 clinic js-clinic-clonable clonable">
<div class="row">
<div class="col-md-8 col-sm-12">
<?php
// necessary for update action.
if(!$clinic->isNewRecord)
{
echo Html::activeHiddenInput($clinic, "[{$i}]id");
}
echo $form
->field($clinic, "[{$i}]name")
->textInput(['maxlength' => true]);
?>
</div>
<div class="col-md-4 col-sm-12">
<?php
echo $form
->field($clinic, "[{$i}]incharge")
->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-8">
<?php
echo $form
->field($clinic, "[{$i}]address")
->textArea(['maxlength' => true]);
?>
</div>
<div class="col-sm-4">
<?php
echo $form
->field($clinic, "[{$i}]landline")
->textInput(['maxlength' => true]);
?>
</div>
<div class="col-sm-4">
<?php
echo $form
->field($clinic, "[{$i}]landmark")
->textInput(['maxlength' => true])
?>
</div>
</div>
<div class="row">
<div class="col-sm-4 selectBox">
<?php
echo $form
->field($clinic, "[{$i}]status")
->dropdownList(Common::get_array("active_inactive"), [ 'prompt' => ' - Select - ' ])
?>
</div>
<div class="col-sm-4 col-sm-offset-4">
<div class="form-group">
<label class="hidden-480 control-label"> </label>
<button type="button" class="clinic-remove-item width-100 buttons <?php echo Common::LINK_CLOSE; ?>"<?php
if($clinic->isNewRecord || (!$clinic->isNewRecord && (count($model_doctor_clinics) > 1) && ($i == 0)))
{
echo ' style="display: none;"';
}
?>><i class="<?php echo Common::ICON_DELETE; ?>"></i> Delete</button>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<div class="col-sm-12 mt-auto mb-auto js-clinic-add-more">
<div class="form-group col-md-4 col-md-offset-4 mb0">
<i class="<?php echo Common::ICON_ADD; ?>"></i> Add more clinics
</div>
</div>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="boxFooter">
<?php
if(!$model_doctors->isNewRecord)
{
echo $form
->field($model_doctors, 'token', [
'options' => [ 'tag' => false ]
])->hiddenInput([
'readonly' => 'readonly'
])->label(false);
}
else
{
echo $form
->field($model_doctors, 'token', [
'options' => [ 'tag' => false ]
])->hiddenInput([
'value' => Common::generate_token()
])->label(false);
}
echo $form
->field($model_doctors, 'uploaded_files', [
'options' => [ 'tag' => false ]
])->hiddenInput([
'readonly' => 'readonly'
])->label(false);
echo $form
->field($model_doctors, 'password_hash', [
'options' => [ 'tag' => false ]
])->hiddenInput([
'readonly' => 'readonly',
'value' => Common::generate_token()
])->label(false);
echo $form
->field($model_doctors, 'auth_key', [
'options' => [ 'tag' => false ]
])->hiddenInput([
'readonly' => 'readonly',
'value' => Common::generate_token()
])->label(false);
?>
<?php
echo Html::submitButton('<i class="' . Common::ICON_SUBMIT . '"></i> ' . Yii::t('app', 'Submit'), [
'class' => 'buttons mini default pull-right'
])
?>
<?php
echo Html::button('<i class="' . Common::ICON_CLOSE . '"></i> ' . Yii::t('app', 'Cancel'), [
'class' => 'js-cancel buttons mini pull-left ' . Common::LINK_CLOSE
])
?>
</div>
<?php ActiveForm::end(); ?>
I think that $model->save(false) skip unique index validation.

Create form input using modal in yii2 is strange

I've tried for create form input using modal. When I implementation in browser is successfully. But when I testing for validation in the textfield, insted form input with using modal redirect to other page. For details, you can see this below.
When I implementation.
Testing validation redirect to other page.
Code in controller
public function actionCreate()
{
$model = new Donatur();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
return $this->redirect(['index']);
return $this->refresh();
} else {
if (Yii::$app->request->isAjax) {
return $this->renderAjax('create', ['model' => $model]);
}
else{
return $this->render('create', ['model' => $model]);
}
}
}
Code create.php in view
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use yii\db\ActiveRecord;
/* #var $this yii\web\View */
/* #var $model app\models\Donatur */
?>
<h2 align="center">Form Donatur</h2>
<?php $form = ActiveForm::begin(['layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'horizontalCssClasses' => [
'label' => 'col-sm-4',
'offset' => 'col-sm-offset-4',
'wrapper' => 'col-sm-8',
'error' => '',
'hint' => '',
'button' => 'col-sm-4'
],
],
]); ?>
<?= $form->field($model, 'kode_donatur')->textInput(['readonly' => true, 'style'=>'width:100px']) ?>
<?= $form->field($model, 'nama_donatur')->textInput(['style'=>'width:350px']) ?>
<?= $form->field($model, 'alamat')->textArea(['rows' => 3, 'style'=>'width:350px']) ?>
<?= $form->field($model, 'telepon')->textInput(['style'=>'width:300px']) ?>
<div class="form-group">
<div class="col-sm-offset-4">
<?= Html::submitButton('Simpan', ['class'=> 'btn btn-primary']) ?>
<?php
echo "&nbsp";
echo "&nbsp";
echo Html::a('Keluar', ['index'],[
'class'=>'btn btn-success',
'onclick' =>'$("#donaturModal").modal("hide");
return false;'
]);
?>
</div>
</div>
<?php ActiveForm::end();?>
Code index.php in view
<?php \yii\widgets\Pjax::begin(['timeout' => false, 'id' => 'pjax-gridview']); ?>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\bootstrap\Modal;
use yii\helpers\Url;
/* #var $this yii\web\View */
/* #var $searchModel app\models\SearchDonatur */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Data Donatur';
?>
<?php if (Yii::$app->session->hasFlash('success')): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Informasi!</h4>
<?= Yii::$app->session->getFlash('success') ?>
</div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('delete')): ?>
<div class="alert alert-success alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
<h4><i class="icon fa fa-check"></i>Informasi!</h4>
<?= Yii::$app->session->getFlash('delete') ?>
</div>
<?php endif; ?>
<div class="donatur-index">
<?php Pjax::begin(['timeout'=>false,'id'=>'pjax-gridview']); ?>
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::button('Tambah Donatur', ['value'=>Url::to('create'),'class' => 'btn btn-success','id'=>'modalButton']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'emptyCell'=>'-',
'summary'=>'',
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'kode_donatur',
'value'=>'kode_donatur',
'contentOptions'=>['style'=>'width: 200px;']
],
[
'attribute'=>'nama_donatur',
'value'=>'nama_donatur',
'contentOptions'=>['style'=>'width: 250px;']
],
[
'attribute'=>'alamat',
'value'=>'alamat',
'contentOptions'=>['style'=>'width: 350px;']
],
[
'attribute'=>'telepon',
'value'=>'telepon',
'contentOptions'=>['style'=>'width: 290px;']
],
[
'class' => \yii\grid\ActionColumn::className(),
'header' => 'Aksi',
'template' => '{update} {delete}',
'buttons' => [
'update' => function($url, $model) {
$icon = '<span class="glyphicon glyphicon-pencil"></span>';
return Html::a($icon, $url,[
'data-toggle' => "modal",
'data-target' => "#donaturModal",
]);
},
'delete' => function($url, $model) {
$icon = '<span class="glyphicon glyphicon-trash"></span>';
return Html::a($icon, $url,
[
'data-confirm' => "Apakah yakin dihapus ?",
'data-method' => 'post',
]);
},
]
],
],
]); ?>
<?php \yii\widgets\Pjax::end() ?>
<?php Pjax::end(); ?>
</div>
<?php
Modal::begin(['id' => 'donaturModal',]);
Pjax::begin(['id'=>'pjax-modal', 'timeout'=>false,
'enablePushState'=>false,
'enableReplaceState'=>false,]);
Pjax::end();
Modal::end();
?>
<?php
$this->registerJs('
$("#donaturModal").on("shown.bs.modal", function (event) {
var button = $(event.relatedTarget)
var href = button.attr("href")
$.pjax.reload("#pjax-modal", {
"timeout":false,
"url":href,
"replace":false,
});
})
');
?>
<?php
Modal::begin([
'header' => '<h1 align="center">Tambah Donatur</h1>',
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'><div>";
Modal::end()
?>
Code AppAsset.php
<?php
/**
* #link http://www.yiiframework.com/
* #copyright Copyright (c) 2008 Yii Software LLC
* #license http://www.yiiframework.com/license/
*/
namespace app\assets;
use yii\web\AssetBundle;
/**
* #author Qiang Xue <qiang.xue#gmail.com>
* #since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $js = [
'js/main.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
Code main.js in folder web/js
$(function(){
//ambil form untuk tambah data
$("#modalButton").click(function(){
$("#modal").modal('show')
.find("#modalContent")
.load($(this).attr('value'));
});
});
EnableAjaxValidation of form :
<?php $form = ActiveForm::begin([
'layout' => 'horizontal',
'enableAjaxValidation' => true,
'id' => 'create-form',
...
]);
Controller
public function actionCreate()
{
$model = new Donatur();
if ($model->load(Yii::$app->request->post())) {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if($model->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
}
else {
Yii::$app->session->setFlash('error', 'error message!');
}
return $this->redirect(['index']);
} else {
if (Yii::$app->request->isAjax) {
return $this->renderAjax('create', ['model' => $model]);
}
else{
return $this->render('create', ['model' => $model]);
}
}
}