1

I need to validate that a form input is numeric in CakePHP 1.3. However, the input is not a property of the model, so I don't think I should try to set the validation for it in the model. Instead, some calculations are done on that input and the results are used in the resulting model object. How can I validate this in the view/controller? That is, check that what the user input was numeric and show a validation error message if not before passing it through the calculations? Thanks!

2 Answers 2

2

There's nothing wrong with defining model validation rules for non-existing / calculated fields, but you can also use the Validation class which might be cleaner. See 1 and 2.

Sign up to request clarification or add additional context in comments.

Comments

1

If you use jquery at least you don't have to do a full page reload to check. Especially if it's only for one value. Just another option, see if it helps!

if($('#Field').val() != "")
{
    if(!($.isNumeric($('#Field').val())) {
        alert('value must be numeric');
    }
}

2 Comments

Definitely also useful. I may want to stick with the CakePHP validation for consistency, but this is certainly useful.
Where possible it's good to do both. The cakephp validation is very good but allowing it to get to the server before it checks anything especially just for a number could possibly be overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.