0

Okay, I am really struggling with this data validation with CakePHP. I'm using CakePHP 2.4.6. I have the following code snippets from the Models:

User.php:

public $validate = array(
    'username' => array(
        'alphanumeric' => array(
            'rule' => array('alphanumeric'),
            'required' => false,
            'message' => 'Please provide your username to log in.'
        ),
    ),
    'password' => array(
        'required' => true,
        'message' => 'Please provide your password to log in.'
    ),
);

Job.php:

public $validate = array(
    'patterns_file' => array(
        'extension' => array(
            'rule' => array(
                'extension', array('csv','txt'),
                'message' => 'Please upload the patterns in CSV or TXT file.'
            )
        ),
        'uploadError' => array(
            'rule' => 'uploadError',
            'message' => 'Something went wrong with the upload.'
        )
    ),
    'xy_value' => array(
        'rule' => array('allowEmpty', false),
        'message' => 'Please select the xy_value.'
    )
);

My first problem is not blocking but still disturbs me. Although I have set messages to the validation rules, non of them appear, only the default message is shown no matter how I set the 'message'. I just mention this because maybe it has something to do with my main problem which is the following:

In Firefox and Chrome the validation is working.

The username and password fields are for the login. They are shown like this in FF (and similar in Chrome):

User's login validation

There is a single drop-down list and File-upload validation in the Job's view, which are shown like this:

Single drop-down list validation File-upload validation

So it is working in FF (30.0) and Chrome (35.0), but it is not working in IE 9. I can easily press login and can easily submit my form without selecting anything from the drop-down list or uploading a file. No red-lighted background and no info bubble.

update 1: I have just checked it again and I was mistaken, the file-upload is only working on the aspect that there has to be uploaded something, but I can upload a file with any extension.

zdtorok
  • 594
  • 1
  • 7
  • 21
  • Apart from the not working `select` element validation, could it be that you are mixing up CakePHP based backend validation and browser based frontend validation? ps. please always mention your CakePHP version and tag your question accordingly (same for IE)! – ndm Jun 19 '14 at 15:07
  • I have updated my question with my CakePHP and IE version numbers. For this part I want to have a front-end FORM validation, but as I know it is related to the models in CakePHP as it is already doing some validation. – zdtorok Jun 19 '14 at 15:11

1 Answers1

3

Frontend and backend validation are separated

Nothing of what you are seeing there is related to CakePHP validation, except for the fact that the form helper defines the required and type attributes for the form elements, which are then interpreted by the browser validation, but primarily in the sense of whether it should apply validation to a specific element at all.

How exactly the field is validated (except for when using the pattern attribute) and what messages are being displayed is by default totally up to the browser, though most of them support specific validation for the various input types, like for example email validation.

However, CakePHP has no further influence on the browser validation, if you need frontend validation based on your model validation settings, then you'll have to use a custom JavaScript based form validation that makes use of your model settings.

Form validation not supported in IE9

The reason why you aren't seeing anything in IE9, is that form validation is only supported as of IE10. For earlier versions of IE you'll have to use some kind of polyfill.

Community
  • 1
  • 1
ndm
  • 59,784
  • 9
  • 71
  • 110
  • Thank you for pointing this out. I didn't know that it is a HTML 5 feature and built into the browsers. I have found this link and now I see that it's really not supported.. [form-validation-support](http://caniuse.com/form-validation) – zdtorok Jun 20 '14 at 07:55