whitebear
whitebear

Reputation: 12471

How can I handle password input?

I am managing user table by SonataAdminBundle

protected function configureFormFields(FormMapper $formMapper){ 
    $formMapper    
    ->with('General')   
     ->add('username')    
     ->add('email') 
     ->add('plainPassword','text',array('required' => false))

I leave password input blank,when I edit the existing user data. It is OK,password is kept as same .

However I forgot to input password when I create new item. It shows SQL error(password has required attribute in database)

but if I deleted required => false attribute,

->add('plainPassword','text')

it requires new input when you edit.

How can I change the behavior or How can I handle password input as normal input?

Upvotes: 0

Views: 167

Answers (1)

TautrimasPajarskas
TautrimasPajarskas

Reputation: 2796

You should use different field type for create and update actions. In your configure configureFormFields:

protected function configureFormFields(FormMapper $formMapper)
{
    // ...

    if ($isCreateAction) {
        $formMapper->add('plainPassword','text');
    }
    else {
        $formMapper->add('plainPassword','text', array('required' => false));
    }   
}

I don't know how to set $isCreateAction exactly, but maybe you could check Admin::getSubject() method? Inside it there is a routine to get GET id. If there is no id set, then action type is create.

Upvotes: 1

Related Questions