Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Check if all wanted Inputs are present
  • HIi there,

    I wanted to use a REST Controller in order to send a mail from my contact form to my SMTP server.

    Now, my question is, how can I easily validate that all my required inputs are there?
    I know I can just use the `empty()` function from PHP, but to do that for 4 different inputs can get dirty (especially if I do it for a longer period of time with more and more inputs across the site)


  • If by input you mean "posted data", you use the Validation class.

    Create an instance, set up your validation rules, then run it on the input.
  • I have created this code:

    // set the validator instance
    $v = Validation::forge();
    // Set up our required validation rules
    $v->add('name', '')->add_rule('required');
    $v->add('email','')->add_rule('required');
    $v->add('subject','')->add_rule('required');
    $v->add('message','')->add_rule('required');

    if($v->run()){
    echo "yay!";
    }else{
    echo "nay!";
    print_r($v->error());
    }


    and it seems to work just fine (I will add constraints later), but when I do not fill in one of the things, I get greeted by a huge array (duh, I use print_r()).
    How can I get just the invalid forms?

    Also, while I'm at it, how can I link it to the form itself (so I can easily display a message)
  • That greatly depends on how your forms are generated, so I don't have a set answer for that.

    Some just output the messages on top of the form, some use the key values of the errors() array to give the input field a red border:

    <input name="fieldname" class="<{?php array_key_exists('fieldname', $errors)?"input-error":""?>" />

    Some have more code to include the error message in the HTML, so it appears near the field, pops up with you enter it, etc. But that is front-end stuff, outside the scope of the framework.

    Fieldset forms have an "error_msg" and an "error_class" variable in the form template. These can be used if you use the fieldset for validation, instead of a separate validation object.

    error() returns an assoc array with field names => validation error objects. The object has a get_message() method to fetch the error message, with or without formatting (either passed of from the configuration). You can also echo the object directly, which will output the message.


  • The form is build using this code:

    $fieldset = Fieldset::forge('contact-form',
    array(
    "form_attributes" => array (
    "class" => "contact-form"
    )
    )
    );
    $form = $fieldset->form();
    $form->add('name','',array("placeholder" => "Your Name", "class" => "form-control"));
    $form->add('email','',array("placeholder" => "Your Email", "class" => "form-control"));
    $form->add('subject','',array("placeholder" => "Subject", "class" => "form-control"));
    $form->add('message','',array("placeholder" => "Your Message", "class" => "form-control", "type" => "textarea"));
    $form->add('submit','',array("type" => "submit","value" => "Submit", "class" => "btn"));
    ?>


    I came by this tutorial so I'll see if It can be of any help
    https://www.tutorialspoint.com/fuelphp/fuelphp_advanced_form_programming.htm

Howdy, Stranger!

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

In this Discussion