Welcome Guest, Not a member yet? Register   Sign In
Dropdown, first one = default, and give validation error?
#1

[eluser]Schumacher[/eluser]
Hallo there.
I have a problem, and it seems there is no way for me to work around it.
Im creating this create user part, and i got some information i would like to have on the user, for an example, i would like to know the gender.

In the dropdown box i would like to have it like:
-gender
-male
-female

And if the gender is selected the form_validation gives back an error, that there must be choosen one.
Can't seem to find a way to put gender as nothing.
I get the data from a database. And inside the select, the id from the database is the value.

Hopefully you know what i mean, and know a way i can make this right Smile
#2

[eluser]Jelmer[/eluser]
Create a callback rule which only returns TRUE when 'male' or 'female' was chosen. That should do it...

More on callbacks in (who could guess) the User Guide: http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]Schumacher[/eluser]
Mh okay, that fixed the validation part.
But arn't there any smart way to give the default value of it ? Because it wouldn't match up if i had 'gender' standing in the same table in my database.
#4

[eluser]Jelmer[/eluser]
This isn't too hard is it? Look into the user guide on how to pass a default value (the last parameter if memory serves me right) and if you need to translate it to the values used by the pulldown that's not too hard using if-else, switch or a translation array (an array which has as its keys the values that need translation and their translation as the values).

Try actually looking into the user guide before asking others, this is pretty well explained there (Form helper & Form validation library).
#5

[eluser]Schumacher[/eluser]
You got me wrong.

right now i have ;
Male
female

standing in my dropdown,
My i would like to have gender stading at the very top, as the first value, the default value. I know how to get a defualt value, that is not the problem. but the best way to get 'gender' to stand first. Are there a way to do that smart?
or is the best one just to insert it into it like : $sex[0] = "Gender";
So the value is 0 and the name is Gender.
so the thing im asking, is, if $sex[0] = "gender"; is the best and easiest way to get gender at top and add it to the dropdown, or are there any other way?
#6

[eluser]Jelmer[/eluser]
Ok, I did get you wrong. That's more a best practice question than a howto since it doesn't really matter what you make the value, as long long as you don't accept it.
I tend to either set it to the empty string "" (which is a valid array key) or to "void", but whatever you decide it doesn't really matter to the user - only to you or whoever needs to look at the code at a later time.




Theme © iAndrew 2016 - Forum software by © MyBB