Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 预填充表单字段上的验证_Symfony_Symfony Forms_Symfony 2.1 - Fatal编程技术网

Symfony 预填充表单字段上的验证

Symfony 预填充表单字段上的验证,symfony,symfony-forms,symfony-2.1,Symfony,Symfony Forms,Symfony 2.1,要在symfony2中的表单字段中设置默认值,我使用rel属性并结合jQuery,如下所述: 这一点非常有效,并提供以下信息: 如您所见,该字段预先填充了“在此处输入标题…”。如果我按原样验证表单,则不会在插入默认值时进行验证(这很有意义) 我想确保用户自定义此字段,而不是仅提交带有默认值的表单 是否有方法检查字段是否与rel属性的值相同?Hmmm 您可以在实体的注释中尝试以下操作: @Assert\Regex("/^(?!Enter a title here\.\.\.)/") 或者更

要在
symfony2
中的表单字段中设置默认值,我使用
rel
属性并结合jQuery,如下所述:

这一点非常有效,并提供以下信息:



如您所见,该字段预先填充了“在此处输入标题…”。如果我按原样验证表单,则不会在插入默认值时进行验证(这很有意义)

我想确保用户自定义此字段,而不是仅提交带有默认值的表单

是否有方法检查字段是否与rel属性的值相同?Hmmm

您可以在实体的注释中尝试以下操作:

@Assert\Regex("/^(?!Enter a title here\.\.\.)/")
或者更好:

/**
 * @Assert\Regex(
 *     pattern="/^Enter a title here\.\.\.$/",
 *     match=false,
 *     message="Please enter a title."
 * )
 */

我们可以在客户端执行此操作,并将rel属性与提交的数据进行比较。如果值相同,则清除对象:

$(function() {
    // When we submit the form
    $('form').submit(function() {

        //iterate over all the elements of the class "prepopulate"
        jQuery('.prepopulate').each(function(){

            //compare the values submitted vs rel attribute
            if( jQuery(this).val() == jQuery(this).attr('rel'))
                jQuery(this).val('');
        });
        return true; 
    });
});

+1.大把戏@keyboardSmasher!这样做的缺点是对每个字段重复代码。但是你知道,它很有效,所以很好!做得好!如果您感兴趣,我建议使用javascript解决方案,因为它的主要优点是迭代每个表单字段。谢谢你的帮助!Yeppers,客户端就可以了。我以为你想要一个服务器端选项,用于那些在浏览器中关闭javascript的疯子作业。哈哈,但现在我想起来了,如果他们的javascript关闭了,他们就不会有预先填充的字段了。那么,你赢了。:)
$(function() {
    // When we submit the form
    $('form').submit(function() {

        //iterate over all the elements of the class "prepopulate"
        jQuery('.prepopulate').each(function(){

            //compare the values submitted vs rel attribute
            if( jQuery(this).val() == jQuery(this).attr('rel'))
                jQuery(this).val('');
        });
        return true; 
    });
});