1

I have an array like:

array(
    'username' => array(
        'required' => true,
        'min' => 5,
        'max' => 20,
        'unique' => 'users',
        'name' => 'Username'
    )
);

This array goes into a function like (this is not the complete function):

//$source = $_POST
public function validate($source, $items = array()) {
    foreach($items as $item => $rules) {
        foreach($rules as $rule => $rule_value) {
                    //db->sanitize = mysqli_real_escape_string()
            $value = $this->db->sanitize($source[$item]);

            if($rule === 'required' && empty($value)) {
                            //The problem is here with $rule['name']
                $this->addError($rule['name'].' is empty');
            }
        }
    }
}

So I would like to use the 'name' key from the array to display a user friendly input field name but all it returns is: Warning: Illegal string offset 'name' in

4
  • 1
    Both $rule and $rule_value are strings. They are the key/value (respectively) of your $rules array. I'm guessing you want $rules['name']. Commented Jun 2, 2014 at 16:24
  • @RocketHazmat thank you for your answer that solved it. Pretty stupid I couldn't come up with that my self tought :P Commented Jun 2, 2014 at 16:26
  • We all make silly mistakes now and then :) Commented Jun 2, 2014 at 16:27
  • @RocketHazmat if you would post it as an answer I can close the question as solved Commented Jun 2, 2014 at 16:28

1 Answer 1

2

Both $rule and $rule_value are strings. They are the key/value (respectively) of your $rules array.

I'm guessing you want $rules['name'].

if($rule === 'required' && empty($value)) {
    $this->addError($rules['name'].' is empty');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.