1
votes

This is quite strange, I have a text input in a registration form and when I am setting the $validate array in the model, I am getting a trim() error - Warning (2): trim() expects parameter 1 to be string, array given [CORE\Cake\View\Helper.php, line 754]

Form input

<?=$this->Form->input("lastname", array("label" => array("text" => "Last name *"), "class" => "required", "div" => array("class" => array("input text last")))); ?>

Model validate array

public $validate = array(
        'lastname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Last name is required'
            )
        )
    );

It seems that when I am doing a validation rule for that field, the div styling array - "div" => array("class" => array("input text last")) is causing the error. I added the following code to the Helper.php file as a workaround:

if(is_array($options["class"])) {
            $options["class"] = $options["class"][0];
        }

but I would like to know why it is causing this error.

1

1 Answers

2
votes

Too many nested arrays for the Form Helper. Try this:

$this->Form->input("lastname", array(
    "label" => "Last name", 
    "id" => "MyInputDiv",
    "class" => "input text last required"));

array("text" => "Last name *") You don't need an array here if you are just setting the text value of the label. Array is reserved for html attributes.

"div" => array("class" => array("input text last")) Not familiar with this in the formHelper. Perhaps you were looking for the before and after attributes to set the wrapping div of the element here.