How to Make Joomla 3x Custom Toolbar Button Work - joomla

I am trying to add a couple of custom toolbar buttons to my component, and at the moment the buttons are showing alright but can't get them to work.
My main problem is how to pass the id variable from the view layout to the sub-controller to perform a task in the case update a single column in the database.
These are my code structure
THE VIEW (view.html.php)
class LoanmanagerViewLoan extends JViewLegacy
{
protected $loanDetail;
public function display($tpl = null){
//Data from loanlist Model
$model=$this->getModel('Loan');
$this->loanDetail = $model->get_loan_detail();
$this->addToolbar();
parent::display($tpl);
}
protected function addToolbar()
{
// Get the toolbar object instance
$bar = JToolbar::getInstance('toolbar');
JToolBarHelper::Title(JText::_('Loan Details'));
//TRYING TO MAKE THIS BUTTON WORK
JToolBarHelper::custom('loan.approve', 'approve.png', 'icon-save.png', 'Approve Loan', false, false);
JToolBarHelper::custom('loan.deny', 'deny.png', 'deny.png', 'Deny Loan', false, false);
}
}
VIEW LAYOUT (tmpl/default.php)
JHtml::_('behavior.formvalidator');
<form action="<?php echo JRoute::_('index.php?option=com_loanmanager&view=loan&type=softloan&id='. (int) $loan->id); ?>" method="post" name="adminForm" id="loan-form" enctype="multipart/form-data">
<input type="hidden" name="option" value="com_loanmanager" />
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
SUBCONTROLLER (controllers/loan.php)
class LoanmanagerControllerLoan extends JControllerLegacy
{
public function approve()
{
$jinput = JFactory::getApplication()->input;
$id = $input->post->get('id', 0, 'INT');
//Perform some SQL query with the $id
return parent::display();
}
}

you need to write an input with the id in the form itself.
<input type="hidden" name="id" value="<?= (int) $loan->id ?>" />
alternatively, don't get the id from post, as you have put it in the action get url of the form
$id = $input->getInt('id');

Related

Codeigniter Radio button form validation?

I want to validate three radio button inputs using codeigniter form validation rule:
HTML Page
<form action="<?php echo base_url('register')" ?> method="POST">
<input type="radio" name="cars" value="BMW">
<input type="radio" name="cars" value="Ferrari">
<input type="radio" name="cars" value="Jaguar">
<input type="submit" name="submitter">
</form>
Controller
public function register()
{
$this->form_validation->set_rules('cars', 'Cars', 'required');
if($this->form_validation->run() == TRUE)
{
i know what to do here
}
else
{
i know what to do here
}
}
How to do form validation in codeigniter for radio button in my case ?
Load library for form validation and other helpers in application/config/autoload.php
$autoload['libraries'] = array('form_validation');
$autoload['helper'] = array('form', 'url');
In your controller
public function register()
{
$this->form_validation->set_rules('cars', 'Cars', 'required');
if($this->form_validation->run() == TRUE)
{
echo "success";
}
else
{
$this->load->view('welcome_message');
}
}
In your view file use below code for printing validation errors
<?php echo validation_errors(); ?>
To show form error use form_error() function and to show all error use <?php echo validation_errors(); ?>
HTML PAGE
<form action="<?php echo base_url('register'); ?>" method="POST">
<?php echo form_error('cars'); ?>
<input type="radio" name="cars" value="BMW">
<input type="radio" name="cars" value="Ferrari">
<input type="radio" name="cars" value="Jaguar">
<input type="submit" name="submitter">
</form>
Controller
public function register(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('cars', 'Cars', 'required');
if($this->form_validation->run() == TRUE)
{
echo "i know what to do here"; //die;
}
else
{
echo "i know what to do here"; //die;
}
$this->load->view('welcome_message'); // your html view page
}
I think you are looking for in_list validation as an addition to required that has been mentioned by the others.
$this->form_validation->set_rules('cars', 'Cars', 'required|in_list[BMW,Ferrari,Jaguar]');
Also, as a self-reminder, since it took me 1 year to know about this.

how to Pass array from view to controller in Laravel?

I make a form in blade.php, Here I can select multiple checkbox, and I want to pass selected input’s value to controller in a array.But I failed, I can not send the data.
Here is code from view. The selected input’s value can be 1,2 etc;
<form method="post" action="{{action('votesubmitController#votesubmit')}}" class="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($candidate_list[$post_list->id] as $candidate_list[$post_list->id])
<li>
<input type="checkbox" name= "selected[]" value= {{
$candidate_list[$post_list->id]->id }}>
<label>
<!-- some code here -->
</label>
</li>
#endforeach
<button type="submit" id="login-button">Submit</button>
</form>
Here is route-
Route::post('/votesubmit','votesubmitController#votesubmit');
If I write return $input in controller I find –
{"_token":"TQIUxVz0LjzM84Z7TaUPk3Y7BLZPjmXUyhXhlQfp","selected":
["1","2"]}
That’s I need. I do not know how to get selected value. When I get specific route error exception happens . and says "Undefined variable: selected".
Here is my Controller’s code-
class votesubmitController extends Controller
{
public function votesubmit()
{
$input = Input::all();
// return $input;
foreach ($selected as $selected) {
echo $selected;
}
}
}
// You can try this
class votesubmitController extends Controller
{
public function votesubmit()
{
//$input = Input::all();
//$selected = $input['selected'];
$selected = Input::get('selected');
foreach ($selected as $selected)
{
echo $selected;
}
}
}
Either use
$selected = $input['selected']
Or
pass it using Ajax.

Weird behaviour of CI after a redirect()

I have a strange bug during the password recovery process.
When a user loses his pwd, the app send an email with a token inside the recovery link ( http://localhost/reset-password/f38fd00aa975b28c70f54d948d20de40 for exemple ) This token is an unique key inside the user table.
In the routes.php, i have :
$route['reset-password/(:any)'] = "/user/reset_password_form/$1";// new password form
$route['reset-password'] = "/register/reset_password"; //simple email form
then, reset_password_form generates a form with the token as hidden input :
public function reset_password_form($hash = NULL) { //create form to change password, with user validation hash inside
$user_id = $this->user_model->get_id_by_confirmation_code(strip_tags($hash));
if (isset($user_id)) {
$this->data['validation_code'] = $hash;
$this->data['title'] = $this->lang->line('user_title_password_edit', FALSE);
$this->template->load('default', 'register/reset_password_form', $this->data);
}
else{
$this->session->set_flashdata('error', $this->lang->line('user_error_reset_password', FALSE));
redirect('reset-password');
}
the view:
<?php $attributes = array('class' => '');
echo form_open('user/edit_password', $attributes) ?>
<input type="hidden" id="validate" name="validate" value="<?=$validation_code?>">
<div class="form-group">
<input type="password" id="password" name="password" placeholder="Password" class="form-control" value="<?php echo set_value('password'); ?>">
</div>
<div class="form-group">
<input type="password" id="password_confirm" name="password_confirm" placeholder="Password confirmation" class="form-control">
</div>
<button type="submit" name="submit" class="btn btn-success">Change password</button>
</form>
Finally, the user/edit_password function changes the user password with a new one.
public function edit_password() { //get new password and change it
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
$this->form_validation->set_rules('password_confirm', 'Confirm Password', 'trim|required|matches[password]');
$this->form_validation->set_rules('validate', 'Validate', 'trim|alpha_numeric|required');
if ($this->form_validation->run() === false) {
//STRANGE BUG
$URL = '/reset-password/'.$this->input->post('validate');
$this->session->set_flashdata('error', validation_errors());
redirect($URL);
}
else {
//change pssword
}
}
The bug happen when the form validation fail : i'm suposed to be redirected to the previous form ( /reset-password/hash) with a flashdata error message, but the error message dont display.
Much more weird : even if i'm on the right form ( but without error message) if i decides to click on another menu item (for exemple /home) , it immediately displays the /reset-password form ( /register/reset_password in the routes) with the error message i was supposed to get previously.
As if the full php instruction was kept in stamp and launched after whatever action.
PS : as edit_password() and reset_password_form() are in the same controller, i could have used $this->reset_password_form($hash) instead of redirect() but it has exactly the same effect !
ps2: here is the register/reset_password:
public function reset_password() {
//display forgotten password form page
$this->data['title'] = 'Forgotten password';
$this->template->load('default', 'register/reset_password', $this->data);
}
you recovery link http://localhost/reset-password/f38fd00aa975b28c70f54d948d20de40 is not finding controller. CI is looking for your token number as controller

Codeigniter loop through post array

So i have this form:
<form id="stepform" action="#" method="post">
<fieldset>
<legend>Step #1</legend>
<label>Title</label>
<input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
<input type="text" name="field[]" class="input-xlarge">
<label>Body</label>
<textarea class="input-xlarge" name="field[]"></textarea>
</fieldset>
</form>
When user clicks a button jquery dynamically appends another two exactly same fields:
count = 2;
$("#addstep").click(function(){
$('#stepform').append('<legend>Step #' + (count++) + '</legend>');
$('#stepform').append('<label>Title</label><input type="text" name="field[]" class="input-xlarge">');
$('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[]"></textarea>');
$('#addstep').scrollintoview();
return false;
});
As you can see one step has 2 fields, when user clicks on the button step increments and adds another 2 fields to that step and so on...
After that i send data to the controller via ajax request. Now i'm stuck on the actual query which should insert new row for every step. How could i accomplish this?
Btw i'm using codeigniter and it's bind queries:
$this->db->query($sql, $data);
Update
I corrected the handling of the difference between textareas and input fields.
Sidenote: The whole Controller logic belongs into a model. I just put it into the Controller here for simplification reasons.
HTML
<form id="stepform" action="#" method="post">
<fieldset>
<legend>Step #1</legend>
<label>Title</label>
<input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
<input type="text" name="field[input][]" class="input-xlarge">
<label>Body</label>
<textarea class="input-xlarge" name="field[textarea][]"></textarea>
</fieldset>
</form>
JS
count = 2;
$("#addstep").click(function(){
$('#stepform').append('<legend>Step #' + (count++) + '</legend>');
$('#stepform').append('<label>Title</label><input type="text" name="field[input][]" class="input-xlarge">');
$('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[textarea][]"></textarea>');
$('#addstep').scrollintoview();
return false;
});
PHP
class SomeController extends MY_Controller{
public function process_request()
{
$insert_data = array();
$field_data = $this->input->post('field');
for($i = 0; $i < count($field_data['input']); $i++)
{
$insert_data[] = array(
'db_col_name_input' => $field_data['input'][$i],
'db_col_name_textarea' => $field_data['textarea'][$i]
);
}
$this->db->insert_batch('my_table', $insert_data);
}
}
Old Answer:
Since you're appending square brackets to your input fields name you will get an array with the values of all fields that are having this name. So you can go trough them with a foreach loop and store all the values in an array and use CodeIgniters insert_batch() Method to insert multiple data at the same time.
class SomeController extends MY_Controller{
public function process_request()
{
$insert_data = array();
foreach($this->input->post('field') AS $field)
{
$insert_data[] = array(
'db_col_name' => $field
)
}
$this->db->insert_batch('my_table', $insert_data);
}
}

CodeIgniter Update Not Working

I have a form in a modal that I'm using to do an update on a record. The form seems to submit okay, the URL in the browser points to the right location (domain.net/cities/update_city/1) but no view shows up and the record is never updated. I've searched the internet but most tutorials seem to skim over the update or hardcode the id to update. I've only been using CI for a week.
Model Code
// Edit City
function edit_city($city_id) {
$data = array('city_name' => $this->input->post('city_name'));
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
View Code (Form is in a modal and gets city_id added by jQuery)
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/update_city/'); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" name="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/update_city/'+city_id);
$('#editCityDialog').modal('show');
});
Controller Code
// Update City
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit_city('cities', array('city_id' => $city_id),$data);
redirect('/cities');
}
}
Here is an example, you can use it to update easily (i'm bad at english, sorry).
Model:
class Cities_model extends CI_Model{
function edit($table,$where,$data){
foreach ($where as $key => $value){
$this->db->where($key,$value);
}
$this->db->update($table,$data);
}
}
Controller:
function update_city($city_id){
$this->load->model('cities_model');
if($this->input->post('submit'){
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit('cities',array('city_id' => $city_id),$data); //(table,where,data); see in model.
redirect('');
}
}
* if you go a link as "http://......update_city/2" and a function update_city($city_id), $city_id will receive as "2" and you need not to write uri_segment to get $city_id. Hope that help you
EDIT :
Controller:
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$this->cities_model->edit_city($city_id);
redirect('/cities');
}
}

Resources