I am creating a form with a many-many relationship. I have a posts table and an activities table. There is a many to many link using pivot table. I am creating a form to add one or more activities to the posts. I am receiving an 'ErrorException' - Trying to get property of non-object. I cannot understand why this is not working.
I would be grateful for any assistance you can offer me.
My relevant code is below.
//Posts/create.blade.php
{!!Form::open(['action' => 'PostController#store','method' => 'POST', 'class'=>'form-group'])!!}
{{Form::bsText('title','',['placeholder' => 'Trip Name'])}}
{{Form::bsTextarea('body','',['placeholder' => 'Trip Description'])}}
{{Form::bsSubmit('submit')}}
{{Form::label('activities', 'Activity:') }}
<select class="form-control select2-multi" name="activities" multiple="multiple">
#foreach($activities as $activity)
<option value="{{ $activity->id }}">{{ $activity->activity_name}}
</option>
#endforeach
</select>
{!! Form::close() !!}
// PostsController
public function create()
{
$activities = Activity::all();
return view('posts.create')->withActivities($activities);
$posts = Post::all();
}
public function store(Request $request)
{
// Create a new post using the request data
// Save it to the database
$this->validate(request(), [
'title' => 'required',
'body' => 'required',
]);
$post = Post::create([
'title' =>request('title'),
'body' =>request('body'),
'user_id' => auth()->id(),
'activity_id' => id()
]);
// And then redirect to somewhere in application
return redirect()->route('posts.show', $post->id);
}
This error throw only when you have empty variable but you point in blade file to render / display for browser. Or if you retrieve records from DB then add findOrFail in query to prevent those kind of issues. Thank you.
Related
I recently set up the ability to tag posts on my site. I had everything working fine. Then as I was wrapping up I tested all my admin side forms again. The Add Tag form no longer does anything. It doesn't even flash an error or redirect after submission. The page just reloads at the same URL. The only changes to the site I have made since initial testing was move the forms to the admin side of the dev site. Here is some code to hopefully reveal what the mystery is. Also my edit tag form is doing similar thing. It has no flash message but redirects back to the index, like its supposed to but with no changes made to the tag. Ill include the edit code as well.
Add.ctp in src/Template/Admin/Tags/Add.ctp
<div class="tags form large-9 medium-8 columns content">
<?= $this->Form->create($tag) ?>
<div class="form-group">
<fieldset>
<h1 class="page-header">New Tag</h1>
<?php
echo $this->Form->input('name', ['class' => 'form-control']);
?>
</fieldset>
</div>
<?= $this->Form->button(__('Submit'), ['class' => 'btn btn-primary']) ?>
<?= $this->Form->end() ?>
</div>
Here is my Add funciton in my TagsController:
public function add()
{
$this->viewBuilder()->layout('admin');
$tag = $this->Tags->newEntity();
if ($this->request->is('post')) {
$tag = $this->Tags->patchEntity($tag, $this->request->data);
if ($this->Tags->save($tag)) {
$this->Flash->success(__('The tag has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The tag could not be saved. Please, try again.'));
}
$this->set(compact('tag'));
$this->set('_serialize', ['tag']);
}
Here is my Edit funciton in my TagsController:
public function edit($id = null)
{
$this->viewBuilder()->layout('admin');
$tag = $this->Tags->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tag = $this->Tags->patchEntity($tag, $this->request->data);
if ($this->Tags->save($tag)) {
$this->Flash->success(__('The tag has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The tag could not be saved. Please, try again.'));
}
$this->set(compact('tag'));
$this->set('_serialize', ['tag']);
}
Edit.ctp in src/Template/Admin/Tags/Edit.ctp
<div class="tags form large-9 medium-8 columns content">
<?= $this->Form->create($tag) ?>
<div class="form-group">
<fieldset>
<h1 class="page-header">Edit Tag</h1>
<?php
echo $this->Form->input('name', array('class' => 'form-control'));
?>
</fieldset>
</div>
<?= $this->Form->button(__('Submit'), ['class' => 'btn btn-primary']) ?>
<?= $this->Form->end() ?>
</div>
Just as a side note. I started getting errors when creating a new post as well.
General error: 1364 Field 'section_id' doesn't have a default value
I did go into my DB and give the field a default value. But then when I fill out the form for a new post again, the error just moves to the next table column. I am assuming they are some how related since they popped up at the same time and because tags and posts are related to each other.
TagsTable:
class TagsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tags');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('PostsTags', [
'foreignKey' => 'tag_id'
]);
}
/**
* Default validation rules.
*
* #param \Cake\Validation\Validator $validator Validator instance.
* #return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
return $validator;
}
}
Tags Entity:
class Tag extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => false,
'id' => false
];
}
When I place <?php debug($tag); ?> into my add.ctp view this is the out put it gives me:
object(App\Model\Entity\Tag) {
'[new]' => true,
'[accessible]' => [],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Tags'
}
Again, in question always post debug pathEntity output, in your case debug($tag), also Tag Entity, your validation code, and how looks your db tags table.
Answer:
General error: 1364 Field 'section_id' doesn't have a default value
This means that you have not passed a value for this field.
You can change that table field to accept null or empty value and/or set default if not passed from application, or make validation in your TagsTable to be sure if submitted data valid before send to db.
After question updated:
protected $_accessible = [
'*' => false, <---- should be true
'id' => false
];
This means that all fields except id are accessible
How can i store my data inside db using {!! form !!} builder?
controller:
public function create()
{
$categories = \DB::table('categories')->lists('title', 'id');
return view('dash.reports.create')->with('categories', $categories);
}
/**
* Store a newly created resource in storage.
*
* #return void
*/
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
Report::create($request->all());
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}
my view:
{!! Form::label('category_id', trans('reports.category_id'), ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('category',
(['0' => 'Select a Category'] + $categories),
null,
['class' => 'form-control'])
!!}
{!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
when i press button action return me blank page, any idea?
You don't have a form, so you're not submitting data to the correct page. You're sending a POST request to the same URL that you used to access the form, which is probably not what you want.
This guide will show you how to set up a Laravel form that points at a controller method which will allow you to capture input, validate it, and store it in the database.
http://www.easylaravelbook.com/blog/2015/02/09/creating-a-contact-form-in-laravel-5-using-the-form-request-feature/
solved
view:
{!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
controller:
public function create()
{
$category = Category::lists('title','id');
return view('dash.reports.create')->with('category', $category);
}
I am trying to achieve something very simple but not sure if this is supported, possible or unexpected behaviour. I have a Symfony entity field which loads some data based on the selection of another field. The data is loaded ok but I want the option name and value to be the same. At the moment, it is populating the names ok, but I want the values to be the same as the names (the choices_as_values option in a Symfony choice field). Is that possible in an entity field.
Here is an example code:
$builder
->addEventListener(
Form\FormEvents::POST_SET_DATA,
function (Form\FormEvent $event) {
$attributeData = $event->getData();
$event->getForm()->add(
'group',
'entity',
array(
'class' => 'AppBundle:CategoryAttributeData',
'placeholder' => 'Select a data group',
'label' => 'Attribute Group',
'choice_label' => 'content',
'required' => false,
'query_builder' => function (Repository\CategoryAttributeData $repository) use ($attributeData) {
$queryBuilder = $repository->createQueryBuilder('u')
->select('u')
->where('u.type = :type')
->andWhere('u.group IS NULL')
->setParameter('type', $attributeData->getType())
;
return $queryBuilder;
}
)
);
}
)
;
The output is:
<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="1">Cars</option>
<option value="2">Electronics</option>
<option value="3">Furniture</option>
</select>
What I am trying to achieve is:
<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="Cars">Cars</option>
<option value="Electronics">Electronics</option>
<option value="Furniture">Furniture</option>
</select>
Since this field is populated via an event listener (because it depends on another field value) I cannot add a view transformer in here.
Any suggestions?
This is actually more complicated than it would seem on the surface. The Entity form type assumes that the "values" of the choice selector are the unique identifier for an entity, this allows the form type (and its associated transformers) to find and transform the passed values from and to the relevant entities. So the first question is - can you uniquely identify your entity by the string "Cars", or "Electronics"? If you can't, then the next question is how were you planning on converting that string into the entity?
If you can, then things are relatively easier. Effectively you need to provide a different ChoiceList implementation for the choice field type - this is effectively what the Entity type already does. I'm not familiar with the correct way to do this and I believe the method changed between Symfony 2.6 and 2.7, but I would look into classes like Symfony\Bridge\Doctrine\Form\Type\EntityType as well as the base Symfony\Component\Form\Extension\Core\Type\ChoiceType.
Short version: it's not wholly straightforward but definitely possible.
It can be done with the option 'choice_value', for example:
'choice_value' => function (?Team $team) {
return $team ? $team->getBesoccerId() : '';
}
It's 100% compatible with the 'query_builder' option. Continuing the same example:
->add('team', EntityType::class, array(
'class' => Team::class,
'label' => 'Equipo',
'query_builder' => function (EntityRepository $er) use ($lang) {
return $er->createQueryBuilder('t')
->andWhere('t.besoccerId IS NOT NULL')
->andWhere('i18n.cod_lang = :cod_lang')
->setParameter('cod_lang', $lang)
->leftJoin('t.translations', 'i18n')
->orderBy('t.sort', 'ASC');
},
'choice_label' => 'name',
'choice_value' => function (?Team $team) {
return $team ? $team->getBesoccerId() : '';
}
))
You can find more information in the documentation.
I am creating a custom module in Drupal 7 that is a job search. I am wanting to make it so you can specify a clean URL to perform the search like so:
www.example.com/job-board/cardiology/california
Where "cardiology" is the "specialty" variable and "california" is the "location" variable.
now I know how to do this as far as mapping the variables in my hook_menu and accessing the variables. The problem is how do I create a search form that creates the clean url when you submit it? In other words if I had this:
<form method="get" action="job-board">
<select name="specialty">
<option value="cardiology">Cardiology</option>
<option value="some_other_value">Some Other Value</option>
</select>
<select name="location">
<option value="california">California</option>
<option value="some_other_state">Some Other State</option>
</select>
<input type="submit">
</form>
and I submit it, it will go to www.example.com/job-board?specialty=cardiology&location=california instead of www.example.com/cardiology/california. How can I make the form construct a clean URL?
You should create that form using Drupal's Form API and assign a custom submit callback to it.
In that submit callback, you can perform validation and if everything is OK, redirect the user to the appropriate URL.
See the source code of Examples modules, specifically one related to Form API in order to learn more about it.
In short, here's what you would do:
/**
* Sample form.
*/
function MYMODULE_sample_form() {
$form['specialty'] = array(
'#title' => t('Specialty'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['state'] = array(
'#title' => t('State'),
'#type' => 'select',
'#options' => array(
'california' => t('California')
),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Submit the form'));
return $form;
}
/**
* Sample form submit processing.
*/
function MYMODULE_sample_form_submit($form, &$form_state) {
$specialty = $form_state['values']['specialty'];
$state = $form_state['values']['state'];
drupal_goto($specialty . '/ ' . $state);
}
When i create an entity field in Symfony2, how i can specify the value of the select option-field generated ?
This is the snippet of my entity field:
->add('preferred_language', 'entity', array(
'mapped' => false,
'property' => 'name',
'class' => 'Common\MainBundle\Entity\Language',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'DESC');
}
Actually i can specify the shown value through the property and it takes automatically the id referred to the db table. Good. What can i do, instead, whether i want to change the option value?
<option value="my_value">my_property</option>
If you create an "entity" field, you create a relation form between two entities, so the default field value is the id annoted field on your entity.
You can change this behavior with a View Transformer. Check out this doc:
http://symfony.com/doc/current/cookbook/form/data_transformers.html#model-and-view-transformers
also looked for same solution and found it here: different property for entity field type in form
just set the property in the field type options and create a getter for the property that formats the string the way to output the label.
I solved it by the following way:
code in FormType must be the same:
->add('preferred_language', 'entity', array(
'mapped' => false,
'property' => 'name',
'class' => 'Common\MainBundle\Entity\Language',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'DESC');
}
In the controller, I get the data with DQL:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM MyBundle:Language ORDER BY u.id DESC');
$data = $query->getResult();
I pass the data through render method:
return $this->render('MyBundle:Default:page.html.twig',
array('formulario' => $formulario->createView(),
'data' => $data));
In the twig file, I create a <select> item with id "myproject_mybundle_myformtype_preferred_language":
<select id="myproject_mybundle_myformtype_preferred_language" name="aeneagrama_adminbundle_itemcontenidotype[preferred_language]" class="form-control">
<option value="0">-- Choose an option --</option>
{% for item in data %}
<option value="{{ item.your_column }}">{{ item.name }}</option>
{% endfor %}</select>
Finally, when you get input data from the form, you can get it in the controller:
$form->get('preferred_language')->getData();
In your controller make this change,
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM MyBundle:Language u ORDER BY u.id DESC');
$data = $query->getResult();