Git Product home page Git Product logo

Comments (6)

timgavin avatar timgavin commented on June 3, 2024

That's the code that you use. If it's not working for you, then please show your code.

from formr.

heisbrainiac avatar heisbrainiac commented on June 3, 2024

I need code like this screenshot
Screenshot_20220310-020857_Chrome

from formr.

timgavin avatar timgavin commented on June 3, 2024

Ok, I think I understand what you want. Add this function to class.formr.php and see if it's what you're looking for

public function error($key)
{
    foreach ($this->errors as $k => $message) {
        if ($key == $k) {
            $this->_echo($message);
        }
    }
}

Then use it in your form like this

$form->text('first_name', 'First name');
$form->error('first_name');

$form->text('last_name', 'Last name');
$form->error('last_name');

from formr.

heisbrainiac avatar heisbrainiac commented on June 3, 2024

Yeah u got me right 🙌, really appreciate and one more pls can unique validation be added, I mean to check data already exist in database field

from formr.

timgavin avatar timgavin commented on June 3, 2024

Cool, I'll add that to the next release.

Can you give me an example of what you mean by unique validation? Normally you'd just do if($database->result == $somevariable), so I'm unclear how you'd use Formr to validate a database result, especially since Formr doesn't work with databases.

from formr.

heisbrainiac avatar heisbrainiac commented on June 3, 2024

This is sample class

class UniqueRule extends Rule
{
protected $message = ":attribute :value has been used";

protected $fillableParams = ['table', 'column', 'except'];

protected $pdo;

public function __construct(PDO $pdo)
{
    $this->pdo = $pdo;
}

public function check($value): bool
{
    // make sure required parameters exists
    $this->requireParameters(['table', 'column']);

    // getting parameters
    $column = $this->parameter('column');
    $table = $this->parameter('table');
    $except = $this->parameter('except');

    if ($except AND $except == $value) {
        return true;
    }

    // do query
    $stmt = $this->pdo->prepare("select count(*) as count from `{$table}` where `{$column}` = :value");
    $stmt->bindParam(':value', $value);
    $stmt->execute();
    $data = $stmt->fetch(PDO::FETCH_ASSOC);

    // true for valid, false for invalid
    return intval($data['count']) === 0;
}

}

And this is the usage

$validation = $validator->validate($_POST, [
'email' => 'email|unique:users,email'
]);

Check the library here
https://github.com/rakit/validation

from formr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.