简体   繁体   中英

More than one textarea giving me problems

I'm using Bootstrap with Laravel 4.2. Whenever I try to add more than 1 textarea to my form, I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comment' cannot be null

I have isolated the problem to be that whichever textarea I allow to be first, no matter what variable name I give it or how I try to save it, only the first textarea content gets saved and the others get "null" when I try to read it in using Input::get('my_textarea_name');

Here's a snipped of my form:

{{ Form::open(array('url' => 'course_review', 'id'=>'clearForm'))}}
       <div class="row">
    <form role="form">
        <div class="form-group">
            <label for="course_comment">How can students succeed in this course?</label>
            <textarea class="form-control" rows="5" id="course_comment" name="course_comment"></textarea>
        </div>
    </form>
</div>
<hr>

<div class="row">
    <form role="form">
        <div class="form-group">
            <label for="assignments_comment">What kind of assignments and tests did this course have?</label>
            <textarea class="form-control" rows="5" id="assignments_comment" name="assignments_comment"></textarea>
        </div>
    </form>
</div>
<hr>

<div class="row">
    <form role="form">
        <div class="form-group">
            <label for="prerequisites_comment">What skills and prerequisites are required for this course?</label>
            <textarea class="form-control" rows="5" id="prerequisites_comment" name="prerequisites_comment"></textarea>
        </div>
    </form>
</div>
    <hr>
    <div class="row">
        <div class="col-xs-12">
            <button type="submit" class="btn btn-info ladda-button" data-style="zoom-out" id="create_or_edit_button"><span class="ladda-label">
            <span class="glyphicon glyphicon-plus"></span> Add this review</span>
            </button>
        </div>
    </div>
</div>
{{ Form::close() }}

Here's a snipped from my Model:

$review = new Review();
$review->course_comment = Input::get('course_comment');
$review->assignments_comment = Input::get('assignments_comment');
$review->prerequisites_comment = Input::get('prerequisites_comment');
$review->save();

I was just wondering if anyone is aware of this or knows of a way around it? Thanks in advance.

The problem is you have nested forms.

{{ Form::open(...)}}
    <form role="form">
    ...
    </form>
    <form role="form">
    ...
    </form>
{{ Form::close() }}

You should get rid of all those <form role="form">...</form> . They are doing nothing and they break HTML standard. Your browser gets confused and only submits one of them at a time. Hence you get null in the not submited ones. You can have several forms in a page but they should not be nested.

From the html5 docs :

4.10.3 The form element

Content model:

Flow content, but with no form element descendants.

you are trying to insert a null value into the comment field of your table. just make sure it isn't null.

It's pretty obvious that cause of this error is empty value of comment

$review = new Review();
$review->course_comment = Input::get('course_comment' ,false);
$review->assignments_comment = Input::get('assignments_comment', false);
$review->prerequisites_comment = Input::get('prerequisites_comment', false);
$review->save();

So... after a lot of pain, I just figured out the answer to my own question. Hopefully this will help someone else who makes the same mistake. In the code I originally posted, you will see I had wrapped each of my textareas in its own <form role="form"> wrapper:

<div class="row">
    <form role="form">
        <div class="form-group">
            <label for="assignments_comment">What kind of assignments and tests did this course have?</label>
            <textarea class="form-control" rows="5" id="assignments_comment" name="assignments_comment"></textarea>
        </div>
    </form>
</div>
<hr>

This was the source of the problem, as it prevented textareas #2 and #3 from getting read. I'm surprised the first textarea was getting read, in fact. Removing the extra wrappers allowed me to submit more than one textarea successfully. There should be only 1 wrapper for a form. I had copied and pasted this code directly from w3c so it took me a while to realize multiple copies of part of the code would cause me an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM