use strict;
use warnings;
use Test::More tests => 4;

use HTML::FormFu;

my $form = HTML::FormFu->new(
    { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

my $config_file = 't-aggregate/elements/label_value_submission.yml';

$form->load_config_file($config_file);

{

    # before 1st render

    $form->default_values( {
            id   => 2,
            name => 'billy bob',
        } );

    is( $form, <<HTML );
<form action="" method="post">
<fieldset>
<input name="id" type="hidden" value="2" />
<div>
<span name="name">billy bob</span>
</div>
<input name="name" type="hidden" value="billy bob" />
<div>
<input name="submit" type="submit" />
</div>
</fieldset>
</form>
HTML
}

{

    # new form object created to handle form submission

    my $form = HTML::FormFu->new(
        { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

    $form->load_config_file($config_file);

    $form->process( {
            id   => '2',
            name => 'billy bob',
        } );

    is( $form, <<HTML );
<form action="" method="post">
<fieldset>
<input name="id" type="hidden" value="2" />
<div>
<span name="name">billy bob</span>
</div>
<input name="name" type="hidden" value="billy bob" />
<div>
<input name="submit" type="submit" />
</div>
</fieldset>
</form>
HTML
}

{

    # check that a submitted value isn't used by the Label element
    # when there's no other field with the same name in the form

    my $form = HTML::FormFu->new(
        { tt_args => { INCLUDE_PATH => 'share/templates/tt/xhtml' } } );

    $form->load_config_file($config_file);

    my $hidden = $form->get_field( {
            type => 'Hidden',
            name => 'name',
        } );

    $hidden->parent->remove_element($hidden);

    $form->process( {
            id   => '2',
            name => 'billy bob',
        } );

    ok( $form->submitted_and_valid );

    ok( !$form->valid('name') );
}
