Remove model name from submitted form url (GET) - php

Here's my form
<?php $form=$this->beginWidget('booster.widgets.TbActiveForm',array(
'id'=>'listing-main-form',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('site/search'),
'method'=>'get',
)); ?>
<div class="form-group" style="padding-bottom:0px;border:none">
<label class="control-label" for="selecttype">Type</label>
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => ''
)
); ?>
</div>
<div class="form-group">
<div id="resproperties">
<div class="resdv">
<?php echo $form->checkboxListGroup(
$model,
'rs',
array(
'widgetOptions' => array(
'data' =>CHtml::listData(ResourceCategory::model()->findAll(), 'id', 'res_category'),
),
'label' => ''
)
); ?>
</div>
</div>
............
............
When the form is submitted, I can read all the field's data fine. But the url appears with Model[field] for each fields and looks very ugly (see below). Is there any where I can remove the model name from there?
index.php?r=site/search&ItemModel[prp]=1&ItemModel[rs]=&ItemModel[rs][]=2&ItemModel[rs][]=3&ItemModel[rs][]=4&ItemModel[cm] ............

You can explicitly set input name.
...
'htmlOptions' => array(
'id'=>'selecttype',
'name' => 'fieldname'
)
...
Also you can override CHtml and CActiveForm classes.

In your array for each element, add
'name'=>'your_custom_name'
So...
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => '',
'name' => 'customName'
)
); ?>

Related

Form Validation in CakePHP 2

In my view I have
<?= $this->Form->create('Asc201516', array(
'url' => array(
'controller' => 'asc201516s',
'action' => 'submit_asc201516'
),
'class' => 'form-inline',
'onsubmit' => 'return check_minteacher()'
)); ?>
<div class="form-group col-md-3">
<?= $this->Form->input('bi_school_na', array(
'type' => 'text',
'onkeypress' => 'return isNumberKey(event)',
'label' => 'NA',
'placeholder' => 'NA',
'class' => 'form-control'
)); ?>
</div>
<?php
$options = array(
'label' => 'Submit',
'class' => 'btn btn-primary');
echo $this->Form->end($options);
?>
In my Controller, I have
$this->Asc201516->set($this->request->data['Asc201516']);
if ($this->Asc201516->validates()) {
echo 'it validated logic';
exit();
} else {
$this->redirect(
array(
'controller' => 'asc201516s',
'action' => 'add', $semisid
)
);
}
In my Model, I have
public $validate = array(
'bi_school_na' => array(
'Numeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'numbers only',
'allowEmpty' => false
)
)
);
When I submit the form, logically it should not get submitted and print out the error message but the form gets submitted instead and validates the model inside controller which breaks the operation in controller.
You have to check validation in your controller like
$this->Asc201516->set($this->request->data);
if($this->Asc201516->validates()){
$this->Asc201516->save($this->request->data);
}else{
$this->set("semisid",$semisid);
$this->render("Asc201516s/add");
}
You will have your ID there in variable $semisid, or you can set data in $this->request->data = $this->Asc201516->findById($semisid);

How to read only some form fields in php using yii framework

I am new at php and yii framework.can any one help me with my form.I have a update form which has 3 fields with drop down menu.how to make the from field value read only.it will be very much helpfull if any one provide me with code.here is my update form code:
Update form:
<div class="row">
<?php
$criteria=new CDbCriteria();
echo $form->dropDownListGroup(
$model,
'user_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'),
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'htmlOptions' => array('prompt'=>'Select'),
)
)
); ?>
</div>
<div class="row" id="jobTitle">
<?php
$criteria = new CDbCriteria;
$criteria->condition = "status= 'active'";
echo $form->dropDownListGroup(
$model,
'job_title_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(JobTitle::model()->findAll($criteria), 'id', 'name'),
'htmlOptions' => array('prompt'=>'Select job title'),
)
)
); ?>
</div>
<div class="row" id="file_name">
<?php echo $form->textFieldGroup(
$model,'file_name',
array(
'wrapperHtmlOptions' => array(
'class'=> 'col-sm-5',
),
)
);
?>
</div>
<div class="row" id="statustype">
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
To make a value readonly - add 'readonly'=>true to the options array of the field you want to make readonly.
For example:
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
'readonly' => true
)
);
?>
It's impossible to set select element readonly. With Yii you can set it disabled and add value into hidden field using unselectValue.
echo $form->dropDownListGroup(
// ...
array(
// ...
'widgetOptions' => array(
'htmlOptions' => array(
'disabled' => 'disabled',
'unselectValue' => $model->user_id,
),
)
)
);
Or use TbSelect2 it has readonly property.

ZF2 Nested Collection element has no value

I have created a form with nested Collection Customer(Form) -> Categories(Collection/Fieldset) -> Tags(Collection/Fieldset).
It's:
One(Customer) -> Many(Categories)
One(Catogrie) -> Many(Tags)
After bind the customer to the Form it looks like everything is working fine. The Hydrator get the object and the elements where created in Tags.
But in the View the Tag-Elements have no value...
I have checked the Hydrator for typos but everything is fine I copy/paste the index to make sure. When i var_dump the Tags-Collection the objects with value are binded.
I really dont know where the error is, thats why i dont enter some code here I think it would be to much. When you have an idea I can show you the code where you guess the error can be.
Greetings.
Tiega.
EDIT:
Okay I will try to do my best to give you readable code :)
class KontakteController extends AbstractActionController {
public function getKontaktAction()
{
$formManager = $this->serviceLocator->get('FormElementManager');
$kontaktForm = $formManager->get('KontakteManager\Form\KontakteForm');
$id = $this->params()->fromRoute('id');
$kontakt = $this->getKontakte()->getKontakt($id);
if (!$id || !$kontakt) {
return $this->redirect()->toRoute('kontakte', array(
'action' => 'addKontakt'
));
}
$kontakt->initFirmaKommunikation($this->getKommunikation());
$kontakt->initAdressen($this->getAdressen());
$kontakt->initAnsprechpartner($this->getKontakte());
$kontakt->initBankverbindungen($this->getBankverbindung());
$kontakt->initFirmaKategorien($this->getKontakteKategorie());
$kontakt->initPersonKategoiern($this->getKontakteKategorie());
$kontaktForm->bind($kontakt);
return new ViewModel(array(
'kontaktForm' => $kontaktForm,
'geloescht' => $kontakt->geloescht,
'tags' => $this->ladeTags(),
));
}
CustomerFieldset:
class KontakteForm extends Form implements InputFilterProviderInterface {
public function __construct()
{
parent::__construct('kontakt');
$this->setHydrator(new KontaktHydrator())
->setObject(new Kontakt());
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'firmaKategorien',
'options' => array(
'count' => 0,
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => false,
'target_element' => array(
'type' => 'KontakteManager\Form\KontaktKategorieFieldset'
)
)
));
}
/**
* #return array
\*/
public function getInputFilterSpecification()
{
return array();
}
The CategorieFieldset:
class KontaktKategorieFieldset extends Fieldset {
public function __construct()
{
parent::__construct('kontakteKategorie');
$this->setHydrator(new KontaktKategorieFieldsetHydrator())
->setObject(new KontaktKategorie());
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
'attributes' => array(
'class' => 'form-control',
),
));
$this->add(array(
'name' => 'bezeichnung',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'label' => 'Kategorie',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'tags',
'options' => array(
'count' => 0,
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => false,
'target_element' => array(
'type' => 'KontakteManager\Form\TagFieldset'
)
)
));
}
And the TagFielset:
class TagFieldset extends Fieldset implements InputFilterProviderInterface {
public function __construct()
{
parent::__construct('Tag');
$this->setHydrator(new TagHydrator())
->setObject(new Tag());
$this->add(array(
'name' => 'id',
'type' => 'Zend\Form\Element\Hidden',
'attributes' => array(
'class' => 'form-control',
),
));
$this->add(array(
'name' => 'mehrsprachig',
'type' => 'Zend\Form\Element\Hidden',
'attributes' => array(
'class' => 'form-control',
),
'options' => array(
'label' => 'Mehrsprachig',
),
));
$this->add(array(
'name' => 'kategorieID',
'type' => 'Zend\Form\Element\Hidden',
'attributes' => array(
'class' => 'form-control',
),
));
$this->add(array(
'name' => 'bezeichnung',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'class' => 'form-control',
'readonly' => 'readonly',
),
'options' => array(
'label' => 'Bezeichnung',
),
));
}
/**
* #return array
\*/
public function getInputFilterSpecification()
{
return array();
}
And the View code how i try to display the collections
<h5 class="text-primary"><strong>Kategorien</strong></h5>
<hr>
<?php foreach($kontaktForm->get('firmaKategorien') as $element): ?>
<div class="row">
<div class="col-lg-12">
<div class="col-lg-6">
<?php echo $this->formElement($element->get('bezeichnung')); ?>
</div>
<div class="col-lg-6">
<?php foreach($element->get('tags') as $tag): ?>
<?php echo $this->formElement($tag->get('bezeichnung')); ?>
<?php endforeach; ?>
</div>
</div>
</div>
<?php endforeach; ?>
and here an example result:
<h5 class="text-primary"><strong>Kategorien</strong></h5>
<hr>
<div class="row">
<div class="col-lg-12">
<div class="col-lg-6">
<input type="text" name="firmaKategorien[0][bezeichnung]" class="form-control" value="Druckerei">
</div>
<div class="col-lg-6">
<input type="text" name="firmaKategorien[0][tags][0][bezeichnung]" class="form-control" readonly="readonly" value="">
<input type="text" name="firmaKategorien[0][tags][1][bezeichnung]" class="form-control" readonly="readonly" value="">
</div>
</div>
See if this will make any changes
$kontaktForm->bind($kontakt);
//Add this line
$kontaktForm->setData((Array)$kontakt);
Please post back the error you get

Yii: Can not list data in Grid View

I cant list data in grid using yii framework.My controller is Sitecontroller.php,My view is list_jobseeker.php .I got the error "Fatal error: Call to a member function getData() on a non-object ".Anybody help me?
My controller code
public function actionlist_jobseeker()
{
$session_id=Yii::app()->session['user_id'];
if ($session_id == "")
{
$this->redirect( array('/employee/site/login'));
}
$user_id =$session_id;
$models = Yii::app()->db->createCommand()
->select('*')
->from('job_seeker_profile s')
->join('job_profile j','s.user_id = j.user_id')
->order('s.id')
->queryAll();
$this->render('list_jobseeker',array('models' =>$models));
}
View- list_jobseeker.php
<h1>View Jobseeker</h1>
<div class="flash-success">
</div>
<div class="form">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'rates-phase-grid',
'htmlOptions' => array('class' => 'table table-striped table-bordered table-hover'),
'dataProvider'=>new CArrayDataProvider($models),
'columns' => array(
array(
'name' => 'Name',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->name)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
),
array(
'name' => 'Email',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->email)',
'htmlOptions' => array('style'=>'width:250px;','class'=>'zzz')
),
array(
'name' => 'Password',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->password)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz')
),
array(
'name' => 'Contact No',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->contact_no)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'name' => 'Gender',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->gender)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'class' =>'CButtonColumn',
'deleteConfirmation'=>'Are you sure you want to delte this item?',
'template'=>'{update}{delete}',
'buttons' =>array('update'=>array(
'label'=>'edit',
'url'=>'Yii::app()->controller->createUrl("UpdateJob",array("id"=>$data["id"]))',
),
'delete'=>array('label'=>'delete',
'url'=>'Yii::app()->controller->createUrl("DeleteJob",array("id"=>$data["id"]))'),
)
)
),
));
?>
dataProvider should be a CDataProvider instance. You are passing in an array: $model. You could wrap this in a CArrayDataProvider:
'dataProvider' => new CArrayDataProvider($model),
A couple of other issues:
a) Your gridview expects $data to be an object and not an array. Either alter all $data->* instances to $data[*] or use CActiveDataProvider and the CActiveRecord instance for the job_seeker_profile table
b) By convention $model usually refers to a single CModel instance. Your array should therefore be named something else in plural e.g $items
I use this:
$data = ...::model()->findAll();
$dataProvider=new CArrayDataProvider('Class of your object');
$dataProvider->setData($data);
$dataProvider->keyField = "primary key of your model";
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
...
array( 'class'=>'CButtonColumn',
'template' => '{ver}',
'buttons' => array(
'ver' => array(
'url'=>'"?id=".$data["..."]',
'imageUrl'=> Yii::app()->baseUrl.'/images/view.png', //Image URL of the button.
),
)
),

Post multipart data with Zend Forms

I want to post arrays with Zend.
html form:
<form id="form" class="form-container" action="<?php echo $this->form->getAction(); ?>" method="post" enctype="multipart/form-data">
<?php foreach ($this->projects as $item): ?>
<?php echo $this->form->time_on_project; ?>
<?php echo $this->form->comment; ?>
<?php endforeach; ?>
Zend Form:
$this->addElement('text', 'time_on_project[]', array(
'label' => 'Время(формат час:минуты):',
'required' => true,
'attribs' => array('class' => 'form-field', 'required' => 'required', 'placeholder' => 'Введите время в формате час:минуты'))
);
$this->addElement('textarea', 'comment[]', array(
'label' => 'Что сделано:',
'required' => false,
'attribs' => array('class' => 'form-field', 'style' => 'height: 100px')
));
After that, my inputs not displayed. How to make it right?
Thanks for help.
you could try doing:
$this->addElement('text', 'time_on_project', array(
'isArray' => true,
'value' => '237',
'label' => 'Время(формат час:минуты):',
'required' => true,
'attribs' => array('class' => 'form-field', 'required' => 'required', 'placeholder' => 'Введите время в формате час:минуты'))
'decorators' => Array(
'ViewHelper'
),
));

Categories