i want to display the error with tbalert in admin page, the alert show when data is duplicate when user click delete in gridview, to check duplicate i used function beforedelete in model, but why does the alert not show in admin page ?
//code in model
public function beforeDelete()
{
$criteria = new CDbCriteria();
$criteria->condition = 'idnasabah=:idnasabah';
$criteria->params = array(':idnasabah'=>$this->idnasabah);
if (Tbpinjaman::model()->exists($criteria)) {
Yii::app()->user->setFlash(
'error',
'<strong>Ooops..!</strong> Data tidak dapat dihapus karena relasi dengan tabel pinjaman.'
);
return false;
}
else {
return parent::beforeDelete();
}
}
//code in admin page
<?php
/* #var $this TbnasabahController */
/* #var $model Tbnasabah */
$this->widget('bootstrap.widgets.TbAlert', array(
'block' => true,
'fade' => true,
'closeText' => '×', // false equals no close link
'events' => array(),
'htmlOptions' => array(),
'userComponentId' => 'user',
'alerts' => array( // configurations per alert type
// success, info, warning, error or danger
'success' => array('closeText' => '×'),
'info', // you don't need to specify full config
'warning' => array('block' => false, 'closeText' => false),
'error' => array('block' => false)
),
));
$this->widget('zii.widgets.grid.CGridView', array(
/*'type'=>'striped bordered condensed',*/
'htmlOptions'=>array('class'=>'table table-striped table-bordered table-condensed'),
'id'=>'tbregister-grid',
'filter' => $model,
'dataProvider'=>$model->search(),
'template'=>'{items}{pager}',
'columns'=>array(
array(
'header'=>'',
'headerHtmlOptions'=>array('style'=>'text-align:center;color:blue'),
'htmlOptions'=>array('style'=>'text-align:center'),
'value'=>'$this->grid->dataProvider->pagination->currentPage*$this->grid->dataProvider->pagination->pageSize + $row+1',
),
Related
I'm a newbie in Yii programming.
I'm using boostrap library on Yii via Yiistrap/Yiiwheels
I've created a relation table view
The related view is a Whgridview itself
The first (master grid) has a TbRelationColum clicking it i display the second grid (detail grid).
When I click on the row to display the sub grid, everything appears ok. When I change the sort order or the page of the sub grid disappear both grid.
I understand we should differentiate the css class of the pager and the sort of sub grid from the main grid. How to do this specifically in Yii-Way?
Is This the problem?
This is the view of the main grid:
$this->widget('yiiwheels.widgets.grid.WhGridView',array(
'id'=>'masterGrid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'type' => array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'columns'=>array(
array(
'class' => 'yiiwheels.widgets.grid.WhRelationalColumn',
//'name' => 'multiMembers.id',
'type' => 'raw',
'header' => 'Sub Items',
'url' => $this->createUrl('multiGroup/ajaxSubItems'),
'cacheData' => false,
'value' => "CHtml::tag('button',array('class'=>'btn btn-primary'),'Sub Items')",
'htmlOptions'=>array('style'=>'width:90px;'),
'cssClass' => 'showSubItems',
),
'id',
'title',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
This is the sub-grid:
echo CHtml::tag('h3',array(),'Sub Items Group #"'.$id.'"');
$this->widget('yiiwheels.widgets.grid.WhGridView', array(
'id'=>'subGrid_'.$id,
'type'=>array(TbHtml::GRID_TYPE_BORDERED, TbHtml::GRID_TYPE_STRIPED),
'dataProvider' => $gridDataProvider,
'template' => "{summary}{items}<div class=\"row-fluid\"><div class=\"pull-right\">{pager}</div></div>",
'columns' => $gridColumns,
));
This is the controller:
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new MultiGroup('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MultiGroup'])) {
$model->attributes=$_GET['MultiGroup'];
}
$this->render('admin',array(
'model'=>$model,
));
}
public function actionAjaxSubItems()
{
$id = Yii::app()->getRequest()->getParam('id');
$model = $this->loadModel($id);
if($model->numSubItems > 0) {
$this->renderPartial('_child', array('id' => $id,
'gridDataProvider' => $this->getGridDataProvider($id),
'gridColumns' => $this->getGridColumns()
), false, true);
} else {
echo 'Non ci sono Sub Items.';
}
}
public function getGridDataProvider($id) {
$sql = 'SELECT * FROM multi_member WHERE groupid = :groupid ORDER BY lastname,firstname';
$cmd = Yii::app()->db->createCommand($sql);
$cmd->bindParam(':groupid', $id, PDO::PARAM_INT);
$result = $cmd->queryAll();
$dataProvider = new CArrayDataProvider(
$result, array(
'sort' => array(
'attributes' => array('id','groupid','firstname','lastname','membersince'),
'defaultOrder' => array('lastname' => CSort::SORT_ASC, 'firstname' => CSort::SORT_ASC),
),
'pagination' => array(
'pageSize' => 2,
),
));
return $dataProvider;
}
public function getGridColumns() {
return array('id', 'lastname', 'firstname', 'membersince');
}
How can I do?
thank you ..
If the extensions you're using all extend CGgridView, then you should be able to use option 'ajaxUpdate' (link to documentation).
Try setting 'ajaxUpdate'=>false in one of the grids (or in both of them) to see whether it helps you.
Sometimes setting ajaxUpdate to false is the only way I get get some grids to behave the way I want them to...
In my view I have this code:
echo $form->select2Row($model, 'Zustelladresse', array(
'asDropDownList' => false,
'options' => array(
'placeholder' => "Zustelladresse",
'width' => '100%',
'closeOnSelect' => true,
'minimumInputLength'=>1,
'initSelection' => "js:function (element, callback) {
var selected_data = new Object;
selected_data.id = '123';
selected_data.text = 'Test';
callback(selected_data);
}",
'ajax' => array(
'url' => Yii::app()->createUrl('address/zustelladresse'),
'dataType' => 'json',
'data' => 'js:function(term,page) { if(term && term.length){ return { zustelladresse: term };} }',
'results' => 'js:function(data,page) { return {results: data}; }',
),
)));
Created html:
Why is created only label and hidden input?
YiiBooster widgets are quite tricky to debug, if anything is wrong they just don't show. If you still need the answer, I successfully displayed a select2 widget with this code:
$form->select2Row($model, 'attribute_name', array(
'data' => array('1'=>'value1,'2'=>'value2'),
'htmlOptions'=>array(
'style' => 'width:600px',
'multiple' => true,
),
'options'=>array('placeholder'=>'Please make a selection'),
));
I'd suggest you to start from this code and add up your options one by one, and see if anything breaks.
Hello I'm trying ZF2 form with an input file.
I have a form with a file input and I want to insert the randomize name into my db.
How I can return the randomized name?
thanks.
This is the simple form class:
class OrdineForm extends Formhttp://stackoverflow.com/questions/ask
public function __construct($name = null)
{
parent::__construct('ordine');
$this->setAttribute('method', 'post');
$this->addElements();
$this->addInputFilter();
}
public function addElements(){
$this->add(array(
'name' => 'pdf',
'attributes' => array(
'type' => 'text',
'disabled' =>'true',
),
'options' => array(
'label' => 'PDF',
),
));
// FILE INPUT
$file = new File('file');
$file
->setLabel('PDF attach')
->setAttributes(array(
'id' => 'file',
));
$this->add($file);
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Add',
'id' => 'submitbutton',
'class' => 'btn btn-success'
),
));
}
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
$fileInput= new FileInput('file');
$fileInput->setRequired(false);
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
array(
'target' => './public/tmpuploads/',
'randomize' => true,
"UseUploadname" => true,
)
);
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}
}
After you have validated the form in your controller you can use $form->getData();
there should be a key 'file' as that is what you named your file element. Within that a key 'tmp_name'.
This will be the randomized name.
Eg:
public function uploadfileAction()
{
//get form and filter
$form = $this->getServiceLocator()->get('SomeModule\Form\UploadFileForm');
$filter = $this->getServiceLocator()->get('SomeModule\Form\UploadFileFilter');
$form->setInputFilter($filter->getInputFilter());
if ($this->getRequest()->isPost()) {
//merge files with post
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
//set data in form
$form->setData($post);
//check is valid- form data will now contain new name
if ($form->isValid()) {
var_dump($form->getData());
}
}
}
The resulting array dump may look something like this:
array(13) {
["file"]=>
array(5) {
["name"]=>
string(14) "some-pdf.pdf"
["type"]=>
string(24) "application/pdf"
["tmp_name"]=>
string(46) "/public/tmpuploads/new-randomized-name.pdf"
["error"]=>
int(0)
["size"]=>
int(651264)
}
["submit"]=>
string(6) "Submit"
["csrf"]=>
string(32) "4df771bb2fb14e28992a408583745946"
}
You can then just do:
$formData = $form->getData();
$fileNewLocation = $formData['file']['tmp_name'];
//do what you want with $fileNewLocation
Ok, so I was having a very interesting issue with CakePHP 1.3, in that even if I used the correct information to try to log in, it wouldn't work. I've now upgraded the same app to Cakephp 2.0, and I'm having a very different issue. Basically now, regardless of what information I put in when I'm logging in, it will log in. Even if the database is empty. No idea why this is happening...
Here's my code:
View:
<div id="login">
<p>Please log in! <a id="register" href="register" alt="Register">Register</a></p>
<hr class="login"/>
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo "<hr class=\"login\"/>";
echo $this->Form->end('Login');
echo $this->Session->flash('flash_registration');
echo "<pre>"; print_r($this->request->data); echo "</pre>";
echo $this->Html->link('Log-Out', 'logout');
?>
</div>
Model:
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
var $name = 'User';
var $validate = array(
'name' => array(
'custom_rule' => array(
'rule' => '/^[A-Za-z\s]*$/i',
'message' => 'Please enter an acceptable name'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
),
'dob' => array(
'rule' => array('date', 'ymd'),
'message' => 'Enter a valid date',
),
'phone' => array(
'numbers' => array(
'rule' => 'numeric',
'message' => 'Numbers only, no dashes or spaces'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
),
'username' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Letters and numbers only'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
),
'e-mail' => array(
'email' => array(
'rule' => 'email',
'message' => 'Please enter a valid e-mail address'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
),
'password_enter' => array(
'length' => array(
'rule' => array('between', 8, 16),
'message' => 'Password must be between 8 and 16 characters'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
),
'password_confirm' => array(
'identicalFieldValues' => array(
'rule' => array('identicalFieldValues', 'password_enter'),
'message' => 'Passwords do not match'
),
'length' => array(
'rule' => array('between', 8, 16),
'message' => 'Password must be between 8 and 16 characters'
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field is required'
)
)
);
function identicalFieldValues( $field=array(), $compare_field=null ){
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
return TRUE;
}
}
}
function beforeValidate(){
$this->data['User']['dob'] = $this->data['User']['dob'];
return true;
}
function beforeSave(){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password_enter']);
$this->data['User']['activated'] = FALSE;
return TRUE;
}
}
?>
Controller:
<?php
class UsersController extends AppController {
var $name = 'Users';
var $uses = array("User");
var $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
)
);
var $helpers = array('Form', 'Session', 'Html');
function beforeFilter(){
// Basic setup
$this->Auth->authenticate = array('Form');
$this->Auth->allow('register', 'activate');
}
function index() {
}
function login() {
$this->Auth->login($this->request->data);
$this->set('title_for_layout', "Welcome to Sound-On.com!");
$this->layout = 'user_functions';
if ($this->Auth->user()) {
echo "Logged in!";
} else {
echo "Not logged in!";
}
}
function logout() {
$this->redirect($this->Auth->logout());
}
function register(){
$this->set('title_for_layout', "Register Here!");
$this->layout = 'user_functions';
$date = date('Y');
if (!empty($this->data)) {
$user_check = $this->User->find('first', array('conditions' => array('username' => $this->data['User']['username'])));
$email_check = $this->User->find('first', array('conditions' => array('e-mail' => $this->data['User']['e-mail'])));
if (empty($user_check)) {
if(empty($email_check)){
if ($this->User->save($this->data)) {
$uuid_string = $this->data['User']['activation_hash'];
$email = <<<EOT
<html>
<head>
<title>Welcome to Sound-On.com!</title>
</head>
<body>
<p>
<h1>Welcome to Sound-on.com!</h1>
<p>You have successfully registered! To activate your account and start sounding on, please click Here! <br/>If the link is not clickable, please copy and paste the link below into your browser address bar.</p>
http://www.sound-on.com/activate?uid=$uuid_string
<p style="">Thank you for registering!</p>
<p>Your friendly Sound-On registration robot</p>
<p>If you did not register or wish to remove your account, please click here.</p>
<p style="font-size:8pt;color:#707070">© Copyright $date Sound-on.com. All rights Reserved.</p>
</p>
</body
</html>
EOT;
$to = $this->data['User']['e-mail'];
$subject = 'Welcome to Sound-On.com!';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: registration#Sound-On.com';
if (mail($to, $subject, $email, $headers)) {
$this->redirect('/');
}
} else {
//$this->Session->setFlash('<p class="register_flash">Something went wrong. Please try again.</p>', 'flash_registration');
//$this->flash('', '/');
}
} else {
//email exists
}
} else {
//username exists
}
}
}
function activate(){
$this->set('title_for_layout', "Register Here!");
$this->layout = 'user_functions';
if (!empty($_GET)) {
$activate = $this->User->updateAll(array('activated' => 1), array('activation_hash' => $_GET['uuid']));
if ($activate) {
$this->set('message', '<p id="activation_message">Your account has been successfully activated! Please click here to proceed to login!</p>');
}
}
}
}
?>
Thanks in advance!
If you send data to the Auth->login() function it will log in with the data.
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
You need to use something like this.
public function login() {
if ($this->request->is('post')) {
if (!$this->Auth->login()) {
$this->Session->setFlash('Your username or password was incorrect.');
} else {
$this->Session->setFlash('You are now logged in.');
//redirect
}
}
}
I want to have a add more button on clicking which i can add dynamically, textboxes in a drupal form api.. can someone help on this?
Thanks in advance
Take a look at Adding dynamic form elements using AHAH. It is a good guide to learn AHAH with Drupal's form API.
EDIT: For an example, install the Examples for Developers module, it has an AHAH example you can use to help you learn.
Here is an example how to solve this with Ajax in Drupal 7(If anyone want I can convert it also to Drupal 6 using AHAH(name before it became Ajax)).
<?php
function text_boxes_form($form, &$form_state)
{
$number = 0;
$addTextbox = false;
$form['text_lists'] = array
(
'#tree' => true,
'#theme' => 'my_list_theme',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
if (array_key_exists('triggering_element', $form_state) &&
array_key_exists('#name', $form_state['triggering_element']) &&
$form_state['triggering_element']['#name'] == 'Add'
) {
$addTextbox = true;
}
if (array_key_exists('values', $form_state) && array_key_exists('text_lists', $form_state['values']))
{
foreach ($form_state['values']['text_lists'] as $element) {
$form['text_lists'][$number]['text'] = array(
'#type' => 'textfield',
);
$number++;
}
}
if ($addTextbox) {
$form['text_lists'][$number]['text'] = array(
'#type' => 'textfield',
);
}
$form['add_button'] = array(
'#type' => 'button',
'#name' => 'Add',
'#ajax' => array(
'callback' => 'ajax_add_textbox_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
'#value' => t('Add'),
);
return $form;
}
function ajax_add_textbox_callback($form, $form_state)
{
return $form['text_lists'];
}
function text_boxes_menu()
{
$items = array();
$items['text_boxes'] = array(
'title' => 'Text Boxes',
'description' => 'Text Boxes',
'page callback' => 'drupal_get_form',
'page arguments' => array('text_boxes_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}