Reputation: 1319
I have a submit form with one field called case_no
, when I enter in the case_no
it should then populate the table with that specific information that matched the case_no
.
Controller:
public function actionSearch()
{
$model = new LegalCase();
$category = new Category();
/*$categories = Category::findAll();*/
if ($model->load(Yii::$app->request->post())) {
Yii::$app->session->setFlash('searchFormSubmitted');
$case = $model->findByCaseNo($case_no);
return $this->render('search', [
'model' => $model,
'dataProvider' => $model->findAll(),
'results' => $case
]);
return $this->refresh();
} else {
return $this->render('search', [
'model' => $model,
]);
}
}
Model:
class LegalCase extends Model
{
public $id;
public $case_no;
public $name;
public $judgement_date;
public $year;
public $neutral_citation;
private static $cases = [
'100' => [
'id' => '1',
'case_no' => '3',
'name' => 'first case'
],
'101' => [
'id' => '1',
'case_no' => '125',
'name' => 'second case'
],
];
/**
* @inheritdoc
*/
public static function findAll()
{
//$query = new Query();
$provider = new ArrayDataProvider([
'allModels' => self::$cases,
'sort' => [
'attributes' => ['id', 'case_no', 'name'],
],
'pagination' => [
'pageSize' => 10,
],
]);
return $provider->getModels();
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return isset(self::$cases[$id]) ? new static(self::$cases[$id]) : null;
}
/**
* Finds case by name
*
* @param string $name
* @return static|null
*/
/**
* Finds case by number
*
* @param string $case_no
* @return static|null
*/
public static function findByCaseNo($case_no)
{
foreach (self::$cases as $case) {
if (strcasecmp($case['case_no'], $case_no) === 0) {
return new static($case);
}
}
return null;
}
View:
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\widgets\ListView;
use app\models\LegalCase;
use backend\models\Standard;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LegalCase */
$this->title = 'Search';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<?php $form = ActiveForm::begin([
'id' => 'search-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-3\">{input} </div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<?= $form->field($model, 'case_no') ?>
<?= $form->field($model, 'name') ?>
<?php /*= Html::activeDropDownList($model, 's_id',
ArrayHelper::map(Standard::find()->all(), 's_id', 'name'))*/ ?>
<?php
?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary', 'name' => 'search-button']) ?>
</div>
</div>
<?php if (Yii::$app->session->hasFlash('searchFormSubmitted')) { ?>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>case_no</th>
<th>name</th>
</tr>
</table>
<?php //$posts = $results->getModels();
foreach($results as $case){
echo '<tr>';
//print_r($case);
echo $case;
//echo $case->id;
/*echo '<td>' . $post['id'] . '</td>';
echo '<td>' . $post['case_no'] . '</td>';
echo '<td>' . $post['name'] . '</td>';*/
echo '</tr>';
} ?>
Any idea why this is not working?
Upvotes: 0
Views: 119
Reputation: 711
You need to do this:
$case = $model->findByCaseNo($model->case_no);
But your attribute $case_no will be empty after the form submit. This happened because attribute is not safe. Function load() set only safe model attributes. If you want to load model attributes after submit you need to add validation to the LegalCase model. For example:
public function rules()
{
return [
['case_no', 'trim'],
];
}
Upvotes: 1
Reputation: 926
Use $_POST['LegalCase']['case_no']
instead of $case_no
.
EDIT: note: I'm using an older version of Yii so things might have changed.
Upvotes: 1