Preventing Form Validation Callback Issues Codeigniter - php

this is my form_validation.php
$config = array(
'buy' =>array(
array(
'field' => 'id',
'label' => 'Item',
'rules' => 'trim|required'
),
array(
'field' => 'qty',
'label' => 'Quantity',
'rules' => 'trim|greater_than[0]|callback_validate_qty'
)
)
);
My Controller to load form validation :
$this->load->library('form_validation');
Function validate_qty($qty)
$item_id = $this->input->post('id');
$total_item= $this->item_model->getTotalItem($item_id);
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return FALSE;
}
this validation is work perfectly, but someone has got throught it, he buy more than maximum stock..
How to prevent this? I don't know how he does that, but it looks like the validation didn't work for him. When I tested, it works perfectly, I don't know what's wrong with it, maybe he uses software or anything? is there any solution? Thanks

i think your code is correct.
i have one doubt on that code.
check the variable you used in if condition is correct or not ($qty). becuase you not show the full code.i don't known how that variable comes.
Also change the code like if and else condition.
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return false;
} else {
return true;
}

Assume if you have 30 stock in your hand and you want to get all 30 stocks out. But according to your if condition (if ($qty >= $total_item) {) it throws error.
Fix 1
if should be > (if ($qty > $total_item) {).
Fix 2
Typo - in your callback function you've used two underscores _. Get rid of one
callback__validate_qty
^^
Read more about codeigniter.com - Callbacks

Related

CodeIgniter Get Value From Form Field

I know this is a stupid question but after messing around, I am confused what is the right way of doing.
Below are my code :
View registration_form.php :
$input_data = array(
'name' => 'user_name',
'id' => 'user_name',
'value' => set_value('user_name'),
'maxlength' => MAX_CHARS_4_USERNAME
);
Controller register.php:
$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );
Form Validation Config :
$config['customer_creation_rules'] = array(
array(
'field' => 'user_name',
'label' => 'USERNAME',
'rules' => 'trim|required|alpha|strtolower'
)
);
Model user_model.php :
public function create_user( $role, $insert_array = array() )
{
// The form validation class doesn't allow for multiple config files, so we do it the old fashion way
$this->config->load( 'form_validation/administration/create_user/create_' . $role, TRUE );
$this->validation_rules = config_item( $role . '_creation_rules' );
$form_username = $this->config->item($role . '_creation_rules', 'field');
echo $form_username;
}
What I want to do right now is to check the username that the user input and auto add a number to the input username before inserting into database.
Initially I thought of getting the inputted username from the Form Validation, after messing around, I can't get the value no matter what I have tried.
Am I doing it wrongly? Do I just get from $_POST instead?
Hope you guys can help me out on this. Thanks in advance!
This shows the full list (waaaay at the bottom): http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I don't see that one there, but it says you can use any PHP function that requires only one parameter so that should work, but you'll have to look deeper into the docs to figure out why it isn't.
I don't think you can set rules for a form, you have to set them per variable, as the docs show:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');

form_validation config file in codeigniter (and callbacks)

After abusing Google for over an hour, I've found no answers to this question :
When using form_validation.php to your validation rules, Is it possible to pass a dynamic value to the callbacks?
array(
'field' => 'passwordrepeat',
'label' => 'סיסמא חוזרת',
'rules' => 'passwordsMatch['myDynamicValue']'
),
This clearly doesn't work as it passes "myDynamicValue" as a string.
Now, because this config file is loaded so early, this only available resource in it is CI_Loader, which doesn't help much, So I can't access the input class.
So my question:
Can a dynamic value pass to the config file, Or should that rule be written inline in the controller itself?
$this->form_validation->set_rules('password1', 'Password', 'trim|required|matches[password2]');
$this->form_validation->set_rules('password2', 'Verify Password', 'trim|required');
This is what I have for setting form validation on two passwords. This is what is what comes after you set all of your rules
if ($this->form_validation->run() == FALSE)
{
//Validation failed
}
else
{
//Validation suceeded carry on
}
Here is a link to some documentation
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
The answer to this question is a bit embarrassing.
The solution to this is that you can pass other field names to your callbacks.
However, remember that what you're passing is the field and not the actual value.
To get the actual value you'll need to access it through $_POST['field'].
For example the built in Matches function
public function matches($str, $field)
{
if ( ! isset($_POST[$field]))
{
return FALSE;
}
$field = $_POST[$field];
return ($str !== $field) ? FALSE : TRUE;
}
I feel a bit silly :)
The answer to the question is little tricky but it's easy to understand.
The solution for the asked question is here.
Create a form_validation.php file under the application/config/folder.
and past the code as bellow.
$config = array(
array(
'field' => 'passwordrepeat',
'label' => 'סיסמא חוזרת',
'rules' => 'passwordsMatch['myDynamicValue']'
),
);
the rules will loaded automatically available to the $this->form_validation->run(); this method.
Also you can append more array, I mean more rules for the different controller.
Hope this will help you.

SugarCRM REST API set_relationship between Quote and ProductBundles

I am using SugarCRM Pro 6.5.5
I need to create a Quote and add Products to it with the REST API. All bundles > Prof the set_entry's work, and the set_relationship for ProductBoducts works fine. But, set_relationship for Quotes > ProductBundles does not work.
Here is my input for ProductBundles > Products: this works fine
{
"session":"5qklti658f0ooou135vt8fkbi4",
"module":"ProductBundles",
"module_id":"50b71673-b555-9d68-04c9-508ef9582f47",
"link_field_name":"products",
"related_ids":[
"a9615ab1-cd89-1549-f9b8-508f00c6fa84"
]
}
Here is my input for Quotes > ProductBundles: this does not work
{
"session":"jqodi1pu8u2l8basca1hhcbt27",
"module":"Quotes",
"module_id":"bc01a88a-35c9-25ed-dfac-508ef206a264",
"link_field_name":"product_bundles",
"related_ids":[
"50b71673-b555-9d68-04c9-508ef9582f47"
]
}
BUT it still returns:
{
"created":1,
"failed":0,
"deleted":0
}
But, no record is created in the product_bundle_quote table.
I have dug into the Sugar code a little, and found something interesting.
In service/core/SoapHelperWebService.php on line 735, is this:
$mod->$link_field_name->add($related_ids, $name_value_pair);
Which calls the add method in file data/Relationships/M2MRelationship.php on line 118. Interestingly, $lhsLinkName is NULL, which causes the method to return false. Here is a snippet:
public function add($lhs, $rhs, $additionalFields = array())
{
$lhsLinkName = $this->lhsLink;
$rhsLinkName = $this->rhsLink;
if (empty($lhs->$lhsLinkName) && !$lhs->load_relationship($lhsLinkName))
{
$lhsClass = get_class($lhs);
$GLOBALS['log']->fatal("could not load LHS $lhsLinkName in $lhsClass");
return false;
}
if (empty($rhs->$rhsLinkName) && !$rhs->load_relationship($rhsLinkName))
{
$rhsClass = get_class($rhs);
$GLOBALS['log']->fatal("could not load RHS $rhsLinkName in $rhsClass");
return false;
}
It returns FALSE in the first if() block, which means the record is never created.
Also, this shows up in my log:
[2139][1][FATAL] could not load LHS in ProductBundle
So yeah, I'm stuck here. I tried searching for everything I could, including the error, but I haven't found anything helpful.
This is a Sugar bug, which will be fixed in the 6.7 release. In the meantime, check out this forums post for the code fix.
http://forums.sugarcrm.com/f6/create-quote-line-items-web-service-api-83183/
This does turn out to be a Sugar Bug, but not the one in the other answer. It's actually related to this bug: Bug 32064. They were able to provide me with a custom module that I could upload and activate to address the issue before release 6.7 when this is slated to be addressed officially. You'll probably want to contact SugarCRM directly for this workaround, depending on your time frame.
I was having the same issue and in order to resolve that i added the below mentioned code in product bundles vardef and it's start working fine.
'quotes' =>
array (
'name' => 'quotes',
'type' => 'link',
'vname'=>'LBL_PRODUCT_BUNDLES',
'relationship' => 'product_bundle_quote',
'source'=>'non-db',
),
Thanks!
You have to link the quote to products too.
make sure in your quote vardef there's the following:
'products' =>
array (
'name' => 'products',
'type' => 'link',
'relationship' => 'quote_products',
'vname' => 'LBL_PRODUCTS',
'source'=>'non-db',
),
and in your webservice, set the relationship between quote and products
{
"session":$session_id,
"module":"Quotes",
"module_id":$quote_id,
"link_field_name":"products",
"related_ids":[
$product_id
]
}
It works for me.

php sprintf not working as expected

$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
In doing so, the edit part is not being displayed.Am i doing anything wrong.
I also tried using the placeholders
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
but still no results. I also noticed that when i remove the class and id attributes in the earlier version, then it works fine.
Can you please give me a satisfactory explanation of this and tell me where am i doing wrong.
EDIT:
Im using this inside Wordpress for creating custom table using WP_List_Table class
function column_course_name($item ) {
//Build row actions
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','book_management','do_process','delete',$item['course_id']),
);
//Return the title contents
return sprintf('%1$s%3$s',
/*$1%s*/ strlen($item['course_name'])>0?$item['course_name']:'<span style="color:silver">(No Name)</span>',
/*$2%s*/ $item['course_id'],
/*$3%s*/ $this->row_actions($actions) //row_actions is a method in this class
);
}
update:
Well, its strange to mention but the code works when i use a single class( ie when i delete the space between the two classes for the tag) .
Any thoughts?
Dipesh, maybe you have errors in the code around this snippet.
Try to check your code in isolation. I copied your code to the separate .php script with little set-up and checked $actions array with print_r, like this:
edit_array.php
<?php
$item = array();
$item['course_id'] = 1;
$actions = array(
'EDIT' => sprintf('Edit',
'abf_cm',
'edit_course',
$item['course_id'],
'thickbox edit-box',
'edit_'.$item['course_id']
),
'DELETE' => sprintf('Delete','course_management','do_process','delete',$item['course_id']),
);
print_r($actions);
I ran this script from console and got the following results:
$ php edit_array.php
Array
(
[EDIT] => Edit
[DELETE] => Delete
)
Generated link for $actions['EDIT'] is HTML valid, so one can safely conclude that your code itself is working fine, and error lies somewhere else.

CodeIgniter - form_checkbox and a required check

I have a little checkbox on a signup form im creating which 'must' be checked before the user is allowed to continue ... Im finding it difficult to figure out how to do this with the form_validation functions as well, basically the 'agree to terms and conditions' checkbox MUST be checked in order for the user to continue, if not, an error message displayed, the code i have at the moment is below, if someone could give me a bit of a helping hand that would be great.
Ok, in my view i have the following
$agreeCheck = array( 'name' => 'agreeCheck', 'id' => 'agreeCheck', 'value' => 'agree', 'checked' => set_checkbox('agreeCheck', 'agree', FALSE));
<?php echo form_checkbox($agreeCheck); ?>
and then in my controller i have the following
$this->form_validation->set_rules('agreeCheck', 'Agree to the Terms and Conditions', 'required');
At the moment, it only remembers the value that was clicked if there is a submission, except if its not checked, it doesnt return anything.
Try this out:
$this->form_validation->set_rules('agreeCheck', 'Agree to the Terms and Conditions', 'required|isset');
Try this out:
$this->form_validation->set_rules('agreeCheck', '...', 'callback_terms_check');
And then set up this method in the controller:
function terms_check() {
if (isset($_POST['agreeCheck'])) return true;
$this->form_validation->set_message('terms_check', 'THIS IS SOOOOO REQUIRED, DUDE!');
return false;
}

Categories