Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Repopulating forms, not using Fieldset class
  • Hello, I'm making a system and I need to add "clients", I use ORM, parser package (with smarty), lang class and a custom definition for my forms. I'd like to know if there's a way to define a custom template for an specific form, to be rendered with fieldset class (would be much easier to validate) or a way to repopulate without using Fieldset class, and to relate Input fields with ORM model, without making this for each field: <code>$model->field = Input::get('field');</code> If you'd like to know why I need to to it like this, is because of template engine, which I love because inheritance <a href="http://pastebin.com/5Z380FdF">Template</a>; Btw, I've used null in some inputs because I needed placeholders but I don't really know what to use here, in CI I used set_value('field') Thanks in advance!
  • Thank you!!! I didn't see that, now it works, but here comes another problem, I can't use placeholders with lang files, I know it's not supposed to load anywhere else but in labels, well I can't use the helper or Lang::get in my ORM model. http://i.imgur.com/8UK1i.png Btw, how can I make a field to have a label but to not show it? I need a label for the error messages
  • You can not use function calls in property definitions, so that's not going to work. You can work around that by creating a static _init() method for your model class. This gets called when the class loads (see it as a static class constructor), and you can add the code to it to define your placeholders:
    public static function _init()
    {
        static::$_properties['address']['form']['placeholder'] == __('profile.street');
    }
    

    Labels in a fieldset are always shown if they are defined. Fieldsets will show validation errors automatically if present. There is a template variable for that, which you can style the way you want it using css.
  • Thank you, thats a workaround, why two == signs? I've used only one xD Labels are used for validation messages as far as I know but for some fields, I don't have labels but placeholders Let me illustrate what I need
    http://i.imgur.com/aZOTF.png And I have this template
    'field_template' => "<div class=\"{error_class}\">{label}{required}{field}<small>{error_msg}</small></div>\n"
    

    I use json for my lang files, and I know I can change validation lang file to not mention the :label, my problem here is that no error messages are loaded (not even default ones) http://i.imgur.com/3o6Ko.png Error messages are supposed to be shown below inputs :) Thank you so much for your help!
  • nakp wrote on Wednesday 1st of August 2012:
    Thank you, thats a workaround, why two == signs? I've used only one xD
    Oops. That looks like a typo...
    nakp wrote on Wednesday 1st of August 2012:
    Labels are used for validation messages as far as I know but for some fields, I don't have labels but placeholders Error messages are supposed to be shown below inputs :)
    Ok, I get you. If you don't have labels you need to define custom error message using the get_message() method on the validation error object. In the template you can add html surrounding {error_msg} to allow you to position them using css. To put them below the input I use a "float:left" on them and push them down a bit, but ymmv...
  • If you see the last image, it's already below the input, but the message is not there http://i.imgur.com/3o6Ko.png The error message should be the one for min lenght, the error class is applied when validation fails (that red area is the <small>{error_msg}</small> , but the error message is not shown Should be just like this http://i.imgur.com/8ZDw0.png
  • Any idea why I don't get any error message? I've tried with default form templates and without my views but I still don't get any error message
  • I don't know. Is the message in the HTML? If so, it's a CSS issue. If not, it's a code issue.
  • No it'st not in the HTML, I've used inspector to inject that message but it's not in the HTML at all. Edit: Never mind, I had to go through Fieldset to Fieldset_Feild classes to find out why errors weren't rendered, default config for inline_errors is false, it's WRONG, there's no doc about forms' templates either.. So, if the default whole-form-build method is used, wouldn't get any error displayed and it's not quite obvious why it wouldn't. Can the validation lang file be overriden by a json one? Is there a way to change the valid_string->numeric error template? I don't really like the "The valid string rule :rule(:param:1) failed for field :label" message... that might not be understandable for the average user
  • Problem with the form-fieldset-validation trio is that it's so very complex that all in's and out's are difficult to document or even explain. Which is why for v2 we're worked very hard to rework it. I'm not sure how you see overriding a lang file with a json what? There is one message defined of one rule. Which obviously can be difficult if you have a rule with lots of options, for which you ideally would like to have a unique message. Ideally you would like a custom message for the combination of field, rule and parameter, but that's not possible atm.
  • As I can override the form config using another one in my app/config, I'd like to override lang/en/validation.php but with a json file, because I'm using json for everything I don't want to use one php file to override the validation, as all others are json
  • You can create that kind of forms and still use the fieldset object. Instead of generating the entire fieldset form at once, you can also do it for individual fields using $fieldsetname->field('fieldname')->build(). It will generate the HTML tags for only the 'fieldname' field. This way you can create custom forms, while still retaining all fieldset features like populate, repopulate and adding fields from a model. And I would strongly suggest never to use Input::get/Input::post in a view unless it was properly sanitized. If not it's a hack waiting to happen. If you want to pass input, to that in the controller so it get's escaped when passed to the view.
  • No, I won't use use Input in the view but in the controller, before saving proper valid data :) So how would I pass the fieldset to the view to be parsed? $data = Fieldset::forge('profile')->add_model('Model_Client');
    return View::forge('view.smarty', $data); Because I need the object $fielsetname (profile) to be rendered with Smarty (I won't use explicit php in my views) Oh, another thing, I need some fields to be rendered with custom attibutes, so I think it's OK if I define in ORM the type, class, placeholdern and such things into the form => array()?
  • You can just pass the fieldset object to the view, and in the view like so: http://scrp.at/bwb (obviously in Smarty it would work a bit different). Here $form is the fieldset object. It's ok to use the form array in the ORM properties, that's what it's there for.
  • Ok this is what I have and doesn't really work :/ Controller
    http://scrp.at/bB9
    Model
    http://scrp.at/bB-
    Test view
    http://scrp.at/bB_ This is the error:
    ErrorException [ Error ]: Call to a member function field() on a non-object
    <?php echo $_smarty_tpl->tpl_vars->value->field('name')->build();?> I also tried using $data and passing $data to the view but didn't work (same error) I've tried to var_dump using $data and I think I'd get some table rows, label and input in the cells, is there any way to change that? I just need <label><input> in that order :) I'm using css and I'm not tables friend :D
  • This is probably the issue:
    // your controller code
    return View::forge('clients/add.smarty', $profile);
    
    // should be
    return View::forge('clients/add.smarty', array('profile' => $profile));
    
  • I've already tried that with $data = Fieldset::forge('profile')->add_model('Model_Client'); but I still get the same error, tried doing var_dump to the $profile and seems to be fine, but if i do it to $profile->field() the same error is given ErrorException [ Error ]: Call to a member function field() on a non-object So, seems like the method doesn't even exist, I've even tried unchaining methods maybe add_model didn't return the object I needed, but no, it doesnt work at all
  • I do this in my controller: http://scrp.at/bCd So perhaps it's related to output encoding, and you should use the false parameter of set() to pass the fieldset object to the view.
  • Hello, It doesn't work, and by the time it gets to work I think I'll have a table row with label and input if I use the build() method, in some cases I don't want the label to be rendered, only the input, I use placeholders for those fields, so for me is easier to not use the Fieldset class, that's why I asked if there's a way to repopulate not using the "premade syntax" of the Fieldset class, something like the old set_value() will work here, is it possible?
  • If you don't use the fieldset to generate the form but use plain handcoded HTML, there is nothing to prepopulate. So I don't see how that could be done.
  • I just want to make sure, did you read the code I posted? It's not "plain handcoded HTML" but a different cleaner markup, I can validate and generate custom forms without using Fieldset class, what I can't do is repopulate fields (not pre), I know you are an experimented CodeIgniter programmer so I think you know what I mean when I say there was a helper set_value() to repopulate data, even if it was handcoded. The Fieldset class bonus is the capability to prepopulate, repopulate and generate form tables, it's a cool feature but not what I need, I need pretty basic features and this became an unfeature. At least is there any way to customize the output without rewriting the whole class? (and I mean, whole output methods)
  • I'm responding to your statement that you don't like the build() output and so you're not going to use the Fieldset class. If you don't, there is nothing to repopulate. You will have to manually create a data structure that you pass on to your view, and populate that with Input::post() data in case you need to repopulate. There is no set_value() equivalent. You can something like this in your controller:
    // init the formdata array
    $formdata = array();
    
    // ... some code here
    
    // repopulate from posted input
    $formdata = array_merge($formdata, \Input::post());
    
    // and pass it to the form
    return \View::forge('myview', array('formdata' => $formdata));
    

    and for your view:
    <?php echo Form::input('fieldname', \Arr::get($formdata, 'fieldname', 'default value if not present'));
    
    so use Arr:get() to simulate CI's set_value() functionality. I would not use the Input class in your views, as input data will not be escaped like data passed to the view. This may be a security issue, and require you to escape manually, using the e() helper.
  • As to customizing the Fieldset: you can define your own form templates, you don't have to use the default one. If you want to modify the standard template, copy core/config/form.php to app/config and modify it. If you want a custom template, either pass that to Fieldset::forge() as part of the config, or use the fieldset set_config() method to set custom form elements.
  • Hello, That's the answer I was trying to get :D thank you!!! I wasn't thinking about using Input in my views, but using Session :) I still have a problem and I think it might be Smarty or Parser package
    $view = View::forge('clients/add.smarty');
    $view->set('data', $data, false);
    return $view;
    

    http://i.imgur.com/Q7jzm.png It says print_r because I'm trying to find out what is there
  • I'm not familiar with Smarty, so I can't help you there. Your snippet uses the template variable 'data' (first parameter), but your template uses 'profile'? And if you print_r() that ->value, what do you get then?

Howdy, Stranger!

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

In this Discussion