问题
I have this simple user model which validates the field if it is empty and should give the message, but it is not picking up model message instead giving HTML message.This is the code for my model validation
public $validate = array(
'first_name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'This field cannot be empty',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
Test link: http://59655b15.ngrok.com/medicare/users/home
Click on signup tab and click create profile button in the popup without entering any first name. It should show the message of model but it is showing default message. If I remove the validation from model it does not do any validation.
回答1:
The message you're seeing is the standard browser message that appears when a field has the 'required' attribute, but has no content in it.
You're not seeing the Model's message because at that point, it hasn't even submitted yet.
Assuming you use the FormHelper, CakePHP automatically adds the 'required' attribute on fields that cannot be empty.
Example of the required field in action with it's HTML.
If you want really want to have your model messages as the Javascript validation, you could look into building it out or at some of the exisitng helpers/plugins...etc.
An example (albeit a bit old): Automagic Javascript Validation Helper
来源:https://stackoverflow.com/questions/24997857/cake-hp-model-validation-message-not-showing-in-html-form