I can´t concatenate a label with string.
->add('originador', EntityType::class, array(
'label' => "app.label.x_originador".'*',
'class' => 'AppBundle:Usuario',
'em' => $options['entityManager'],
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u');
},
'placeholder' => '',
'required' => false,
))
In the part of 'label' => "app.label.x_originador".'*',
I need that the result be Originador*,because the label is for required value.
The result that I recieve is app.label.x_originador*
Please, help me to get
Originador* as result.
You can pass the translator service to your form type and translate then concatenate like this:
class MyFormType extends AbstractType
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('originador', EntityType::class, array(
'label' => $this->translator->trans('app.label.x_originador',[], 'domain').'*',
'class' => 'AppBundle:Usuario',
'em' => $options['entityManager'],
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u');
},
'placeholder' => '',
'required' => false,
));
}
}
juste replace "domain" with your translation domain.
EDIT: but yeah, the best solution is probably #ccKep's one
Related
I managed to combine two columns. But the searching column is missing. How to add searching column?
Here is the code :
echo GridView::widget([
'dataProvider' => $dataProviderAcad,
'filterModel' => $searchModelAcad,
'columns' => [
'prog_and_remark_combined' => [
'format' => 'raw',
'label' => "Programme Name",
'value' => function ($data) {
return nl2br(
$data->NAME_PROG_ENG .
"\r\nPreviously known as: " .
$data->REMARKS
);
}
],
In you Class for AcadSearch add a pubblic var for prog_and_remark_combined
class YourModelAcadSearch extends YourModelAcad
{
public $prog_and_remark_combined;
/**
* #inheritdoc
*/
public function rules()
{
return [
....
I have the following code:
use Symfony\Component\Validator\Constraints\Positive;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('x', IntegerType::class, [
'mapped' => false,
'required' => false,
'constraints' => [new Positive()]
])
}
The twig form is as follows:
{{ form_widget(form.x, { 'attr': {'class': 'form-control'} }) }}
However, the rendered form (HTML) still allows users to input values with a minus sign.
How do I change that, so the rendered form forbids minus sign and stops at 1 on the arrow input?
You will have to add the HTML5 min attribute for that, which you can add at the definition of your form field:
use Symfony\Component\Validator\Constraints\Positive;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('x', IntegerType::class, [
'mapped' => false,
'required' => false,
'constraints' => [new Positive()],
'attr' => [
'min' => 1
]
])
}
I am new to Yii-2.
I have a grid-view in my index page which some entries are displaying.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'meter_id',
[
'label' => 'Meter MSN',
'value' => function ($d) {
return $d->meter->meter_msn;
},
// 'filter' => Html::activeDropDownList($searchModel, 'meter_id', \app\models\Meters::toArrayList(), ['prompt' => "All Meters", 'class' => 'form-control']),
],
'imsi',
'telecom',
'status',
[
'label' => 'Created By',
'value' => function ($data) {
if (is_object($data))
return $data->created->name;
return ' - ';
},
//'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Created By", 'class' => 'form-control']),
],
'comments',
'historic',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Now I want to add a search-box against Meter MSN. In above code the filter is hidden so it was working but I don't want to add a drop-down instead I want a search box.
Below is my search class
public function search($params)
{
$query = MetersInventoryStore::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'meter_id' => $this->meter_id,
'created_by' => $this->created_by,
'updated_by' => $this->updated_by,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'store_id' => $this->store_id,
'meter_serial'=>$this->meter_serial,
// 'historic' => $this->historic,
'status'=>'SIM Installed',
])
// ->orFilterWhere(['status'=>'Communication Failed'])
;
// $query->andFilterWhere(['like', 'meter_serial', $this->meter_serial])
// ->andFilterWhere(['like','meter_id',$this->meter_id]);
$query->orderBy(['id' => SORT_DESC]);
return $dataProvider;
}
How can I place a search-box in it? As the simple search class will set up the search functionality by default. But my MSN value is coming from a function so I have no idea how can I place a search-box.
Any help would be highly appreciated.
for add filter field in a calculated column you should add a pubblic var in
in your search model
public function search($params)
{
public $your_column;
// declare as safe
public function rules()
{
return [
...
[[ 'your_column', ], 'safe'],
];
}
$query = MetersInventoryStore::find();
and then refer to your_column in grid_view
...
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'meter_id',
[
'attribute' => 'your_column',
'label' => 'Meter MSN',
'value' => function ($d) {
return $d->meter->meter_msn;
},
],
And last your searchModel you must expand your filter condition for manage properly your calculated column based on the filter value you passed.
You can find some sample in this tutorial http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/
i make a simple CRUD in yii2 backend from table name "Guru",i already make models with the same table and make the CRUD but when i try to Create new data from the Create function,the data doesnt saved in database at all,i already make the code similar with the frontend and change the namespace but it didnt work at all in backend.
here is my backend "Guru" models code
<?php
namespace backend\models;
use Yii;
class Guru extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'guru';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['nip', 'nama_guru', 'ttl', 'jenis_kelamin', 'alamat', 'telp', 'agama', 'jabatan', 'user', 'pass', 'role', 'tgl_create', 'update_create', 'mapel'], 'required'],
[['jenis_kelamin'], 'string'],
[['mapel'], 'integer'],
[['nip', 'telp', 'jabatan', 'user', 'pass'], 'string', 'max' => 20],
[['nama_guru', 'ttl'], 'string', 'max' => 30],
[['alamat'], 'string', 'max' => 50],
[['agama', 'role', 'tgl_create', 'update_create'], 'string', 'max' => 10]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
//'id_guru' => 'Id Guru',
'nip' => 'NIP',
'nama_guru' => 'Nama Guru',
'ttl' => 'Tempat Tanggal Lahir',
'jenis_kelamin' => 'Jenis Kelamin',
'alamat' => 'Alamat',
'telp' => 'No Telpon',
'agama' => 'Agama',
'jabatan' => 'Jabatan',
'user' => 'User',
'pass' => 'Pass',
'role' => 'Role',
'tgl_create' => 'Tgl Create',
'update_create' => 'Update Create',
'mapel' => 'Mata Pelajaran',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getJadwalGuru() {
return $this->hasMany(Jadwal::className(), ['id_mapel'=>'mapel']);
}
public function getJadwal() {
return $this->hasMany(Jadwal::className(), ['nip'=>'id_guru']);
}
}
and this is my Controller code
namespace backend\controllers;
use Yii;
use backend\models\Guru;
use backend\models\GuruSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
public function actionCreate()
{
$model = new Guru();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_guru]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
i really dont know why it doesnt work when the other CRUD in backend and frontend is work.i'm new to this framework,please kindly help me.
Could be a validation problem,
you have a lot of required field and if one is missing the model is not saved.
For evaluate this situation try using save(false ) false mean without model validation. in this way :
public function actionCreate()
{
$model = new Guru();
if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
return $this->redirect(['view', 'id' => $model->id_guru]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
If with the param false the model is saved check selectively (by commenting) the rule that create problem with validation.
I'm using CakePHP search plugin http://cakedc.com/downloads/view/cakephp_search_plugin and want to make an arg to search multiple fields I have the array filterArgs as follows
var $filterArgs = array(
array('name' => 'search', 'type' => 'like', 'field' => 'Interview.title_en'),
);
I want the search arg to search not only the field Interview.title_en but to search also another field I tried something like
var $filterArgs = array(
array('name' => 'search', 'type' => 'like', 'field' => array('Interview.title_en', 'Interview.Desc'));
but It doesn't work!!
Any suggestions?
In order to achieve this, you have to create a simple method in your model which builds the 'OR' conditions for searching the fields.
public $filterArgs = array(
array('name' => 'q', 'type' => 'query', 'method' => 'filterQuery'),
);
public function filterQuery($data = array()) {
if(empty($data['q'])) { // q is the name of my search field
return array();
}
$query = '%'.$data['q'].'%';
return array(
'OR' => array(
'Model.title LIKE' => $query,
'Model.description LIKE' => $query,
'Model.resources LIKE' => $query,
)
);
}