Different validation messages in different web browsers

孤者浪人 提交于 2019-12-13 04:25:55

问题


I am a newbie in CakePHP and doing my first application - first blog from cakephp tutorial. Everything is fine, but one thing bothers me. When I define validation rules in my model, validations are working.

But every web browser show different message. For example firefox show message in czech language (i'm from czech), chrome show "Please fill out this field" and internet explorer show "This field cannot be left blank".So i tried to translate the messages (by add parameter message into model validation). this is working, but only in internet explorer, other browsers are without change. Is there any way, how to have same validation messages same in all browsers?

Validation in model:

public $validate = array(
        'title' => array(
            'rule' => 'notEmpty',
            'message' => 'Please fill.....'
        ),

回答1:


I think you are talking about client side validation messages. If I'm right the messages you see are created by your browser and are browser dipendant. Cake just tells the browser that the field is required by setting the required property in the input tag.

Instead the actual validation that cake does is made server side. If your browser sends data to the server then cake validates the data and returns error messages




回答2:


There are two checks in place. The first one (that one that you see) ist the check on client side. The input field has a required parameter. So the browser knows that the field can't be blank and says than in it's language. Nothing is sent to the server till now. After filling in and sending the form, then the second check is in place which is the cakephp validation.

Try it with this:

    'title' => array(
        'kosher' => array(
            'rule' => 'email',
            'message' => 'Please make sure your email is entered correctly.'
        ),
        'required' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter your email.'
        )

Then you will see: 1) if you enter nothing, then the browser message appears (client side), 2) if you enter some text, which is no email, then the message from above appears ('Please enter your email.')



来源:https://stackoverflow.com/questions/21093940/different-validation-messages-in-different-web-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!