Welcome Guest, Not a member yet? Register   Sign In
How do I validate non-required fields?
#1

[eluser]Skyscraper[/eluser]
I'm building an API in CodeIgniter, and I'm having trouble with the validation for an UPDATE method. My table has 3 columns (plus id):

- first_name
- last_name
- city

I'd like the client to be able to update any of the 3 columns, and apply validation rules (such as min_length[1]) to each one the client wants to update.

The issue is that if the client POSTs this:

Code:
first_name=Foo&last;_name=

It skips validating `last_name` since CodeIgniter sees it as "empty" and it's not "required" by my validation. But then when active record attempts to `UPDATE` the record in MySQL, it sets `last_name` to `""` (empty).

I don't want the fields to be *required*, but if they are set, they should have a minimum length.

Any ideas?
#2

[eluser]sv3tli0[/eluser]
When you set last_name to empty its normal to be updated to empty !

Lets say it in other order.. if you want to put your last name to empty and it can't be saved ? Will that be nice ?

Show us your update function to see what you are trying to update.
#3

[eluser]noideawhattotypehere[/eluser]
Or fill in the forms with existing values? I guess they can't be empty? This should do the trick.
#4

[eluser]Skyscraper[/eluser]
Thanks for your replies.

@noideawhattotypehere There is no form - this is an API

@sv3tli0 I want to disallow setting `last_name` to empty by enforcing a minimum length. But I want you to be able to update one field, like `first_name`, without updating `last_name`. These two things appear incompatible.

This is not some crazy specific use-case; what I'm trying to do is common, particularly for APIs. Codeigniter is behaving unexpectedly in how it's handling it. Is there a way around it or is this a bug?
#5

[eluser]sv3tli0[/eluser]
@Skyscraper:

If you are at user settings page and you are updating user fields and there is last_name your script should update it.

Not to change it just set the old value inside the last name input and it will update it self..
So if user change it to empty your check will said : "at least 4 symbols.."
And if user doesn't change it the old value will update it self with the same value.. And you will keep it as it is!


Your case is custom "Don't update field if its empty"..
Perhaps you must create dependence on which to update your table..
#6

[eluser]Skyscraper[/eluser]
@sv3tli0 Again though, there is no form - I am creating an API. Users interact with it via REST.

To give you a use case, imagine an edit-in-place page where a user can update just the Last Name if they want to, without having to update all the rest of the fields. This would be done by sending &last_name=asdf. In this case, first_name is not required. But if they do try to update First Name and set it to empty (&first;_name=), they should get a validation error.
#7

[eluser]sv3tli0[/eluser]
Ok..
This is custom case..

In real you are trying to response with 1 method to few different update requests.

The best way is in your controller where you put form rules to set such checks:
Code:
if($this->input->post('last_name') !== NULL){
    $this->form_validation->set_rules('last_name', 'Last name', 'min_length[4]');
}

so for each field if its set you will add form rule..

And at your model you should update only those fields which are given from the validation .
#8

[eluser]Skyscraper[/eluser]
@sv3tli0 I suppose that would work, just seems a bit silly to have to do that for every field on every controller...I thought the validation (and putting the rules in a config file) was supposed to make that part easier Undecided Again, I do not feel this is a custom case or anything that uncommon. Codeigniter is choosing to ignore empty fields when validating but not when updating via Active Record.

Is there any way to automate this or fix the issue in the Form Validation class?
#9

[eluser]sv3tli0[/eluser]
@Skyscraper

Form validation is validating you last_name as correct empty string - '' and pass it to your update function..
This work can't be skipped because Validation has validated both first and last name as correct (emtpy or not) and pass them to update which do them both..
You may set at update function a loop..

Code:
foreach($inputs as $key=>$value){
   if(empty($value)){
       unset($inputs[$key]);
   }
}

which will skip all empty fields..
#10

[eluser]Skyscraper[/eluser]
Okay, I've figured out where the bug lies. In system/libraries/Form_validation.php line 335 you can see:
Code:
if (isset($_POST[$field]) AND $_POST[$field] != "")
This considers "not set" and "set but empty string" to be the same thing, so when _execute line 488 occurs, it sees you don't have "required" as a validation rule and does not apply your "min_length" rule because it thinks the value is "not set" (null) even though it's "set but empty string".

To fix this, simply comment out the second half of the conditional:
Code:
if (isset($_POST[$field])/* AND $_POST[$field] != ""*/)
(Or extend the Form_validation.php class)




Theme © iAndrew 2016 - Forum software by © MyBB