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'));
Related
[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)
);
This is my code, once it's submitted it should in theory go to localhost/site/main/login_validation.
<?php
echo form_open('main/login_validation');
$emailData = array(
'id' => 'inputEmail3',
'class' => 'form-control',
'placeholder' => 'Email',
'value' => 'email'
);
echo form_input($emailData);
$passwordData = array(
'id' => 'inputPassword3',
'class' => 'form-control',
'placeholder' => 'Password',
'value' => 'password'
);
echo form_password($passwordData);
$buttonData = array("type" => "submit", "class" => "btn btn-success btn-sm", "value" => "Login", 'name' => 'login_submit');
echo form_submit($buttonData, 'Login');
echo form_close();
?>
It sends me to http://joeobrien.kd.io/ci_site/?email=Email%40domain.com&password=password123&login_submit=Login, I assume I’ve made some simple mistake. This link might show you the webpage if it's still online.
Looking at the link you provided you seem to have a form within a form remove the outer one and it should work fine.
I am trying to pass data from a button within a gridview to a modal window. I need to pass the ID of the record in order to be able to reference it after submitting the form within the modal window.
I am struggling with this quite a bit. First I need to be able to pass the ID variable to the modal, and then upon clicking the submit button make an ajax call to create a new record within the DB.
The Gridview
if(isset($results)){
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
'id'=>'searchgrid',
'fixedHeader' => true,
'headerOffset' => 40, // 40px is the height of the main navigation at bootstrap
'type'=>'condensed',
'dataProvider'=>$results,
'responsiveTable' => true,
'template'=>"{items}",
'columns'=>array(
array('name'=>'title', 'header'=>'Name'),
array('name'=>'city', 'header'=>'City'),
array('name'=>'state', 'header'=>'State'),
array('name'=>'leads', 'header'=>'Leads', 'value'=>'Parkslist::model()->leadRange($data["leads"])'),
array('name'=>'pastbid', 'header'=>'Previous', 'value'=>'Parkslist::model()->pastBid($data["pasthighbid"])'),
array('name'=>'currentbid', 'header'=>'Current', 'value'=>'Parkslist::model()->highBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
array('name'=>'minimumbid', 'header'=>'Minimum', 'value'=>'Parkslist::model()->minimumBid($data["currenthighbid"], $data["secondhighbid"], $data["countcurrenthighbid"])'),
array('name'=>'userhighbid', 'header'=>'Your Bid'),
array('name'=>'placebid', 'header'=>'Bid', 'value'=>'CHtml::textField("bid" . $data["id"])', 'type'=>'raw'),
array('name'=>'report', 'header'=>'Report',
'value'=>function($data){
$this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Click me',
'type' => 'primary',
'htmlOptions' => array(
'data-toggle' => 'modal',
'data-target' => '#myModal',
'data-id' => '$data["id"]',
),
));
}
),
),
));
}
The Modal
<?php
$this->beginWidget('bootstrap.widgets.TbModal', array('id' => 'myModal')); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Why should this park be removed?</h4>
</div>
<form>
<div class="modal-body">
<select>
<option>Duplicate</option>
<option>Closed</option>
</select>
</div>
<div class="modal-footer">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'type' => 'primary',
'buttonType'=>'submit',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)); ?>
</div>
</form>
<?php $this->endWidget(); ?>
I was able to get this working. I would assume there might be a better solution but this seems to work.
First, inside of the button in the gridview I made the button ID = to the id of the record. Next, I created a javascript function called includeData and included the button ID.
Button Code
array('name'=>'report', 'header'=>'Report',
'value'=>function($data){
$this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Click me',
'type' => 'primary',
'htmlOptions' => array(
'id'=>$data["id"],
'data-toggle' => 'modal',
'data-target' => '#myModal',
'data-id' => '$data["id"]',
'onClick' => 'includeData(this.id);'
),
));
}
),
JS Code
<script type='text/javascript'>
function includeData(parkid){
$('#reportparkid').val(parkid);
}
</script>
The JS function just sets the value of a hidden field equal to the buttonid. I would love to see some better ways to handle this.
Thanks
i am very much new to cakephp. I have created a simple form with input controls on it as follows:
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('address', array('rows' => '3'));
echo $this->Form->input('aaa', array(
'type' => 'date',
'label' => 'select',
'before' => '--before--',
'after' => '--after--',
'between' => 'Date',
'separator' => '****',
'empty' => '--select--'
));
echo $this->Form->checkbox('subjects', array('value' => 'Java'));
?>
java
<?php
echo $this->Form->input('gen', array(
'type' => 'radio',
'options' => array('m', 'f')
));
echo $this->Form->input('file', array('type' => 'file'));
echo $this->Form->input('listbox', array('options' => array(1,2,3,4,5), 'multiple' => 'multiple'));
echo $this->Form->end('Submit');
?>
i wish to print the values entered in these components on another page. how do i do that?
i tried to do it with the help of session(which seems to be inappropriate) as follows:
public function contactus() {
if ($this->request->data!=null) {
$var=$this->request->data;
$this -> Session -> write('myvar', $this->request->data);
//$this->set($var, $this->request->data);
$this->redirect(array('action' => 'contactview'));
}
}
but it outputs array and i cant use session to store each component's value.
how do i solve this?
According to cakephp book (Form: http://book.cakephp.org/1.3/view/1384/Creating-Forms)
<?php
echo $this->Form->create(null,
array('url' => array('controller' => 'recipes', 'action' => 'add')));
?>
//Output:
<form method="post" action="/recipes/add">
Therefore , just change your create function and add the url option.
Rather than using the session, I used this->data and it was able to resolve my problem.
How do I show the messages when using the CakePHP validation? As I creating the input fields manually using input() instead using the shorthand form() helper.
e.g. Form:
<?php echo $this->Form->create('User', array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('email', array( 'label' => array('class' => 'placeholder', 'text' => 'Email address or username') )); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array( 'type' => 'password', 'label' => array('class' => 'placeholder', 'text' => 'Password') )); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
and this is my validation in the user model:
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'The email is not valid'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an email'
)
)
);
However the validation error messages don't show?
EDIT:
I tested this on my register form at /users/add/ and it works so it seems that the auto validation does not work with the login method???? How do I add validation for the login form then :/
The validation is actually stored in the model object. I'm not entirely sure off-hand how to access the errors, but I think its in $this->User->validationErrors.
Have a look at the model api for more information.
For logging in, use the auth component. If you'd rather not, then just get the user from the db and display an error using $this->Session->SetFlash() if the user doesn't authenticate.
You can show your code login function?
I think in your code that have redirect in-case validate false
If you use $this->redirect() it'll not show validate messages :)
First you check your post data is going to validate function.you can simple check this in your else condition like this :
$this->{$this->modelClass}->set($this->data);
if($this->{$this->modelClass}->validates(){
//Save data in DB
}else{
pr($this->{$this->modelClass}->validationErrors); // This will show your error message
}
You can also show error in your view.ctp file like this
$errors = '';
foreach ($this->validationErrors[$model] as $key => $validationError) {
$errors .= $this->Html->tag('li', $validationError[0]);
}
echo $this->Html->tag('ul', $errors,array('class' => 'error'));