来自公共源的 jQuery/PHP 验证
我有一个通用对象的模型类,其中包含要对输入执行的验证数组。类似于:
class Dog extends GenericObject
{
$this->validations = array ( "breed" => array ("max_length" => 15) );
}
这一切都很好,但我正在尝试找出一种通过 javascript 评估某个字段的有效性的方法。到目前为止,我一直在使用 AJAX 调用来触发控制器来检查模型。伟大的。然而,对于某些验证,例如长度、匹配正则表达式,调用 PHP 似乎是系统密集型的。
我知道我显然可以在 Javascript 中重复验证,但我知道必须有一种方法可以在不重复验证的情况下做到这一点。我想到了一个常见的 JSON 或 XML 文件,但这些文件是可编辑的,因此可能容易受到攻击。
您对如何最好地实现这一目标有什么建议吗?
编辑:我也考虑过让 PHP 生成相关的 jQuery 验证,但不认为这会被认为是非常好的形式?
I have a Model class for a Generic Object that contains an array of validations to perform for input. Similar to:
class Dog extends GenericObject
{
$this->validations = array ( "breed" => array ("max_length" => 15) );
}
This is all fine and dandy but I'm trying to work out a way to assess the validity of a certain field via javascript. So far I've been using an AJAX call that fires off to Controller to check the Model. Great. However, for certain validations such as length, matches regular expression, it seems ridiculously system intensive to be calling PHP.
I know I could obviously just repeat the validations in Javascript but I know there has to be a way to do it without repeating my validations. I thought of a common JSON or XML file but these would be editable and could therefore be vulnerable.
Do you have any suggestions as to how best achieve this goal?
Edit: I also considered having PHP generate the relevant jQuery validations but didn't think this would be considered terribly good form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们所做的就是在 (PHP) 模型中定义所有验证规则,然后从这些基本规则生成相关的 jQuery 验证规则。这工作得很好,但唯一的缺点是,当您使用重要的验证方法时,您最终也会在 Javascript 中定义自定义方法。对于我们来说,这只是一个小缺点,因为我们没有很多这样的规则,所以我想这取决于您的情况是否适合您。
不过,我很想听听其他一些(更好的)方法。
What we do is define all our validation rules in our (PHP) Models and then generate the relevant jQuery validation rules from those base rules. This works fine, but the only drawback is that when you use non-trivial validation methods you end up defining your custom method in the Javascript as well. For us this is just a minor drawback as we don't have many of those rules, so I guess it depends on your situation if it's a good method for you.
I would love to hear of some other (better) methods however.