Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Using Validation Class without using Fieldset Class
  • Hello !

    I was wondering if it was possible to use Validation Class methods of checking without creating a form with Fieldset Class. Indeed, I created my form and field manually in a template. Is it possible to link Validation instance to a fieldset created manually in a view ? I tested but it doesn't work. Is it possible to use checking methods without link to a fieldset or do I have to create my own validation class with RegEx to use independently ?


  • HarroHarro
    Accepted Answer
    There should not be any code in a view. If you construct fieldsets in a View, you are not following best practices. And as a side effect, you don't have the fieldset in your controller, where you need it.

    You can use Validation without a fieldset (see http://docs.fuelphp.com/classes/validation/validation.html#/usage), but it will create a fieldset object behind the scenes anyway, since it's where the fields are stored.
  • I tried to create a validation instance linked to my fieldset as explained in doc but I didn't success in. The reason why I put my code in my view is because I use custom elements with a JQuery UI Framework and FuelPHP Classes utilities can't create them. 
  • HarroHarro
    Accepted Answer
    Why not? A lot of our apps use JQuery, and without problems.

    It just means that you have to tweak the template that is used to generate the form (in config/form.php), and if you're doing really special stuff, generate the individual fields instead of the entire fieldset.

    An (shortened) example from one of our apps:

    echo Form::open(array('action' => Uri::main(), 'class' => 'focus', 'onsubmit' => 'return fuel_set_csrf_token(this);', 'novalidate' => 'novalidate'));

    echo '    <fieldset>
            <legend>'.__('sessions.form.'.$type).'</legend>';
            if ($type == 'edit')
            {
    echo '
            <input id="form_session_id" name="session_id" type="hidden" value="'.$form->field('id')->get_attribute('value').'" />';
            }
    echo '
            <dl>'.
            $form->field('fuel_csrf_token')->build().
            ($type == 'edit' ? $form->field('id')->build() : '').
            $form->field('sessiontype_id')->build().
            $form->field('date')->build().
            (\Auth::is_client()?$form->field('requestdate')->build():'').
            $form->field('status')->build().
            $form->field('org')->build().
            $form->field('type')->build().
            $form->field('language')->build().
            (\Auth::is_client()?$form->field('code')->build():'').
            (\Auth::is_client()?$form->field('snapshot')->build():'').
            (\Auth::is_client()?$form->field('client_id')->build():'').
            $form->field('clientref')->build().
    '        </dl>
            </fieldset>';

    echo    Form::submit('save', __('sessions.form.save'), array('value' => 'save', 'class' => 'button left')).
            Form::submit('btn_close', __('sessions.form.close'), array('value' => 'btn_close', 'class' => 'button left')).
            ((\Auth::is_client() and $candidates) ? ('<a class="button" href="#" onclick="window.open(\'/candidates/presencelist/'.$form->field('id')->get_attribute('value').'\');return false;">'.__('sessions.form.candidatelist').'</a>') : '').
            Form::submit('cancel', __('sessions.form.cancel'), array('value' => 'cancel', 'formnovalidate', 'class' => 'button gray right'));

    echo Form::close();

  • Yes indeed ! An other way too would be to extend Validation and Fieldset classes in my app folder and add methods to create and manage custom UI elements. 

    Your framework is so well designed. :D
  • HarroHarro
    Accepted Answer
    Thanks for the kudos!

    You should have a look at the Fuel v2 fieldsets, you will love it! They can be fully customized using renderers instead of templates, so you have 100% control over the HTML it will generate for each of the field types..

    v2 is constructed from individual components, so you can use v2 fieldsets in a v1 application today, if you wish. Several companies already do so.
  • Oh yes I'm interested ! Where can I get Fuel v2 Fieldset and how can I install it ? Is there a doc ?
  • HarroHarro
    Accepted Answer
    There are no docs yet (there is no new documentation system yet).

    You can find the repository here: https://github.com/fuelphp/fieldset
    It is installable through composer: https://packagist.org/packages/fuelphp/fieldset

    The repo has a short readme with a few examples. If you want to know more, hop onto IRC and talk to Steve (uru), he's in charge of Fieldset.
  • I don't understand how to link the form object to the fieldset instance and process to validation. Could you explain me plz ?
  • If you're talking about the v2 packages, pop into IRC and talk to Steve. I haven't used them yet, so I can't help you with it.
  • For v1, when you create a fieldset object, it has built-in validation, so you can just add rules to fieldset fields:

    An example:
            // construct the form
            $form = \Fieldset::forge('form');
            $form->form()->add_csrf();
            $form->add('company_name', __('registration.step2.company_name'), array('class' => 'medium'));
            $form->add('company_contact', __('registration.step2.company_contact'), array('class' => 'medium'));
            $form->add('company_address', __('registration.step2.company_address'), array('class' => 'medium'));
            $form->add('company_zipcode', __('registration.step2.company_zipcode'), array('class' => 'small'))->add_rule('strtoupper')->add_rule('valid_zipcode');
            $form->add('company_city', __('registration.step2.company_city'), array('class' => 'medium'));
            $form->add('person_initials', __('registration.step2.person_initials'), array('class' => 'small'))->add_rule('required')->add_rule('valid_initials');
            $form->add('person_prefix', __('registration.step2.person_prefix'), array('class' => 'small'));
            $form->add('person_surname', ('registration.step2.person_surname'), array('class' => 'medium'))->add_rule('required');
            $form->add('person_gender', __('registration.step2.person_gender'), array('type' => 'select', 'options' => __('global.gender')))->add_rule('required');
            $form->add('person_birthdate', __('registration.step2.person_birthdate'), array('class' => 'datepicker birthdate dateonly'))->add_rule('required')->add_rule('valid_birthdate');
            $form->add('person_birthplace', __('registration.step2.person_birthplace'), array('class' => 'medium'))->add_rule('required');
            $form->add('person_address', __('registration.step2.person_address'), array('class' => 'medium'))->add_rule('required');
            $form->add('person_zipcode', __('registration.step2.person_zipcode'), array('class' => 'small'))->add_rule('required')->add_rule('strtoupper')->add_rule('valid_zipcode');
            $form->add('person_city', __('registration.step2.person_city'), array('class' => 'medium'))->add_rule('required');
            $form->add('person_email', __('registration.step2.person_email'), array('class' => 'medium'))->add_rule('required')->add_rule('valid_email');
            $form->add('person_phone', __('registration.step2.person_phone'), array('class' => 'small'))->add_rule('required');
            $form->validation()->add_callable('\\Validationrules');

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion