symfony2 form hidden row - php

I'm trying to put two hidden fields in a form for a particular user. I have this code:
<div id="mio_miobundle_empleadotype">
{{ form_row(edit_form.dni) }}
{{ form_row(edit_form.nombre) }}
{{ form_row(edit_form.username) }}
{{ form_row(edit_form.apellido1) }}
{{ form_row(edit_form.apellido2) }}
{{ form_row(edit_form.localidad) }}
{{ form_row(edit_form.provincia) }}
{{ form_row(edit_form.telefono) }}
{{ form_row(edit_form.movil) }}
{{ form_row(edit_form.email) }}
{{ form_row(edit_form.direccion) }}
{% if is_granted('ROLE_A') %}
{{ form_row(edit_form.activo) }}
{{ form_row(edit_form.role) }}
{%endif%}
{{ form_row(edit_form.password) }}
and:
$builder
->add('dni','text',array('label' => 'Dni'))
->add('nombre','text',array('label' => 'Nombre'))
->add('username','text',array('label' => 'Usuario'))
->add('apellido1','text',array('label' => 'Apellido1'))
->add('apellido2','text',array('label' => 'Apellido2'))
->add('email','email',array('label' => 'Email'))
->add('localidad','text',array('label' => 'Localidad'))
->add('provincia','text',array('label' => 'Provincia'))
->add('telefono','text',array('label' => 'Teléfono'))
->add('movil','text',array('label' => 'Móvil'))
->add('direccion','text',array('label' => 'Dirección'))
->add('activo')
->add('role')
->add('password', 'repeated', array('first_name' => 'Nueva contraseña','second_name' => 'Repite contraseña','type' => 'password' ,'invalid_message'=> 'Las contraseñas deben ser iguales.'))
;
}
but I get this error:
Catchable Fatal Error: Argument 1 passed to mio\mioBundle\Entity\Empleado::setRole() must be an instance of mio\mioBundle\Entity\Role, null given, called in /var/www/Symfony/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 347 and defined in /var/www/Symfony/src/mio/mioBundle/Entity/Empleado.php line 289
I say that because I have to fill in the state are any help?

Place the checking role code in the controller
public function someAction()
{
$form = $this->createFrom( // ...
if (false === $this->get('security.context')->isGranted('ROLE_A')) {
$form->remove('activo');
$form->remove('role');
}
// ...
}
And in the template check if form field is defined
{{ edit_form.activo is defined ? form_row(edit_form.activo) : '' }}
{{ edit_form.role is defined ? form_row(edit_form.role) : '' }}

Instead of not rendering the fields completely, try hiding the fields from view using css.
{% if is_granted('ROLE_A') %}
{{ form_row(edit_form.activo) }}
{{ form_row(edit_form.role) }}
{% else %}
{{ form_widget(edit_form.activo, { 'attr': {'class': 'hide'} }) }}
{{ form_widget(edit_form.role, { 'attr': {'class': 'hide'} }) }}
{% endif %}

Related

Symfony: Custom Twig Rendering for Entity Type

I have an issue with rendering an Entity Type in a Symfony Form.
Here is what I call in the form type:
$builder
->add('categories', EntityType::class, array
(
'class' => 'AppBundle\Entity\ArticleCategory',
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
'constraints' => array
(
new NotBlank(array('message' => 'Select Category'))
)
))
This is the template I am trying to do:
{% form_theme form _self %}
{% block _article_categories_entry_row %}
<div class="ckbox ckbox-default">
{{ form_widget(form) }}
{{ form_label(form) }}
{{ form_errors(form) }}
</div>
{% endblock %}
In the same template I am doing
{{ form_start(form) }}
{{ form_row(form.categories) }}
{{ form_end(form) }}
The weird thing happens when I use block _article_categories_entry_widget and it picks it up however it renders the label twice. Any ideas how to manage to situation ? In this scenario above it doesn't render or adds the ckbox class at all ! :(
You can loop through your form.categories in template as below and render checkboxes as you want.
{{ form_start(form) }}
{% for category in form.categories %}
{{ form_label(category) }}
{{ form_widget(category) }}
{% endfor %}
{{ form_end(form) }}
As you named your custom block _article_categories_entry_row, I assume, your form is named ArticleType.
The custom block for a specific form field should be named with following pattern:
_<form_name>_<field_name>_<part_name>
Possible parts as row, errors, widget, label. You're using row here.
But the entry part of block name is unnecessary.
Try with:
{% block _article_categories_row %}
You can find more details in Symfony Documentation

MethodNotAllowedHttpException laravel 4.2 error

here is my ContactController.php:
public function destroy($id){
$contact = Contact::find($id);
$contact->delete();
return Redirect::to('http://localhost:8000/contactsview');
}
Here is my rountes.php
Route::delete('/contactsview/destroy/{id}', array('uses'=>'ContactController#destroy'));
Here is my index.blade.php:
{{ Form::open(array('url'=>'/contactsview/delete/'.$contact->id, 'method'=>'DELETE', 'style'=>'display:inline;')) }}
<!-- {{ Form::hidden('id', $contact->id) }} -->
{{ Form::submit('Delete') }}
{{ Form::close() }}
What did I do wrong?
Try the form with this instead, passing in the $contact->id as a param rather than directly in the URL:
{{ Form::open(array('method' => 'DELETE', 'action' => array('ContactController#destroy', $contact->id )) }}

Getting a NotFoundHttpException() Error in laravel

I just have a very simple product category creation form in laravel , like so:
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
For the create method i have the following code:
public function postCreate() {
$validator = Validator::make(Input::all() , Category::$rules);
if($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
Now when i click on the submit button, i get the following error:
C:\xampp\htdocs\ecomm\bootstrap\compiled.php
if (!is_null($route)) {
return $route->bind($request);
}
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
You can see the error more visvually HERE.
What am i doing wrong ?
Instead of
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
try this:
{{ Form::open(array('route'=>'post.homes')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
In routes.php:
Route::post('aboutus', array('as' => 'post.homes', 'uses' => 'HomeController#postContactUs'));

Laravel 4 - Input::all() is not containing values

Input:all is not showing the post data. Any clue why? I have already tried Input:all before and I haven't experienced this.
print_r($_POST);
print_r(Input::all());
exit;
Array
(
[_token] => Sr4MSYL2ovDWMtU3o2Ms6YULFcxsuFYtAasS3Vqu
[username] => name
[password] => password
)
Array
(
)
This is my login view. I don't think that this has anything to do with the issue though.
#extends("layout")
#section("content")
{{ Form::open() }}
{{ $errors->first("password") }}<br />
{{ Form::label("username", "Username") }}
{{ Form::text("username", Input::old("username")) }}
{{ Form::label("password", "Password") }}
{{ Form::password("password") }}
{{ Form::submit("login") }}
{{ Form::close() }}
#stop

laravel 4 Auth::attempt allways returns true?

Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}

Categories