Welcome Guest, Not a member yet? Register   Sign In
My Questions
#1

[eluser]php_princess[/eluser]
Going to be putting my questions in just this thread, you know, to keep it all organized.

#1 I'm not sure if I should use helpers or not. I cannot figure out why I would want to use some of them. Like, the form helpers. It seems like it would be easier, and less of a drain on the CPU, to just write the HTML myself. Is there some unseen advantage I'm missing here?

#2 I have a session question, too. You see, from reading the documentation, I was under the impression that whenever you use sesson:Confusedet_userdata() it sends the information to the user's cookies, whether you have the database stuff enabled or not. However, when I use it, it seems to only be updating the user's database row. If I check the cookies, all that's there is the "ci_session" variable and its value, not any I created. Is this how it is supposed to work?

#3 How can I keep from making calls to the same views multiple times in one controller? For instance, in my User controller there are signup, login, activate, etc. methods and each one loads the header and the footer. This isn't the biggest inconvenience in the world, but I was told to follow the DRY principle, so, I'm trying to do so Smile
#2

[eluser]giberti[/eluser]
#1 - I use helpers for things like data conversion that don't apply to just a single model. For example, converting CP1252 (Windows) default to UTF8:

Code:
if( ! function_exists('toUTF8') )
{
function toUTF8($str){
  return @iconv("CP1252", "UTF-8//TRANSLIT//IGNORE", $str);
}
}
#3

[eluser]giberti[/eluser]
#3 - You can place the header/footer views in the view file itself. Not perfect, but it would remove them from the controller.
#4

[eluser]TWP Marketing[/eluser]
1) Do use the helpers, they make maintenance easier. I particularly like the database helpers since they do a lot of escaping which is always a pain. Plus, if you someday change db drivers, the helpers mean minimal code rewriting.

2) When you select database for your session storage, the cookie is still written, however the critical data is moved to the database file, which is a little more secure and most importantly, it allow you to avoid the 4kb limit on cookie size.

3) Make a single method which loads the header and footer views into string variables which you then return.
Note the third parameter on the loader call. If prevents the loader form sending the view to your browser and instead outputs a string containing your html code. Check the User Guide for more details.
Code:
function load_header_footer($data)
...
$data['footer'] = $this->load->view('myfooter',$data, TRUE);
$data['header'] = $this->load->view('myheader',$data, TRUE);
...
return($data);

last, split you posts up into single questions, it makes it easier for us to answer them and your topic sentence can be more specific (unlike: "My Questions" which doesn't tell us much about the question(s) itself...)
#5

[eluser]Aken[/eluser]
#3 - FireStarted just shared a good basic way to load views inside a template: http://ellislab.com/forums/viewthread/223232/
#6

[eluser]php_princess[/eluser]
Hello again guys. I'm having some trouble deciding what to do about my form validation needs.

You see, I was messing with CI's native form validation and I realized what it can check for is limited. Like, there are cases when I only want a value to have alpha-numeric or underscore characters and the closest thing CI gives me is a check that allows alpha-numeric, underscore, or dash characters. This, obviously, won't work.

The documentation says I can use a callback function in the calling controller, when I need to specify custom validation rules, but, because form validation happens in more than one controller, I'm concerned about having the exact same functions in multiple controllers. As I'm sure you know, such redundancy is best avoided.

So, I was wondering how you guys would approach this?
#7

[eluser]Rolly1971[/eluser]
you could extend the form_validation class by creating a file in application/core called: MY_Form_validation.php

Code:
class MY_Form_validation extends CI_Form_validation
{
function __construct()
{
  parent::__construct();
}
}

in this class put your own common validation functions. and when the form_validation library is loaded you MY_Form_validation class is also automatically loaded.

check this section of the user guide: http://ellislab.com/codeigniter/user-gui...asses.html

#8

[eluser]php_princess[/eluser]
I'm trying to make a form validation check that compares the entered value against a list of acceptable values. Picture a "What is your gender?" question along with a <SELECT> menu containing a choice of "male" or "female". You'd think this enough to limit the possible answers but what if somebody makes their own form, with bogus values, and submits it to the controller?

I added this code to MY_Form_validator.php

Code:
public function one_of_these($currentField, $acceptable)
{
  if ( ! is_array) explode(',', $acceptable);
  
      return ( ! in_array($acceptable, $currentField)) ? FALSE : TRUE;
}

It accepts a string or an array. If $acceptable is a string, it turns it into an array. The reason is so if I have a long list of acceptable values as would, for example, be the case with countries, I can just pass it an array. I picture I'd do it like this:

Code:
//Somehow I'd populate $array with values

$this->form_validation->set_rules('gender', 'Gender', 'required|one_of_these['.$array.']');

Anyway, this is the code I have in my controller right now:

Code:
$this->form_validation->set_rules('gender', 'Gender', 'required|one_of_these[male,female]');

Nothing is happening, so I don't think "male,female" is being passed to the function. I know MY_Form_validator.php is working because I have other custom functions in there, and they work.
#9

[eluser]NeoArc[/eluser]
An error message is missing when the validation fails.

Code:
$this->set_message('one_of_these','The field %s does not contain one of these values...');
#10

[eluser]php_princess[/eluser]
I have my own copy of form_validation_lang.php in application/language/english and it takes care of that. I did the same thing with my other custom validation checks and they work.

This is the line pertaining to one_of_these:

Code:
$lang['one_of_these'] = "What you put for %s was not in the list of acceptable answers.";




Theme © iAndrew 2016 - Forum software by © MyBB