[below translated from translate.google.com]
I'm looking for the syntax of how to make a button disabled in CakePHP and I can not get a result; My application need to first save a field for a button to finish the whole process after another button. The first button is a submit and redirects to the same page. The second button performs a function of the controller and go to the next process. I want to prevent the user to go to the next procedure without saving the first; I already have a variable that defines whether it is safe or not, just do not know how to make the Finish button is disabled;
Button code:
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array('controller' => 'Questoes','action' => 'limparSession'),
array('role' => 'button', 'class' => 'btn btn-success', 'escape' => false)
);
Add the disabled class to your button:
<?php
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array(
'controller' => 'Questoes',
'action' => 'limparSession'
),
array(
'role' => 'button',
'class' => 'btn btn-success disabled',
'escape' => false
)
);
?>
This is a bootstrap feature related to the given class.
If you want to do it without bootstrap:
<?php
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok')) . " Finalizar",
array(
'controller' => 'Questoes',
'action' => 'limparSession'
),
array(
'role' => 'button',
'class' => 'btn btn-success',
'disabled' => 'disabled',
'escape' => false
)
);
?>
echo $this->Form->button(
$this->Html->tag('span', '', array('class' => 'glyphicon glyphicon-ok'))." Finalizar",
array('type' => 'submit','onclick' => 'this.disabled=true;return true;',
'class' => 'btn disabled', 'escape' => false)
);
Related
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);
I'd like to add a icon <i> tag to a Cakephp link.
Here is my code :
<?= $this->Html->link($this->Html->tag('i', '', array('class' => 'fa fa-shopping-cart')).'Cart', array('controller' => 'shop', 'action' => 'cart')) ?>
This line generates :
<i class="fa fa-shopping-cart"></i>Cart
Why < is replaced by its hexa value? My charset is UTF-8.
Thanks!
Add option 'escape' set to false:
<?= $this->Html->link($this->Html->tag('i', '', array('class' => 'fa fa-shopping-cart')).'Cart', array('controller' => 'shop', 'action' => 'cart'), array('escape' => false)) ?>
Documentation page about HtmlHelper.
Html->link($this->Html->tag('i', '',['class' => 'fa fa-shopping-cart']).'Cart',['controller' => 'shop', 'action' => 'cart'], ['escape' => false]); ?>
I would like to change the custom implementation of the delete button in gridview for buttoncolumn. I want to add a custom icon and a custom css class to the delete button. When i add the option parameter it dose not work any more. So i decided to create a custom link to the delete function but it gives me a 400 error when i click on it.
Any ideas ?
Below code
array(
'header' => __('Manage'),
'class' => 'booster.widgets.TbButtonColumn',
// 'htmlOptions'=>array('style'=>'white-space: nowrap;'),
'template' => '{approve} {details} {erase}',
'htmlOptions' => array('style' => 'white-space: nowrap;'),
'buttons' => array(
'approve' => array(
'label'=>__('Approve'),
'icon'=>'pencil',
// 'options' => array('target' => '_blank'),
'url' => 'Yii::app()->createUrl(\'tours/updateadmin/\'. $data->tour_id)',
'options' => array(
'class' => 'btn btn-small btn-info',
),
),
'details' => array(
'label'=>__('View Details'),
'icon'=>'check',
'url' => 'Yii::app()->createUrl(\'tours/view/\'. $data->tour_id)',
'options' => array(
'class' => 'btn btn-small btn-info',)
// 'options' => array('target' => '_blank'),
// 'url' => 'Yii::app()->createUrl(\'tours/updateadmin/\'. $data->tour_id)',
),
'erase' => array(
'label'=>__('Delete'),
'icon'=>'trash',
'url'=>'CController::createUrl("/tours/delete", array("id"=>$data->tour_id))',
'options' => array(
'class' => 'btn btn-small btn-info',)
),
),
),
What's your 400 error details? I think the problem is your link. Try to remove first / from your link:
'erase' => array(
'label'=>__('Delete'),
'url'=>'Yii::app()->createUrl("tours/delete", array("id"=>$data->tour_id))',
'options' => array(
'class' => 'btn btn-small btn-info'
)
),
),
If the problem did not resolve, you should edit your question and add error details for better helping.
Also, note that CButtonColumn#buttons does not have icon field. Here is a link that shows possible options for CButtonColumn#buttons . So you should remove icon field from all of your buttons.
I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong?
and can each button call a different method in the controller?
Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>
clicking the first button will give post data like:
array(
[btn1] => btn1value
[YourModel] => array(...)
)
Which makes it easy to do something like:
if (isset($this->request->data['btn1'])) {
// btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
// btn2 was clicked
}
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
<?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
echo $this->Form->button(
'Your Action Description Here',
array(
'type' => 'submit',
'formaction' => 'yourActionHere' //
)
);
Try this, This is easy to do.
<div class="submit">
<?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
<?php echo $this->Form->button('Cancel', array('type' => 'button'));?>
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.:
echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));
My application has contacts controller with add action which able to post contacts message to the database table comments. It works fine with its validation.
Now I want to use the add view (The contacts form) as a global form to be rendered in all pages of the application.
I know, to do that, I have to make an element contains the form (or the add view) as follows:
elements/contact.ctp
<div class="panel">
<h4><?php echo __('contact'); ?></h4>
<?php echo $form->create('Contact',array('action'=>'index', 'class' => ''));?>
<?php echo $form->input('name', array('size' => 45, 'class' => 'input-text', 'label' => array( 'text' => __('name',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('email', array('size' => 45, 'class' => 'input-text', 'label' => array('text' => __('email',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('subject', array('type' => 'select', 'options' => array(null => __('choose subject',true), 'g' => __('general', true), 'r' => __('report', true)), 'class' => 'input-text', 'label' => array('text' => __('subject', true).'<sup>*</sup>'),'error' => array('class' => 'error', 'wrap' => 'small'))); ?>
<?php echo $form->input('content', array('class' => 'input-text', 'style' => 'height: 140px', 'title' => __('message', true), 'label' => array('text' => __('message',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php //echo $fck->load('Contact.message','Mini'); ?>
<span>
<?php
App::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
$cap = new Fox_captcha(120,30,5);
$cap->image_dir = $html->url('/').'img/';
$cap->lines_amount = 13;
$cap->en_reload = $html->image('reload.png', array('alt' => __('reload', true), 'title' => __('reload the captcha', true), 'id' => 'frel', 'style' => 'vertical-align:middle'));
?>
</span>
<div>
<span class="display:inline"><?php echo $cap->make_it('HTML');?></span>
<?php echo $form->input('vcode', array('size' => 45, 'class' => 'small input-text', 'label' => array('text' => __('captcha', true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
</div>
<?php echo $form->submit(__('send',true), array('class' => 'nice medium radius white button'));?>
<?php echo $form->end();?>
<div class="alert-box warning"><?php echo __('fields label with * are required');?></div>
</div>
The problem is: When I use this form from any page (for example posts/view/22) it submits to contacts/add. I want after submit posts/view/22 is rendered with any validation message triggered.
There is no solution for this requirement without Ajax and JSON. It includes submitting the form with Ajax to the contacts controller index action with event handler component activated and then check isAjax then render JSON output retrieved by the post's view and then populates the results.