0

CakePHP 3 in debug mode:

When submitting a form, I would like to add an error for an input in Controller. As it does the validation in the Table.

In the controller function I have tried to assign the error:

$user['errors'] = ['password2' => array('text' => 'Custom text')];

But it creates a new array in the model $user, and it does not insert the error.

In addition, I would like to focus the cursor to the input with the error, so it is clear to the user which field is falling.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Please show a complete code example. `In addition, I would like to focus the cursor to the input with the error` you can't do that with php, if you want to do that, [use js](http://stackoverflow.com/q/17500704). – AD7six Dec 12 '16 at 15:32

1 Answers1

0

I have solved the validation in Model, because I think what I was trying is not possible.

For the second part of the question, to focus the cursor I have added the following script in the view Layout/default.ctp:

<script type="text/javascript">
    $(window).load(function() {
        $("form .error input").first().focus();
    });
</script>