• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New Helpers on the Wiki

#1
[eluser]Jim OHalloran[/eluser]
Hi there!

Just thought I'd let everyone know that my I've posted two of my helper modules to the CI Wiki. There's a Credit Card helper which makes some common credit card oprtations a little easier, and an Australian Date helper which has a few functions to make working with Australian formatted dates (d/m/y) a little easier.

Hope someone else finds them as useful as I do.

Jim.

#2
[eluser]Unknown[/eluser]
Thanks Jim, I'll take a look !

I just posted a new helper on the wiki, so I think it is the good place to introduce it Smile
It is a BBCode helper, it can actually :
- parse/remove bbcode
- generate an array of bbcode buttons that can be clicked to be inserted
- generate a javascript function needed to insert bbcodes into a form field
- read config/bbcode.php to get the bbcodes

Its code is mainly inspired by the smiley helper, so it works the same way. You can refer to the smiley helper guide if you don't know how to use it.

#3
[eluser]skattabrain[/eluser]
Jim, this is driving me crazy (although it could have been the beers i had at lunch), I can't figure out why your CC validation helper wont validate my data.

I'm jsut trying to validate the cc number at this point. The other form fields not included below all validate.

1 - I took your code form the wiki - http://codeigniter.com/wiki/Credit_Card_Helper/ - and saved it into a file called ccval_helper.php and placed in into my application/helper directory

2 - the form in my view looks like this ...
Code:
$form1 .= form_label('Name On Card', 'ccname');
$form1 .= form_input('ccname');
$form1 .= form_label('Card Number', 'bccnumber');
$form1 .= form_input('ccnumber', $this->validation->ccnumber);
$form1 .= form_label('Expiration Month', 'ccmon');
$form1 .= form_input('ccmon');
$form1 .= form_label('Expiration Year', 'ccyear');
$form1 .= form_input('ccyear');
$form1 .= form_label('CVV', 'cccvv');
$form1 .= form_input('cccvv');

3 - my controller ...
Code:
$rules['ccnumber'] = "trim|required|callback_card_number_valid";
$this->validation->set_rules($rules);

$fields['ccnumber'] = 'Credit Card';        
$this->validation->set_message('card_number_valid', 'Invalid Credit Card Number');
$this->validation->set_fields($fields);

This won't validate, any idea what I'm doing wrong? Thanks.

#4
[eluser]maicobreako[/eluser]
I've been scratchin' my head all day, 'cause I also can't get this to work. I've been using the user guide validation form to try out a couple of things. The only thing that works as far as cc number goes, is if the field is left blank. Otherwise, anything and everything validates. I also tried another, very similar function that didn't work either. BTW, the truncate_card function is working on output to formsuccess.php, proving the helper is available to the class. Am I just overlooking something obvious regarding ci???? Any advice would be much appreciated.

<< Oh yeah, ci 1.6.2, php5.2.6 >>

Controller: form.php:
Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url', 'html', 'cc'));
        // Remember to use array() when calling multiple libraries etc, DUH!!!
        $this->load->library(array('validation', 'encrypt'));
        
        $rules['username'] = "trim|required|min_length[5]|max_length[12]|xss_clean";
        $rules['password'] = "trim|required|matches[passconf]";
        $rules['passconf'] = "trim|required";
        $rules['email'] = "trim|required|valid_email";
        $rules['number'] = "trim|required|callback_card_number_valid";

        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        $this->validation->set_rules($rules);
        
        $data['mypass'] = $this->encrypt->encode($this->input->post('password'));
        $data['number'] = truncate_card(card_number_clean($this->input->post('number')));
        
        $fields['username'] = 'Username';
        $fields['password'] = 'Password';
        $fields['passconf'] = 'Password Confirmation';
        $fields['email'] = 'Email Address';
        $fields['number'] = 'CC Number';

        $this->validation->set_fields($fields);

        if ($this->validation->run() == FALSE) {
            $this->load->view('myform');
        } else {
            $this->load->view('formsuccess', $data);
        }
    }
}
?&gt;

View: myform.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;

&lt;style type="text/css"&gt;
    .error { color:#CC0033; }
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;?php //echo $this->validation->error_string; //This is for creating a list of error messages ?&gt;

&lt;?php echo form_open('form'); ?&gt;

<h5>Username</h5>
&lt;?php echo $this->validation->username_error;?&gt;
&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username;?&gt;" size="50" />

<h5>Password</h5>
&lt;?php echo $this->validation->password_error; ?&gt;
&lt;input type="text" name="password" value="&lt;?php echo $this-&gt;validation->password;?&gt;" size="50" />

<h5>Password Confirm</h5>
&lt;?php echo $this->validation->passconf_error; ?&gt;
&lt;input type="text" name="passconf" value="&lt;?php echo $this-&gt;validation->passconf;?&gt;" size="50" />

<h5>Email Address</h5>
&lt;?php echo $this->validation->email_error; ?&gt;
&lt;input type="text" name="email" value="&lt;?php echo $this-&gt;validation->email;?&gt;" size="50" />

<h5>CC Number</h5>
&lt;?php echo $this->validation->number_error; ?&gt;
&lt;input type="text" name="number" value="&lt;?php echo $this-&gt;validation->number;?&gt;" size="50" />

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

I've also tried altering the card_number_valid function as follows:
Code:
&lt;?php

>snipped
    // If the total has no remainder it's OK
    //return ($sum % 10 == 0);
    if ($sum % 10 == 0) {
        return TRUE;
    } else {
        return FALSE;
    }
?&gt;

#5
[eluser]skattabrain[/eluser]
a cc validation helper in the core would rock ... i think it's a common task that web developers face often.

#6
[eluser]bretticus[/eluser]
Apparently, this has to be a controller function or method. I wrote a wrapper function in my controller to get around it:

Code:
&lt;?php
function check_card($card_number)
{
    $this->load->helper('cc');
    return card_number_valid($card_number);
}
?&gt;

I suppose if somebody wants to call http://url.tld/controller/check_card/4111111111111111 they are welcome too Smile Where the call is not going to a payment gateway, I'm not worried. But still the validation class should have access to a helper I think.

Also, don't forget to define the error message:

Code:
&lt;?php
$this->validation->set_message('check_card', '%s is invalid');
?&gt;


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.