Welcome Guest, Not a member yet? Register   Sign In
Update a user and still use validation class?
#1

[eluser]Tommy_DK[/eluser]
Is it possible to use the validation class when updating a user? The problem is that the form should start out with being populated by information from the database. When the user submits and it shows a error message it should not throw-away the user input.

I think about something like this, but need to know what to check for...
(The sex-field is a radio-button... and that does not work)

<?
//Will be filling in the data from the database
//Unless it is being reloaded because of the validation
//Done to not lose user-input
If ($this->validation->sex = ''){
$sex = $profile->sex;
$first_name = $profile->first_name;
$last_name = $profile->last_name;
$introduction = $profile->introduction;
}
else {
$sex = $this->validation->sex;
$first_name = $this->validation->first_name;
$last_name = $this->validation->last_name;
$introduction = $this->validation->introduction;
}
?>
#2

[eluser]mikedfunk[/eluser]
I was wondering the same thing. What I ended up doing is checking for a post value. It's wordy and feels like a lot of code for a little thing but it seems to work:

Code:
<input name="name" type="text" id="name_field" value="<?php if (isset($_POST['name'])) { echo $this->validation->name; } else { echo $pg->name; } ?>" />

Similar thing for selects but do the select="selected" instead. Hope that helps someone. I originally tried the $this->uri->post() as a check but it returned false when the post value was null. $_POST returned a null value but i could verify that it was submitted so i could echo null.
#3

[eluser]Tommy_DK[/eluser]
Great solution, thank you very much Smile
- It seems like more code than CI normally needs, but I don't belive it can be done easier/better.
#4

[eluser]mikedfunk[/eluser]
yeah it's kind of a mess... another thing - if you use this, you have to declare rules and fields in your controller for every form input you want to use it with. Otherwise it throws an error for each one you don't have declared. For instance:

Code:
// validation
        $rules['name']            = "required";
        $rules['title']            = "required";
        $rules['is_section']    = "";
        $rules['in_menu']        = "";
        $rules['in_footer']        = "";
        $rules['is_active']        = "";
        $rules['description']    = "";
        $rules['url']            = "required";
        $rules['parent']        = "";
        $rules['meta_title']    = "required";
        $rules['meta_keywords']    = "";
        $rules['styles']        = "";
        $rules['head_content']    = "";
        $rules['content']        = "required";
        
        $fields['name']            = "Name";
        $fields['title']        = "Title";
        $fields['is_section']    = "";
        $fields['in_menu']        = "";
        $fields['in_footer']    = "";
        $fields['is_active']    = "";
        $fields['description']    = "";
        $fields['url']            = "URL";
        $fields['parent']        = "";
        $fields['meta_title']    = "HTML Meta Title";
        $fields['meta_keywords']= "";
        $fields['styles']        = "";
        $fields['head_content']    = "";
        $fields['content']        = "Page Content";

ugh what a pain.
#5

[eluser]Colin Williams[/eluser]
Quote:yeah it’s kind of a mess… another thing - if you use this, you have to declare rules and fields in your controller for every form input you want to use it with

Well, there are very easy ways to abstract this out. CI isn't forcing you to do anything stupid. It doesn't care how or where the rules come from. And if you're making a different point, show me a framework or methodology that just happens to magically know what fields are in your form.

The way to get values to your form works the same way you always pass data to a view. Generate a $values array (which essentially runs by the same logic you are running in your view), and pass that on to the view. For new forms, $values entries will be empty or contain default values. For edit forms, the $values entries will generally be populated.
#6

[eluser]mikedfunk[/eluser]
[quote author="Colin Williams" date="1221272285"]
Quote:yeah it’s kind of a mess… another thing - if you use this, you have to declare rules and fields in your controller for every form input you want to use it with

Well, there are very easy ways to abstract this out. CI isn't forcing you to do anything stupid. It doesn't care how or where the rules come from. And if you're making a different point, show me a framework or methodology that just happens to magically know what fields are in your form.

The way to get values to your form works the same way you always pass data to a view. Generate a $values array (which essentially runs by the same logic you are running in your view), and pass that on to the view. For new forms, $values entries will be empty or contain default values. For edit forms, the $values entries will generally be populated.[/quote]

relax man, i'm not blaming it on codeigniter. i'm not a programmer so i figured i was doing something the difficult way. That makes sense about generating the values array outside of the form and just echoing the value in the form. Much more MVC way of keeping the logic out of the view. Thanks for the tip. I guess you would find out if it's a new form the same way, by checking the post value?
#7

[eluser]Colin Williams[/eluser]
Quote:I guess you would find out if it’s a new form the same way, by checking the post value?

I can't imagine you would have the same controller function for creating a new entry and updating an old one. The workflow and logic is typically pretty different. So, you don't really have to know it's new if you have an add() and an edit() function, for example.
#8

[eluser]mikedfunk[/eluser]
I do have separate controllers for adding an entry and editing an entry. But like Tommy_DK said, the problem is in the edit view - at first view you want to populate the fields with values from the database. When they submit the form, if they break the validation rules, you want to populate those fields with the validation values instead so they stay the same as they entered before they hit submit. otherwise if they change the "content" field extensively but accidentally cleared the "name" field (which is required), all their "content" field changes are lost because it repopulates the field from the database. The opposite is a more obvious problem: if you set the value of the fields just to the validation values, it's not really editing because all the fields are blank when the view loads despite the columns containing data in the database.

Maybe you didn't understand the original question?
#9

[eluser]Colin Williams[/eluser]
No, I just don't think you understand the methodology I'm laying out. If validation fails, the form should be populated with the incorrectly supplied values. So, in the instance this happens, the $values passed to the view with the form need to be updated with the posted values.

So, new form:

1. $values = default values
2. if validation fails, $values = posted values

And, an edit form:

1. $values = db values
2. if validation fails, $values = posted values
#10

[eluser]mikedfunk[/eluser]
got it, yeah that's what i gleaned in post #5. ooooh i see now, you thought when i said "new form" i meant a form to insert a new record. No, I meant an unsubmitted edit form. ok now we're on the same page... yeah i'll apply that logic in the controller instead of the view to take the logic out of the view.




Theme © iAndrew 2016 - Forum software by © MyBB