如何使用javascript访问twig表单密码字段值?

时间:2014-12-03 14:21:55

标签: forms symfony passwords field twig

我想知道如何使用javascript访问twig表单密码字段值。解释如下:

这是我的html \ twig代码:

    <html>
    <head>
        <link rel="stylesheet" type="text/css" href="{{asset('bundles/ikprojhome/css2/css11.css')}}"/>
        <script src='{{asset('bundles/ikprojhome/lib/jquery.min.js')}}'></script>
        <script src='{{asset('bundles/ikprojhome/lib/sha512.js')}}'></script>
        <script>
            $(document).ready(function(){
                {% for h in pwd %}
                var pwd = "{{ h.pswd }}";
                {% endfor %}
                $('#update').on('click', function() { 

                    var a = {{ form.vars.value.passWD.first }};
                    alert(a);
                });
            });
        </script>
    </head>
    <body>
        <center>
            <div id="container">
                <div id="header">
                </div>
                <div id="content">
                    <table width="100%" height="100%" align="center">
                        <tr>
                            <td>   
                                <!--<form id="actualpwdform">
                                    <table align="center" width="100%">
                                        <tr>
                                            <td class="separation">
                                                <label>Actuel:</label>
                                            </td>
                                            <td>
                                                <input id="actualpwd" type="text">
                                            </td>
                                        </tr>
                                    </table>
                                </form>-->
                                <form id="pwdform" action="{{path('ikproj_home_passwordchange',{id:id})}}" method="POST" {{ form_enctype(form) }} onsubmit="javascript:parent.jQuery.fancybox.close();">
                                    {{ form_errors(form) }}
                                    <table align="center">
                                        <tr>
                                            <td class="separation">
                                                <label>Actuel:</label>
                                            </td>
                                            <td>
                                                <input id="actualpwd" type="text">
                                            </td>
                                        </tr>
                                        <tr height="20px"></tr>
                                        <tr>
                                            <td class="separation">
                                                <label>Nouveau:</label>
                                            </td>
                                            <td>
                                                {{ form_widget(form.passWD.first) }}
                                            </td>
                                        </tr>
                                        <tr height="20px"></tr>
                                        <tr>
                                            <td class="separation">
                                                <label>Saisir à nouveau:</label>
                                            </td>
                                            <td>
                                                {{ form_widget(form.passWD.second) }}
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="center" id="button" valign="bottom">
                                                <input class="button" type="submit" value="" id="update"/>
                                            </td>
                                        </tr>
                                    </table>
                                    {{form_widget(form._token)}}
                                </form>
                            </td>
                        </tr>
                    </table> 
                </div>
            </div>
        </center>
    </body>
</html>

这是表单构建器的代码:

    <?php

namespace Ikproj\HomeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserpasswordchangeType extends AbstractType
{
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('passWD', 'repeated', array(
                    'type' => 'password',
                    'invalid_message' => 'Les mots de passe doivent correspondre',
                    'options' => array('required' => true),
                    'first_options'  => array('label' => null),
                    'second_options' => array('label' => null)));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Ikproj\HomeBundle\Entity\User'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'ikproj_homebundle_user';
    }
}

这是我正在处理的实体的代码:

    <?php

namespace Ikproj\HomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="Ikproj\HomeBundle\Entity\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_user", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="pseudo", type="string", length=255)
     */
    private $pseudo;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255)
     */
    private $passWD;

    /**
     * @var string
     *
     * @ORM\Column(name="sexeuser", type="string", length=255)
     */
    private $sexeuser;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dateanniv", type="date")
     */
    private $dateanniv;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set pseudo
     *
     * @param string $pseudo
     * @return User
     */
    public function setPseudo($pseudo)
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    /**
     * Get pseudo
     *
     * @return string 
     */
    public function getPseudo()
    {
        return $this->pseudo;
    }

    /**
     * Set passWD
     *
     * @param string $passWD
     * @return User
     */
    public function setPassWD($passWD)
    {
        $this->passWD = $passWD;

        return $this;
    }

    /**
     * Get passWD
     *
     * @return string 
     */
    public function getPassWD()
    {
        return $this->passWD;
    }

    /**
     * Set sexeuser
     *
     * @param string $sexeuser
     * @return User
     */
    public function setSexeuser($sexeuser)
    {
        $this->sexeuser = $sexeuser;

        return $this;
    }

    /**
     * Get sexeuser
     *
     * @return string 
     */
    public function getSexeuser()
    {
        return $this->sexeuser;
    }

    /**
     * Set dateanniv
     *
     * @param \DateTime $dateanniv
     * @return User
     */
    public function setDateanniv($dateanniv)
    {
        $this->dateanniv = $dateanniv;

        return $this;
    }

    /**
     * Get dateanniv
     *
     * @return \DateTime 
     */
    public function getDateanniv()
    {
        return $this->dateanniv;
    }
}

问题是我收到以下错误消息:&#34; Impossible to access an attribute ("first") on a string variable ("3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2") in IkprojHomeBundle:configuration:passwordchange.html.twig at line 23&#34;

所以,我的问题是:

  1. 我的代码有什么问题?
  2. 如何访问表单中的密码字段值?

2 个答案:

答案 0 :(得分:1)

最后,我解决了这个问题!...实际上,在我的html \ twig代码中,我更改了下面的代码部分:

var a = {{ form.vars.value.passWD.first }};

到这一个:

var a = $('#{{ form.passWD.first.vars.id }}').val();

正如您在此处所注意到的,我使用jQuery来访问twig表单密码字段值。

答案 1 :(得分:0)

您似乎已经使用{{ form.vars.value.passWD }}获取了密码值,不需要first属性。

您还可以使用jQuery访问密码值。检查输入的名称,例如使用fos用户捆绑包,您需要这样的内容:$('input[name="fos_user_change_password_form[new][first]"]').val()。检查您的html源代码以替换name的值。