I was wondering why my checkbox fails even after it has already been ticked. It is a checkbox for terms and conditions
Controller:
// Load the registration page
public function registration_page(){
$this->load->view('includes/main_index');
$this->load->view('recycler/forms/register');
$this->load->view('includes/footer');
}
public function accept_terms_conditions()
{
// Checkbox name is 'agree' which checks if the value of $checked
// is equal to 1
$checked = $this->input->post('agree');
return (int) $checked == 1 ? TRUE : FALSE;
}
$this->form_validation->set_rules('agree', '', 'callback_accept_terms_conditions');
if($this->form_validation->run() == FALSE)
{
// After form submission, it always goes here
$this->registration_page();
}
else{ /* Proceed to data insertion */ }
I printed out the post value of it using print_r function and it returned 1, yet the validation still fails. $this->form_validation->run() == FALSE means that if the form validation has errors, it will go back to the register view, which is the $this->register() method call.
print_r
ticked checkbox
Does anyone know why it keeps failing?
EDIT:
Added the method for loading the registration view which is the register() method
try
public function accept_terms_conditions() {
$checked = ($this->input->post('agree')) ? TRUE : FALSE;
return $checked;
}
A checkbox brings "on" as value when it is checked not 1.
public function accept_terms_conditions()
{
// returns true if the checkbox 'agree' has been checked
return ($this->input->post('agree') === 'on');
}
And your condition is wrong, you are saying that if the validation has failed then call method register I think it should be the other way round.
Related
I am trying to create this filter thing with check boxes.
I am trying to toggle the state of the check boxes to be true or false
<x-input.checkbox wire:click="$toggle(free_trial)"/>
public $free_trial = false;
public $free_forever = false;
public $pay_per_user = false;
public $progressive = false;
When I do this approach, I am not sure how to make the inverse work, so if it is unchecked It would be false. Is there a way to add something to this, so if it's clicked again it would change it to false?
<x-input.checkbox wire:click="$set('free_trial','true')"/>
Any help much appreciated :)
<x-input.checkbox wire:click="$set('free_trial',{{ $free_trial ? 'false' : 'true' }})"/>
I prefer dev this kind of login in a backend method.
<x-input.checkbox wire:click="toggleProperties('free_trial')"/>
public function toggleProperties($property)
{
if($property == 'free_trial')
$this->free_trial = !$this->free_trial;
elseif(...) //.....
}
I have two functions, one to check whether a user is logged in, and another to check if the user is an admin. I also have a User database with one column named user_lvl, which displays fine if I output all the user data.
The problem I'm having is that with the admin function it doesn't seem read anything.
Here is the two functions code:
define('USER_LEVEL_ADMIN', '1');
// check whether a user session is active, return true if yes, else return no
function isLoggedIn() {
if (isset($_SESSION['userId'])) {
return true;
}
else {
return false;
}
}
// check whether user has required user level to access admin privileges, return true if yes
function isAdmin() {
// check if a user is in a session, then check if the users user_lvl is equal to 'USER_LEVEL_ADMIN
if (isset($_SESSION['userId']) && $_SESSION['userId'] && USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl']) {
return true;
}
else { // works if you reverse true and false, else this is broke
return false;
}
}
And here is where it is being called:
<?php if (isLoggedIn() ) : ?>
<?php if (isAdmin() ) : ?>
<div>
Admin Panel
</div>
<?php endif; ?>
<div>
My Account
</div>
<?php endif; ?>
It displays 'My Account' but not 'Admin Panel'. Any help is much appreciated.
This code snippet is ment for testing and identify which function is gives this output
<?php
function isLoggedIn() {
return true;
}
function isAdmin() {
return false; // change it to true to see Admin Panel. You need to check the condition in this function.
}
if (isLoggedIn() ) :
if (isAdmin() ) :
echo '
<div>
Admin Panel
</div>';
endif;
echo '
<div>
My Account
</div>';
endif;
?>
The isAdmin() condition looks fine, You may echo out the session variable and crosscheck.
One of the 3 checks in the if statement is failing (returning false):
isset($_SESSION['userId']) basic isset check
$_SESSION['userId'] not sure what we're looking for here but this needs to result in boolean true
USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl'] authorized privilege check
All 3 need to be true for the if to succeed.
if (isset($_SESSION['userId']) && $_SESSION['userId'] && USER_LEVEL_ADMIN == $_SESSION['userId']['user_Lvl']) {
return true;
}
else { // works if you reverse true and false, else this is broke
return false;
}
I suspect the if is false due to this: define('USER_LEVEL_ADMIN', '1'); which creates the named constant USER_LEVEL_ADMIN with a STRING value of '1'. Then, in your if statement you compare it to $_SESSION['userId']['user_Lvl']. Please check the variable type of $_SESSION['userId']['user_Lvl']. You can drop this line in your code to check that:
echo gettype($_SESSION['userId']['user_Lvl']);
It of course would need to match the type of USER_LEVEL_ADMIN which is string.
I would like to know how i can get the current status of checkbox without saving the model in octbercms or laravel. Thanks. The following is what i would like to do:
public function button_pressed(){
if(checked){
//do something
}
}
To check if a checkbox is checked you basically have to check if the input exists, so using the request object:
use Illuminate\Http\Request;
public function button_pressed( Request $request ){
if( $request->has('checkbox_name') ){
// checkbox is check
} else {
// checkbox is NOT check
}
}
EDIT:
public function button_pressed(){
if( request()->has('checkbox_name') ){
// checkbox is check
} else {
// checkbox is NOT check
}
}
I'm trying to modify the below code snippet / function hook to disable registration if the user is logged in.
<?php
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $entry, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 160)
return $is_disabled;
//check submitted values to decide if registration should be stopped
if ($entry["4"] == "No" && $entry["5"] == "No") {
//disable registration
return true;
}
else{
return false;
}
}
?>
I've tried the following to no avail:
add_filter("gform_disable_registration", "disable_registration", 10, 4);
function disable_registration($is_disabled, $form, $fulfilled){
//check form id and if not the form being checked status passed in to function
if ($form["id"] != 2)
return $is_disabled;
//check user login to decide if registration should be stopped
if( ! is_user_logged_in() ) {
return true;
}
else {
return false;
}
}
Hoping I can get this to work! Thank you.
Here is an article/snippet I wrote to do this... I didn't confirm if this is still the best way to accomplish this, but it certainly is a way that works. :)
http://gravitywiz.com/skip-user-registration-for-logged-in-users/
I believe there's a setting in the Form Settings to allow you require users to be logged in. Is there a reason you can't just use that?
Im carrying out some form validation with codeigniter using a custom validation callback.
$this->form_validation->set_rules('testPost', 'test', 'callback_myTest');
The callback runs in a model and works as expected if the return value is TRUE or FALSE. However the docs also say you can return a string of your choice.
For example if I have a date which is validated, but then in the same function the format of the date is changed how would I return and retrieve this new formatted value back in my controller?
Thanks for reading and appreiate the help.
I'm not entirely sure I got what you were asking, but here's an attempt.
You could define a function within the constructor that serves as the callback, and from within that function use your model. Something like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controllername extends CI_Controller {
private $processedValue;
public function index()
{
$this->form_validation->set_rules('testpost','test','callback');
if ($this->form_validation->run()) {
//validation successful
echo $this->processedValue; //outputs the value returned by the model
} else {
//validation failed
}
}
private function callback($input)
{
$this->load->model('yourmodel');
$return = $this->yourmodel->doStuff($input);
//now you have the user's input in $input
// and the returned value in $return
//do some checks and return true/false
$this->processedValue = $return;
}
}
public function myTest($data){ // as the callback made by "callback_myTest"
// Do your stuff here
if(condition failed)
{
$this->form_validation->set_message('myTest', "Your string message");
return false;
}
else
{
return true;
}
}
Please try this one.
I looked at function _execute in file Form_validation of codeigniter. It sets var $_field_data to the result of callback gets(If the result is not boolean). There is another function "set_value". Use it with the parameter which is name of your field e.g. set_value('testPost') and see if you can get the result.
The way Tank_Auth does this in a controller is like so
$this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');
if ($this->form_validation->run()) {
// validation ok
$this->form_validation->set_value('login')
}
Using the set_value method of form_validation is undocumented however I believe this is how they get the processed value of login after it has been trimmed and cleaned.
I don't really like the idea of having to setup a new variable to store this value directly from the custom validation function.
edit: sorry, misunderstood the question. Use a custom callback, perhaps. Or use the php $_POST collection (skipping codeigniter)...apologies haven't tested, but I hope someone can build on this...
eg:
function _is_startdate_first($str)
{
$str= do something to $str;
or
$_POST['myinput'} = do something to $str;
}
================
This is how I rename my custom callbacks:
$this->form_validation->set_message('_is_startdate_first', 'The start date must be first');
.....
Separately, here's the callback function:
function _is_startdate_first($str)
{
$startdate = new DateTime($this->input->post('startdate'), new DateTimeZone($this->tank_auth->timezone()));
$enddate = new DateTime($this->input->post('enddate'), new DateTimeZone($this->tank_auth->timezone()));
if ($startdate>$enddate) {
return false;
} else {
return true;
}
}