Welcome Guest, Not a member yet? Register   Sign In
[Naming] FormValidation and set_value()
#1
Question 

Hi everyone,

i noticed a possible confusion about the set_value function while i was reading the documentation of the form validation (specifically here)

The example shown is :
PHP Code:
<h5>Username</h5>
<?
php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" /> 

Why is "set_value" in an echo? I mean, "set" is usually used for any setter function. Shouldn't it be named "get_value" ?
Maybe i'm the only one which was disturbed by that but, i always used "set_value" as a setter and not a getter.

I hope that i post that in the right section and that you undesrtand what i mean.

Thanks for your reading and have a good day !
Reply
#2

set_value() is used for repopulate the value when server side error occur. i.e set the previous value when error happen. its make sense. not bad
The largest Bengali tutorial site on Web Development in this planet
Reply
#3

Quote:Why is "set_value" in an echo? I mean, "set" is usually used for any setter function. Shouldn't it be named "get_value" ?

Maybe i'm the only one which was disturbed by that but, i always used "set_value" as a setter and not a getter.

It's a matter of how you look at the action being performed.  The method will "set" a string value (the method return type) upon either an error after form submission or upon initial form display if you have coded the second parameter default value.  The string value will be read from the POST array if the form was submitted and failed.  The 'set' implies that the string value is being sent TO the output stream.

 It is 'echo' because it is an instruction to the php interpreter to place the string value returned by the method into the html stream to be sent to the browser.
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply
#4

I'm not sure how you could use the form_validation library's set_value method as a setter except to set the value of a field in a form. The method doesn't set anything in the library, it just returns a value based on the inputs and the existing state of the library.

The name set_value seems to be intended for consistency with the other form population methods in the library (set_select, set_radio, and set_checkbox) rather than with the general OOP idea of getters/setters.
Reply
#5

Quote:The method doesn't set anything in the library, it just returns a value based on the inputs and the existing state of the library.
That's the point which make the name a little bit wrong to me(because it's a getter's mechanism with a setter's name Tongue ).

But like twpmarketing said, it's about how you see the action... Clearly, i think that it's not the best way to name it but if it's normal to other people, it's mustn't be a really wrong name  Smile
Maybe i shouldn't consider that echo is setting the value got by set_value() in the html stream

I should have been too much harassed by grumpy teachers who where always telling us something like "a setter will never return anything".

Thank you for your time guys!
It was nice to have your opinion about it!
Reply
#6

Your grumpy teachers are correct ... only this isn't a setter. Smile
It is a naming issue, but CI's legacy forces some of these on us.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB