Welcome Guest, Not a member yet? Register   Sign In
Catch Validation_Errors() into an array() ?
#1

[eluser]stormbytes[/eluser]
The title pretty much says it all. What can I say? I'm anal about my html, and the validation_errors() function just spits out errors without regard for aesthetics. Anyhow, if I could catch the output of this function into an array, something along the lines of $errors = array($validation_errors), it'd be easy enough to loop through the array and print a tidy UL.

I already looked into form_error() and it's too much work. I'd have to futz around with stuff every time I added/removed a form field, etc.

Curious if anyone else has any thoughts.
#2

[eluser]stormbytes[/eluser]
It just occurred to me that I could add a '|' or '*' to the closing delimiter, then catch the whole of the output into $errors, and then explode($errors, 'delimiter'). I suppose I could then simply cycle through the array and output each error item.

Well.. I think that'd work!? Time to give it a shot!
#3

[eluser]WanWizard[/eluser]
Error messages are stored in the $this->form_validation->_error_messages array.

Since it's (support to be) a private property, extend the form validation class, and add a method that retrieves the values from this array.
#4

[eluser]stormbytes[/eluser]
Woah that was fast!

Hiya, Wiz Smile

So...

Code:
class Foo extends Form_Validation()
{
     $errors = $this->form_validation->_error_messages;
    
      etc...
}

Then I call this method from the controller that's validating the user input (form)?

Sorry my head's locking up.
#5

[eluser]kaejiavo[/eluser]
You should really read and try to understand the user guide.
Chapter "Creating Core System Classes" --> Extending Core Class

WanWizard's advice was to add a method, that would look like this, then:

Code:
class MY_Form_Validation extends Form_Validation()
{
function get_error_messages()
{
  ...
  return $array_with_error_messages;
}
}
#6

[eluser]stormbytes[/eluser]
[quote author="mawi27" date="1288186694"]You should really read and try to understand the user guide.
Chapter "Creating Core System Classes" --> Extending Core Class
[/quote]

Hey, thanks for the tip.

I'm curious though, what makes you think I haven't or don't incessantly read the CIUG? It's a 'big book' my friend and the fact that you have obviously gotten/read the specific part makes it possible for you to contribute as wonderfully as you have, which I (and whoever else find this useful) very much appreciate!

As a side note -

I've been reading nothing BUT the CIUG for weeks! I think it exemplifies the way documentation should be written, and not just for PHP. In fact, I'd even go so far as to say that the CIUG can be credited with 70% of CI's overall usefulness.

Thanks for pointing chapter & verse. I'm certainly going to review the section you suggested, and I found your code example is very helpful.
#7

[eluser]kaejiavo[/eluser]
[quote author="stormbytes" date="1288192679"]
Hey, thanks for the tip.

I'm curious though, what makes you think I haven't or don't incessantly read the CIUG?[/quote]


It's the fact that i found in nearly all of your posts you try to find ways of doing things different from the examples in the user_guide. Examples:
- It shows how to extend a base class by using name MY_Baseclass, you try to do it by foo or MY_common. Then you ask "Is there an issue with loading libs?"
- It says there, helpers contain functions w/o dependence to other functions, you try to access config->vars globally in your helper.
- It is explained how to access the variables in config files, you are constructing an own class.
- You ask a question regarding the very basic PHP boolean operator 'OR' where you simply have not understood how this works
- In another Post you ask a bunch of questions just to learn that PHP variables are case sensitive
and so on.
These make me think you didn't read neither the PHP nor the CI user guide thoroughly enough and/or your project is a bit ambitious for your PHP knowledge. Of course this is only an assumption on my side, as i don't know you as a person really.

No offense meant, only cause you asked.
Marco
#8

[eluser]stormbytes[/eluser]
[quote author="mawi27" date="1288194601"]
It's the fact that i found in nearly all of your posts you try to find ways of doing things different from the examples in the user_guide. Examples:[/quote]
[quote author="mawi27" date="1288194601"]
- It shows how to extend a base class by using name MY_Baseclass, you try to do it by foo or MY_common. Then you ask "Is there an issue with loading libs?"[/quote]

Remarkable. Actually I spent the better part of 10 hours on that issue. It was never my intention to 'extend core classes'. In fact, I was reading about loading custom classes (application/libraries) which, if anything, aims to achieve a very different goal. As for experimenting with MY_common, well that's simply a name I decided to use to describe the class I wanted to write. I saw 'MY_' as a CI conventional prefix and so I prepended my class name. You seem to have altogether misunderstood my intentions and seemingly, feel that I went about things 'differently'.

[quote author="mawi27" date="1288194601"]
- It says there, helpers contain functions w/o dependence to other functions, you try to access config->vars globally in your helper.
[/quote]

I invite you to re-read the post, perhaps a bit more thoroughly then merely skimming some quotes, so that you might better understand the substance and context of the questions I asked. In this instance specifically, I was fairly elaborate (in a subsequent respose) about what my aims were, and how/why these parted from the conventional $this->load->config() approach.

[quote author="mawi27" date="1288194601"]
- It is explained how to access the variables in config files, you are constructing an own class.
[/quote]

See above -

[quote author="mawi27" date="1288194601"]
- You ask a question regarding the very basic PHP boolean operator 'OR' where you simply have not understood how this works
- In another Post you ask a bunch of questions just to learn that PHP variables are case sensitive
and so on.

These make me think you didn't read neither the PHP nor the CI user guide thoroughly enough and/or your project is a bit ambitious for your PHP knowledge. Of course this is only an assumption on my side, as i don't know you as a person really.

No offense meant, only cause you asked.
Marco[/quote]

What can I say, Marco? Its obvious you are simply beyond 'stupid questions' and silly oversights. Some of us aren't nearly as gifted and dare not hold ourselves to such lofty standards, despite incessant effort and hard work. I understand it can be hard to empathize for someone who is clearly accomplished. I thank you for sharing your sentiments on my 'issues' and for your words of encouragement.

On a personal note, I don't take offense and I certainly appreciate your input, wherever/whenever you choose to provide it. It's very helpful and ultimately that is the point of learning and sharing. If I may offer my own word of advice, I'd encourage you to continue posting helpful tips and solutions, as you seem far more gifted in the realms of CI & PHP then in more qualitative matters.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB